exasol / pyexasol

Exasol Python driver with low overhead, fast HTTP transport and compression
MIT License
72 stars 39 forks source link

Query text before execution #56

Closed kamal-1 closed 4 years ago

kamal-1 commented 4 years ago

We can get the query text after the query has executed.

q = "select name, val from {sn!i}.{tn!i}" p = { "sn": "ETL", "tn": "METADATA" } c.execute(q, p).fetchall() c.last_statement().query

Is there a way to get the query text substituted with the parameter values before the query execution?

littleK0i commented 4 years ago

Yes, of course.

q = "select name, val from {sn!i}.{tn!i}"
p = { "sn": "ETL", "tn": "METADATA" }
print(c.format.format(q, **p))

Reference for format() function: https://github.com/badoo/pyexasol/blob/master/docs/REFERENCE.md#format

General information about Exasols-specific formatting: https://github.com/badoo/pyexasol/blob/master/docs/SQL_FORMATTING.md

Enjoy!

littleK0i commented 4 years ago

Please note, the better way to specify schema for identifier is to use tuple.

q = "select name, val from {table!i}"
p = {"table": ("ETL", "METADATA")}
print(c.format.format(q, **p))

Result:

select name, val from ETL.METADATA
kamal-1 commented 4 years ago

Please note, the better way to specify schema for identifier is to use tuple.

q = "select name, val from {table!i}"
p = {"table": ("ETL", "METADATA")}
print(c.format.format(q, **p))

Result:

select name, val from ETL.METADATA

Awesome. Thanks for the quick response. I'll update the code based on you recommendation of using the tuple.