Skip to content

Commit 040471c

Browse files
authored
docs(FIR-44268): add documentation for paramstyle (#451)
1 parent d5f4c8e commit 040471c

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

docsrc/Connecting_and_queries.rst

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,21 @@ placeholders and then pass values into those placeholders when the query is run.
362362
protects against SQL injection attacks and also helps manage dynamic queries that are
363363
likely to change, such as filter UIs or access control.
364364

365+
There are two supported styles for parameterized queries in the Firebolt Python SDK:
366+
367+
* **QMARK style** (default): Use question marks ``?`` as placeholders. This is controlled by the ``firebolt.db.paramstyle`` variable set to ``"qmark"`` or ``"native"``. Substitution is performed on the client side.
368+
* **FB Numeric style**: Use numbered placeholders ``$1, $2, ...``. This is enabled by setting ``firebolt.db.paramstyle = "fb_numeric"`` before connecting. Substitution is performed on the server side, providing additional protection against SQL injection.
369+
365370
To run a parameterized query, use the ``execute()`` cursor method. Add placeholders to
366-
your statement using question marks ``?``, and in the second argument pass a tuple of
367-
parameters equal in length to the number of ``?`` in the statement.
371+
your statement using the appropriate style, and in the second argument pass a tuple of
372+
parameters equal in length to the number of placeholders in the statement.
368373

374+
**QMARK style example (default):**
369375

370376
::
371377

378+
# No need to set paramstyle, it defaults to "qmark"
379+
372380
cursor.execute(
373381
"""
374382
CREATE FACT TABLE IF NOT EXISTS test_table2 (
@@ -379,15 +387,31 @@ parameters equal in length to the number of ``?`` in the statement.
379387
PRIMARY INDEX id;"""
380388
)
381389

390+
cursor.execute(
391+
"INSERT INTO test_table2 VALUES (?, ?, ?)",
392+
(1, "hello", "2018-01-01"),
393+
)
394+
395+
396+
**fb_numeric style example (server-side substitution):**
382397

383398
::
384399

400+
import firebolt.db
401+
firebolt.db.paramstyle = "fb_numeric"
402+
385403
cursor.execute(
386-
"INSERT INTO test_table2 VALUES (?, ?, ?)",
387-
(1, "apple", "2018-01-01"),
404+
"INSERT INTO test_table2 VALUES ($1, $2, $3)",
405+
(2, "world", "2018-01-02"),
406+
)
407+
408+
# paramstyle only needs to be set once, it will be used for all subsequent queries
409+
410+
cursor.execute(
411+
"INSERT INTO test_table2 VALUES ($1, $2, $3)",
412+
(3, "!", "2018-01-03"),
388413
)
389414

390-
cursor.close()
391415

392416
.. _parameterized_query_executemany_example:
393417

@@ -397,6 +421,10 @@ as values in the second argument.
397421

398422
::
399423

424+
import firebolt.db
425+
# Explicitly set paramstyle to "qmark" for QMARK style in case it was changed
426+
firebolt.db.paramstyle = "qmark"
427+
400428
cursor.executemany(
401429
"INSERT INTO test_table2 VALUES (?, ?, ?)",
402430
(

0 commit comments

Comments
 (0)