From ffa172e3a603f7950df260fe968aab5987a0fe1e Mon Sep 17 00:00:00 2001 From: Andrey Klychkov Date: Mon, 10 Jun 2019 12:15:07 +0300 Subject: [PATCH] postgresql: moved function exec_sql from the modules to module_utils (#57603) --- lib/ansible/module_utils/postgres.py | 32 ++++++++++++++++ .../database/postgresql/postgresql_copy.py | 38 +++---------------- .../postgresql/postgresql_sequence.py | 38 +++---------------- 3 files changed, 42 insertions(+), 66 deletions(-) diff --git a/lib/ansible/module_utils/postgres.py b/lib/ansible/module_utils/postgres.py index a0c9a162485..5d6232930d9 100644 --- a/lib/ansible/module_utils/postgres.py +++ b/lib/ansible/module_utils/postgres.py @@ -147,3 +147,35 @@ def connect_to_db(module, autocommit=False, fail_on_conn=True, warn_db_default=T db_connection = None return db_connection + + +def exec_sql(obj, query, ddl=False, add_to_executed=True): + """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). + + Arguments: + obj (obj) -- must be an object of a user class. + The object must have module (AnsibleModule class object) and + cursor (psycopg cursor object) attributes + query (str) -- SQL query to execute + ddl (bool) -- must return True or False instead of rows (typical for DDL queries) + (default False) + add_to_executed (bool) -- append the query to obj.executed_queries attribute + """ + try: + obj.cursor.execute(query) + + if add_to_executed: + obj.executed_queries.append(query) + + if not ddl: + res = obj.cursor.fetchall() + return res + return True + except Exception as e: + obj.module.fail_json(msg="Cannot execute SQL '%s': %s" % (query, to_native(e))) + return False diff --git a/lib/ansible/modules/database/postgresql/postgresql_copy.py b/lib/ansible/modules/database/postgresql/postgresql_copy.py index d720692c718..d4515010822 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_copy.py +++ b/lib/ansible/modules/database/postgresql/postgresql_copy.py @@ -175,43 +175,15 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.database import pg_quote_identifier -from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec +from ansible.module_utils.postgres import ( + connect_to_db, + exec_sql, + postgres_common_argument_spec, +) from ansible.module_utils._text import to_native from ansible.module_utils.six import iteritems -def exec_sql(obj, query, ddl=False, add_to_executed=True): - """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). - - Arguments: - obj (obj) -- must be an object of a user class. - The object must have module (AnsibleModule class object) and - cursor (psycopg cursor object) attributes - query (str) -- SQL query to execute - ddl (bool) -- must return True or False instead of rows (typical for DDL queries) - (default False) - add_to_executed (bool) -- append the query to obj.executed_queries attribute - """ - try: - obj.cursor.execute(query) - - if add_to_executed: - obj.executed_queries.append(query) - - if not ddl: - res = obj.cursor.fetchall() - return res - return True - except Exception as e: - obj.module.fail_json(msg="Cannot execute SQL '%s': %s" % (query, to_native(e))) - return False - - class PgCopyData(object): """Implements behavior of COPY FROM, COPY TO PostgreSQL command. diff --git a/lib/ansible/modules/database/postgresql/postgresql_sequence.py b/lib/ansible/modules/database/postgresql/postgresql_sequence.py index aefc143554d..d74d97cdb23 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_sequence.py +++ b/lib/ansible/modules/database/postgresql/postgresql_sequence.py @@ -284,42 +284,14 @@ except ImportError: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.database import pg_quote_identifier -from ansible.module_utils.postgres import connect_to_db, postgres_common_argument_spec +from ansible.module_utils.postgres import ( + connect_to_db, + exec_sql, + postgres_common_argument_spec, +) from ansible.module_utils._text import to_native -def exec_sql(obj, query, ddl=False, add_to_executed=True): - """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). - - Arguments: - obj (obj) -- must be an object of a user class. - The object must have module (AnsibleModule class object) and - cursor (psycopg cursor object) attributes - query (str) -- SQL query to execute - ddl (bool) -- must return True or False instead of rows (typical for DDL queries) - (default False) - add_to_executed (bool) -- append the query to obj.executed_queries attribute - """ - try: - obj.cursor.execute(query) - - if add_to_executed: - obj.executed_queries.append(query) - - if not ddl: - res = obj.cursor.fetchall() - return res - return True - except Exception as e: - obj.module.fail_json(msg="Cannot execute SQL '%s': %s" % (query, to_native(e))) - return False - - class Sequence(object): """Implements behavior of CREATE, ALTER or DROP SEQUENCE PostgreSQL command.