From 1c550cd4ef2fb719ddfcd97b8cab6109ffb169e3 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Wed, 25 Mar 2020 14:44:28 +0300 Subject: [PATCH] postgres: make interfaces of exec_sql clearer (#43) (cherry picked from commit fb6583a15c1f7fd8e48f4f3ef214ed4e74c3d8c6) --- plugins/module_utils/postgres.py | 15 +++++++-------- .../database/postgresql/postgresql_copy.py | 4 ++-- .../database/postgresql/postgresql_idx.py | 4 ++-- .../database/postgresql/postgresql_owner.py | 18 +++++++++--------- .../postgresql/postgresql_publication.py | 2 +- .../database/postgresql/postgresql_sequence.py | 10 +++++----- .../database/postgresql/postgresql_slot.py | 6 +++--- .../postgresql/postgresql_subscription.py | 2 +- .../database/postgresql/postgresql_table.py | 16 ++++++++-------- .../postgresql/postgresql_tablespace.py | 12 ++++++------ 10 files changed, 44 insertions(+), 45 deletions(-) diff --git a/plugins/module_utils/postgres.py b/plugins/module_utils/postgres.py index 63811c3055..5324d80676 100644 --- a/plugins/module_utils/postgres.py +++ b/plugins/module_utils/postgres.py @@ -122,13 +122,12 @@ def connect_to_db(module, conn_params, autocommit=False, fail_on_conn=True): return db_connection -def exec_sql(obj, query, query_params=None, ddl=False, add_to_executed=True, dont_exec=False): +def exec_sql(obj, query, query_params=None, return_bool=False, add_to_executed=True, dont_exec=False): """Execute SQL. Auxiliary function for PostgreSQL user classes. - Returns a query result if possible or True/False if ddl=True arg was passed. - It necessary for statements that don't return any result (like DDL queries). + Returns a query result if possible or a boolean value. Args: obj (obj) -- must be an object of a user class. @@ -139,8 +138,8 @@ def exec_sql(obj, query, query_params=None, ddl=False, add_to_executed=True, don Kwargs: query_params (dict or tuple) -- Query parameters to prevent SQL injections, could be a dict or tuple - ddl (bool) -- must return True or False instead of rows (typical for DDL queries) - (default False) + return_bool (bool) -- return True instead of rows if a query was successfully executed. + It's necessary for statements that don't return any result like DDL queries (default False). add_to_executed (bool) -- append the query to obj.executed_queries attribute dont_exec (bool) -- used with add_to_executed=True to generate a query, add it to obj.executed_queries list and return True (default False) @@ -167,7 +166,7 @@ def exec_sql(obj, query, query_params=None, ddl=False, add_to_executed=True, don else: obj.executed_queries.append(query) - if not ddl: + if not return_bool: res = obj.cursor.fetchall() return res return True @@ -246,7 +245,7 @@ class PgMembership(object): continue query = 'GRANT "%s" TO "%s"' % (group, role) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) if self.changed: self.granted[group].append(role) @@ -263,7 +262,7 @@ class PgMembership(object): continue query = 'REVOKE "%s" FROM "%s"' % (group, role) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) if self.changed: self.revoked[group].append(role) diff --git a/plugins/modules/database/postgresql/postgresql_copy.py b/plugins/modules/database/postgresql/postgresql_copy.py index 8f17158796..071d339974 100644 --- a/plugins/modules/database/postgresql/postgresql_copy.py +++ b/plugins/modules/database/postgresql/postgresql_copy.py @@ -253,7 +253,7 @@ class PgCopyData(object): if self.changed: self.executed_queries.append(' '.join(query_fragments)) else: - if exec_sql(self, ' '.join(query_fragments), ddl=True): + if exec_sql(self, ' '.join(query_fragments), return_bool=True): self.changed = True def copy_to(self): @@ -288,7 +288,7 @@ class PgCopyData(object): if self.changed: self.executed_queries.append(' '.join(query_fragments)) else: - if exec_sql(self, ' '.join(query_fragments), ddl=True): + if exec_sql(self, ' '.join(query_fragments), return_bool=True): self.changed = True def __transform_options(self): diff --git a/plugins/modules/database/postgresql/postgresql_idx.py b/plugins/modules/database/postgresql/postgresql_idx.py index 1b50c2a04e..507af2a32e 100644 --- a/plugins/modules/database/postgresql/postgresql_idx.py +++ b/plugins/modules/database/postgresql/postgresql_idx.py @@ -411,7 +411,7 @@ class Index(object): self.executed_query = query - if exec_sql(self, query, ddl=True, add_to_executed=False): + if exec_sql(self, query, return_bool=True, add_to_executed=False): return True return False @@ -447,7 +447,7 @@ class Index(object): self.executed_query = query - if exec_sql(self, query, ddl=True, add_to_executed=False): + if exec_sql(self, query, return_bool=True, add_to_executed=False): return True return False diff --git a/plugins/modules/database/postgresql/postgresql_owner.py b/plugins/modules/database/postgresql/postgresql_owner.py index 93d7f59ca5..3c7274ca1f 100644 --- a/plugins/modules/database/postgresql/postgresql_owner.py +++ b/plugins/modules/database/postgresql/postgresql_owner.py @@ -231,7 +231,7 @@ class PgOwnership(object): query.append('TO %s' % pg_quote_identifier(self.role, 'role')) query = ' '.join(query) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def set_owner(self, obj_type, obj_name): """Change owner of a database object. @@ -325,49 +325,49 @@ class PgOwnership(object): """Set the database owner.""" query = "ALTER DATABASE %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'database'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_func_owner(self): """Set the function owner.""" query = "ALTER FUNCTION %s OWNER TO %s" % (self.obj_name, pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_seq_owner(self): """Set the sequence owner.""" query = "ALTER SEQUENCE %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'table'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_schema_owner(self): """Set the schema owner.""" query = "ALTER SCHEMA %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'schema'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_table_owner(self): """Set the table owner.""" query = "ALTER TABLE %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'table'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_tablespace_owner(self): """Set the tablespace owner.""" query = "ALTER TABLESPACE %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'database'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_view_owner(self): """Set the view owner.""" query = "ALTER VIEW %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'table'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __set_mat_view_owner(self): """Set the materialized view owner.""" query = "ALTER MATERIALIZED VIEW %s OWNER TO %s" % (pg_quote_identifier(self.obj_name, 'table'), pg_quote_identifier(self.role, 'role')) - self.changed = exec_sql(self, query, ddl=True) + self.changed = exec_sql(self, query, return_bool=True) def __role_exists(self, role): """Return True if role exists, otherwise return False.""" diff --git a/plugins/modules/database/postgresql/postgresql_publication.py b/plugins/modules/database/postgresql/postgresql_publication.py index 10737f47df..d68d005ba8 100644 --- a/plugins/modules/database/postgresql/postgresql_publication.py +++ b/plugins/modules/database/postgresql/postgresql_publication.py @@ -562,7 +562,7 @@ class PgPublication(): self.executed_queries.append(query) return True else: - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) # =========================================== diff --git a/plugins/modules/database/postgresql/postgresql_sequence.py b/plugins/modules/database/postgresql/postgresql_sequence.py index 3834184ce6..f81d05ee20 100644 --- a/plugins/modules/database/postgresql/postgresql_sequence.py +++ b/plugins/modules/database/postgresql/postgresql_sequence.py @@ -419,7 +419,7 @@ class Sequence(object): if self.module.params.get('cycle'): query.append('CYCLE') - return exec_sql(self, ' '.join(query), ddl=True) + return exec_sql(self, ' '.join(query), return_bool=True) def drop(self): """Implements DROP SEQUENCE command behavior.""" @@ -429,7 +429,7 @@ class Sequence(object): if self.module.params.get('cascade'): query.append('CASCADE') - return exec_sql(self, ' '.join(query), ddl=True) + return exec_sql(self, ' '.join(query), return_bool=True) def rename(self): """Implements ALTER SEQUENCE RENAME TO command behavior.""" @@ -437,7 +437,7 @@ class Sequence(object): query.append(self.__add_schema()) query.append('RENAME TO %s' % pg_quote_identifier(self.module.params['rename_to'], 'sequence')) - return exec_sql(self, ' '.join(query), ddl=True) + return exec_sql(self, ' '.join(query), return_bool=True) def set_owner(self): """Implements ALTER SEQUENCE OWNER TO command behavior.""" @@ -445,7 +445,7 @@ class Sequence(object): query.append(self.__add_schema()) query.append('OWNER TO %s' % pg_quote_identifier(self.module.params['owner'], 'role')) - return exec_sql(self, ' '.join(query), ddl=True) + return exec_sql(self, ' '.join(query), return_bool=True) def set_schema(self): """Implements ALTER SEQUENCE SET SCHEMA command behavior.""" @@ -453,7 +453,7 @@ class Sequence(object): query.append(self.__add_schema()) query.append('SET SCHEMA %s' % pg_quote_identifier(self.module.params['newschema'], 'schema')) - return exec_sql(self, ' '.join(query), ddl=True) + return exec_sql(self, ' '.join(query), return_bool=True) def __add_schema(self): return '.'.join([pg_quote_identifier(self.schema, 'schema'), diff --git a/plugins/modules/database/postgresql/postgresql_slot.py b/plugins/modules/database/postgresql/postgresql_slot.py index c21c303156..4c812fdb93 100644 --- a/plugins/modules/database/postgresql/postgresql_slot.py +++ b/plugins/modules/database/postgresql/postgresql_slot.py @@ -192,19 +192,19 @@ class PgSlot(object): self.changed = exec_sql(self, query, query_params={'name': self.name, 'i_reserve': immediately_reserve}, - ddl=True) + return_bool=True) elif kind == 'logical': query = "SELECT pg_create_logical_replication_slot(%(name)s, %(o_plugin)s)" self.changed = exec_sql(self, query, - query_params={'name': self.name, 'o_plugin': output_plugin}, ddl=True) + query_params={'name': self.name, 'o_plugin': output_plugin}, return_bool=True) def drop(self): if not self.exists: return False query = "SELECT pg_drop_replication_slot(%(name)s)" - self.changed = exec_sql(self, query, query_params={'name': self.name}, ddl=True) + self.changed = exec_sql(self, query, query_params={'name': self.name}, return_bool=True) def __slot_exists(self): query = "SELECT slot_type FROM pg_replication_slots WHERE slot_name = %(name)s" diff --git a/plugins/modules/database/postgresql/postgresql_subscription.py b/plugins/modules/database/postgresql/postgresql_subscription.py index 856e423aa7..8623561a93 100644 --- a/plugins/modules/database/postgresql/postgresql_subscription.py +++ b/plugins/modules/database/postgresql/postgresql_subscription.py @@ -562,7 +562,7 @@ class PgSubscription(): self.executed_queries.append(query) return True else: - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) # =========================================== diff --git a/plugins/modules/database/postgresql/postgresql_table.py b/plugins/modules/database/postgresql/postgresql_table.py index 92190d5d5f..1449f2b69d 100644 --- a/plugins/modules/database/postgresql/postgresql_table.py +++ b/plugins/modules/database/postgresql/postgresql_table.py @@ -367,7 +367,7 @@ class Table(object): if tblspace: query += " TABLESPACE %s" % pg_quote_identifier(tblspace, 'database') - if exec_sql(self, query, ddl=True): + if exec_sql(self, query, return_bool=True): changed = True if owner: @@ -414,7 +414,7 @@ class Table(object): if tblspace: query += " TABLESPACE %s" % pg_quote_identifier(tblspace, 'database') - if exec_sql(self, query, ddl=True): + if exec_sql(self, query, return_bool=True): changed = True if owner: @@ -424,17 +424,17 @@ class Table(object): def truncate(self): query = "TRUNCATE TABLE %s" % pg_quote_identifier(self.name, 'table') - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def rename(self, newname): query = "ALTER TABLE %s RENAME TO %s" % (pg_quote_identifier(self.name, 'table'), pg_quote_identifier(newname, 'table')) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def set_owner(self, username): query = "ALTER TABLE %s OWNER TO %s" % (pg_quote_identifier(self.name, 'table'), pg_quote_identifier(username, 'role')) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def drop(self, cascade=False): if not self.exists: @@ -443,16 +443,16 @@ class Table(object): query = "DROP TABLE %s" % pg_quote_identifier(self.name, 'table') if cascade: query += " CASCADE" - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def set_tblspace(self, tblspace): query = "ALTER TABLE %s SET TABLESPACE %s" % (pg_quote_identifier(self.name, 'table'), pg_quote_identifier(tblspace, 'database')) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def set_stor_params(self, params): query = "ALTER TABLE %s SET (%s)" % (pg_quote_identifier(self.name, 'table'), params) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) # =========================================== diff --git a/plugins/modules/database/postgresql/postgresql_tablespace.py b/plugins/modules/database/postgresql/postgresql_tablespace.py index 923ed2e8a2..e022a376ff 100644 --- a/plugins/modules/database/postgresql/postgresql_tablespace.py +++ b/plugins/modules/database/postgresql/postgresql_tablespace.py @@ -287,14 +287,14 @@ class PgTablespace(object): location (str) -- tablespace directory path in the FS """ query = ("CREATE TABLESPACE %s LOCATION '%s'" % (pg_quote_identifier(self.name, 'database'), location)) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def drop(self): """Drop tablespace. Return True if success, otherwise, return False. """ - return exec_sql(self, "DROP TABLESPACE %s" % pg_quote_identifier(self.name, 'database'), ddl=True) + return exec_sql(self, "DROP TABLESPACE %s" % pg_quote_identifier(self.name, 'database'), return_bool=True) def set_owner(self, new_owner): """Set tablespace owner. @@ -308,7 +308,7 @@ class PgTablespace(object): return False query = "ALTER TABLESPACE %s OWNER TO %s" % (pg_quote_identifier(self.name, 'database'), new_owner) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def rename(self, newname): """Rename tablespace. @@ -320,7 +320,7 @@ class PgTablespace(object): """ query = "ALTER TABLESPACE %s RENAME TO %s" % (pg_quote_identifier(self.name, 'database'), newname) self.new_name = newname - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def set_settings(self, new_settings): """Set tablespace settings (options). @@ -358,7 +358,7 @@ class PgTablespace(object): setting (str) -- string in format "setting_name = 'setting_value'" """ query = "ALTER TABLESPACE %s RESET (%s)" % (pg_quote_identifier(self.name, 'database'), setting) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) def __set_setting(self, setting): """Set tablespace setting. @@ -369,7 +369,7 @@ class PgTablespace(object): setting (str) -- string in format "setting_name = 'setting_value'" """ query = "ALTER TABLESPACE %s SET (%s)" % (pg_quote_identifier(self.name, 'database'), setting) - return exec_sql(self, query, ddl=True) + return exec_sql(self, query, return_bool=True) # ===========================================