From 10ab41d587e0a9f4c14dc9ae6f66e0152837b6c2 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sun, 14 Jan 2024 06:15:20 -0600 Subject: [PATCH] Strip comments to ensure more stable parameter pre-binding (fixes #26) --- database/macrostrat/database/utils.py | 2 +- database/test_database.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/database/macrostrat/database/utils.py b/database/macrostrat/database/utils.py index 3ab9f83..ce80361 100644 --- a/database/macrostrat/database/utils.py +++ b/database/macrostrat/database/utils.py @@ -150,7 +150,7 @@ def _get_queries(sql, interpret_as_file=None): if isinstance(sql, Path): sql = sql.read_text() - return split(sql) + return split(format(sql, strip_comments=True)) def _is_prebind_param(param): diff --git a/database/test_database.py b/database/test_database.py index f0388ab..ca60eea 100644 --- a/database/test_database.py +++ b/database/test_database.py @@ -278,6 +278,16 @@ def test_run_query_2(db): assert r1[0].name == "Test" +def test_ambiguous_comment_bind(db): + sql = """ + /* This is a comment {with_fake_param} */ + SELECT name FROM sample WHERE name = {name} + """ + res = db.run_query(sql, dict(name=Literal("Test"))) + data = res.scalar() + assert data == "Test" + + def test_close_connection(conn): """ Basic test demonstrating the underlying capability to kill a long-running query