Skip to content

Commit

Permalink
postgresql_query, postgresql_script: don't convert list arguments to …
Browse files Browse the repository at this point in the history
…postgres arrays - let psycopg2 do it
  • Loading branch information
fhamme committed Feb 10, 2023
1 parent 984bf53 commit 089d434
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 51 deletions.
38 changes: 0 additions & 38 deletions plugins/module_utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,44 +396,6 @@ def set_search_path(cursor, search_path):
cursor.execute('SET search_path TO %s' % search_path)


def convert_elements_to_pg_arrays(obj):
"""Convert list elements of the passed object
to PostgreSQL arrays represented as strings.
Args:
obj (dict or list): Object whose elements need to be converted.
Returns:
obj (dict or list): Object with converted elements.
"""
if isinstance(obj, dict):
for (key, elem) in iteritems(obj):
if isinstance(elem, list):
obj[key] = list_to_pg_array(elem)

elif isinstance(obj, list):
for i, elem in enumerate(obj):
if isinstance(elem, list):
obj[i] = list_to_pg_array(elem)

return obj


def list_to_pg_array(elem):
"""Convert the passed list to PostgreSQL array
represented as a string.
Args:
elem (list): List that needs to be converted.
Returns:
elem (str): String representation of PostgreSQL array.
"""
elem = str(elem).strip('[]')
elem = '{' + elem + '}'
return elem


def convert_to_supported(val):
"""Convert unsupported type to appropriate.
Args:
Expand Down
6 changes: 0 additions & 6 deletions plugins/modules/postgresql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@
)
from ansible_collections.community.postgresql.plugins.module_utils.postgres import (
connect_to_db,
convert_elements_to_pg_arrays,
convert_to_supported,
ensure_required_libs,
get_conn_params,
Expand Down Expand Up @@ -442,11 +441,6 @@ def main():
else:
args = None

# Convert elements of type list to strings
# representing PG arrays
if args:
args = convert_elements_to_pg_arrays(args)

# Set defaults:
changed = False

Expand Down
7 changes: 0 additions & 7 deletions plugins/modules/postgresql_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,9 @@
)
from ansible_collections.community.postgresql.plugins.module_utils.postgres import (
connect_to_db,
convert_elements_to_pg_arrays,
convert_to_supported,
ensure_required_libs,
get_conn_params,
list_to_pg_array,
postgres_common_argument_spec,
set_search_path,
TYPES_NEED_TO_CONVERT,
Expand Down Expand Up @@ -299,11 +297,6 @@ def main():
else:
args = None

# Convert elements of type list to strings
# representing PG arrays
if args:
args = convert_elements_to_pg_arrays(args)

# Execute script content:
try:
cursor.execute(script_content, args)
Expand Down

0 comments on commit 089d434

Please sign in to comment.