|
23 | 23 | options: |
24 | 24 | query: |
25 | 25 | description: |
26 | | - - SQL query to run. Variables can be escaped with psycopg2 syntax |
| 26 | + - SQL query string or list of queries to run. Variables can be escaped with psycopg2 syntax |
27 | 27 | U(http://initd.org/psycopg/docs/usage.html). |
28 | | - type: str |
| 28 | + type: raw |
29 | 29 | positional_args: |
30 | 30 | description: |
31 | 31 | - List of values to be passed as positional arguments to the query. |
|
132 | 132 | db: acme |
133 | 133 | query: SELECT version() |
134 | 134 |
|
| 135 | +# The result of each query will be stored in query_all_results return value |
| 136 | +- name: Run several queries against acme db |
| 137 | + community.postgresql.postgresql_query: |
| 138 | + db: acme |
| 139 | + query: |
| 140 | + - SELECT version() |
| 141 | + - SELECT id FROM accounts |
| 142 | +
|
135 | 143 | - name: Select query to db acme with positional arguments and non-default credentials |
136 | 144 | community.postgresql.postgresql_query: |
137 | 145 | db: acme |
|
268 | 276 | sample: [{"Column": "Value1"},{"Column": "Value2"}] |
269 | 277 | query_list: |
270 | 278 | description: |
271 | | - - This return value has been B(deprecated) and will be removed in |
272 | | - community.postgresql 3.0.0. |
273 | 279 | - List of executed queries. |
274 | 280 | Useful when reading several queries from a file. |
275 | 281 | returned: always |
|
278 | 284 | sample: ['SELECT * FROM foo', 'SELECT * FROM bar'] |
279 | 285 | query_all_results: |
280 | 286 | description: |
281 | | - - This return value has been B(deprecated) and will be removed in |
282 | | - community.postgresql 3.0.0. |
283 | 287 | - List containing results of all queries executed (one sublist for every query). |
284 | | - Useful when reading several queries from a file. |
| 288 | + Useful when running a list of queries. |
285 | 289 | returned: always |
286 | 290 | type: list |
287 | 291 | elements: list |
@@ -340,7 +344,7 @@ def insane_query(string): |
340 | 344 | def main(): |
341 | 345 | argument_spec = postgres_common_argument_spec() |
342 | 346 | argument_spec.update( |
343 | | - query=dict(type='str'), |
| 347 | + query=dict(type='raw'), |
344 | 348 | db=dict(type='str', aliases=['login_db']), |
345 | 349 | positional_args=dict(type='list', elements='raw'), |
346 | 350 | named_args=dict(type='dict'), |
@@ -370,6 +374,9 @@ def main(): |
370 | 374 | search_path = module.params["search_path"] |
371 | 375 | as_single_query = module.params["as_single_query"] |
372 | 376 |
|
| 377 | + if query and not isinstance(query, (str, list)): |
| 378 | + module.fail_json(msg="query argument must be of type string or list") |
| 379 | + |
373 | 380 | if not trust_input: |
374 | 381 | # Check input for potentially dangerous elements: |
375 | 382 | check_input(module, session_role) |
@@ -411,7 +418,10 @@ def main(): |
411 | 418 | except Exception as e: |
412 | 419 | module.fail_json(msg="Cannot read file '%s' : %s" % (path_to_script, to_native(e))) |
413 | 420 | else: |
414 | | - query_list.append(query) |
| 421 | + if isinstance(query, str): |
| 422 | + query_list.append(query) |
| 423 | + else: # if it's a list |
| 424 | + query_list = query |
415 | 425 |
|
416 | 426 | # Ensure psycopg2 libraries are available before connecting to DB: |
417 | 427 | ensure_required_libs(module) |
|
0 commit comments