Open felipenazario opened 1 day ago
@felipenazario thanks for opening the issue. The concerns raised in #312 are still valid.
In particular, if in the postgresql_query
module you can pass your queries as a list, so there's no problem with determining when one query starts and ends: it's just an element of the list and the module executes them one by one and appends the result.
With the postgresql_script
we should also create a list of queries first to be able to append the result of each query and return it, however:
So if the getting a result of each query is important for a user, I would suggest using postgresql_query
instead.
If the user wants to get queries from a file, I think they can use the lookup plugin to fetch it and then other plugins to transform it to the yml list and pass it to the _query
module
Hi @Andersson007,
Nice to talk to you again, the last the we talked, I said...
As you already figure out, we do not split multiple queries in the same position. At least for now, when somebody send a Pull Request providing a special regex to find all query delimiters in all scenarios, it would be merged.
Below there is something to try the correct split.
import re
sql = "SELECT * FROM users; INSERT INTO users (name) VALUES ('John; Doe'); DELETE FROM users WHERE id = 1;INSERT INTO text VALUES ('my text ; blah ; blah'); SELECT 1"
statements = re.split(r';(?=(?:[^"\'"]|"[^"]*"|\'[^\']*\')*$)', sql)
# Remove empty strings and strip spaces
statements = [s.strip() for s in statements if s.strip()]
for statement in statements:
print(statement)
Let me know what do you think of it. Meanwhile, I will be looking for what you suggested.
if the user wants to get queries from a file, I think they can use the lookup plugin to fetch it and then other plugins to transform it to the yml list and pass it to the _query module
SUMMARY
Sometime ago I opened the issue 312 and I it is marked as closed and resolved, but it keeps the same behavior as before. I still believe that if the script file has N SQL queries, it should return N results. But today it only returns the last SQL Query in the script file.
ISSUE TYPE
COMPONENT NAME
community.postgresql.postgresql_script
ANSIBLE VERSION
COLLECTION VERSION
CONFIGURATION
OS / ENVIRONMENT
uname -a
cat /etc/os-release
STEPS TO REPRODUCE
EXPECTED RESULTS
ACTUAL RESULTS