From e85a1184a070779ac21528662c7cce682cbff308 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Thu, 5 Jul 2012 18:25:32 +0300 Subject: [PATCH] skytools.exists_view(): new function --- python/skytools/__init__.py | 1 + python/skytools/sqltools.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/python/skytools/__init__.py b/python/skytools/__init__.py index 84ed00de..73a0e9ed 100644 --- a/python/skytools/__init__.py +++ b/python/skytools/__init__.py @@ -108,6 +108,7 @@ _symbols = { 'exists_table': 'skytools.sqltools:exists_table', 'exists_temp_table': 'skytools.sqltools:exists_temp_table', 'exists_type': 'skytools.sqltools:exists_type', + 'exists_view': 'skytools.sqltools:exists_view', 'fq_name': 'skytools.sqltools:fq_name', 'fq_name_parts': 'skytools.sqltools:fq_name_parts', 'full_copy': 'skytools.sqltools:full_copy', diff --git a/python/skytools/sqltools.py b/python/skytools/sqltools.py index f8014eb4..558c0dc0 100644 --- a/python/skytools/sqltools.py +++ b/python/skytools/sqltools.py @@ -13,7 +13,7 @@ except ImportError: __all__ = [ "fq_name_parts", "fq_name", "get_table_oid", "get_table_pkeys", "get_table_columns", "exists_schema", "exists_table", "exists_type", - "exists_sequence", "exists_temp_table", + "exists_sequence", "exists_temp_table", "exists_view", "exists_function", "exists_language", "Snapshot", "magic_insert", "CopyPipe", "full_copy", "DBObject", "DBSchema", "DBTable", "DBFunction", "DBLanguage", "db_install", "installer_find_file", "installer_apply_file", @@ -137,6 +137,16 @@ def exists_sequence(curs, seq_name): res = curs.fetchone() return res[0] +def exists_view(curs, view_name): + """Does view exists?""" + schema, name = fq_name_parts(view_name) + q = """select count(1) from pg_namespace n, pg_class c + where c.relnamespace = n.oid and c.relkind = 'v' + and n.nspname = %s and c.relname = %s""" + curs.execute(q, [schema, name]) + res = curs.fetchone() + return res[0] + def exists_type(curs, type_name): """Does type exists?""" schema, name = fq_name_parts(type_name) -- 2.39.5