Skip to content

Commit

Permalink
fix(targets): Quote column names in INSERT statement (meltano#2200)
Browse files Browse the repository at this point in the history
* Update sql.py

* Mattwill09 patch 1 (#1)

* Update sql.py

* Update test_target_sqlite.py

* Update singer_sdk/sinks/sql.py

* Use IdentifierPreparer

---------

Co-authored-by: Matthew Will <[email protected]>
  • Loading branch information
edgarrmondragon and mattwill09 authored Jan 30, 2024
1 parent af3b678 commit bfc1bc1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 6 additions & 1 deletion singer_sdk/sinks/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import sqlalchemy as sa
from pendulum import now
from sqlalchemy.sql import quoted_name
from sqlalchemy.sql.expression import bindparam

from singer_sdk.connectors import SQLConnector
Expand Down Expand Up @@ -279,10 +280,14 @@ def generate_insert_statement(
An insert statement.
"""
property_names = list(self.conform_schema(schema)["properties"].keys())
column_identifiers = [
self.connector.quote(quoted_name(name, quote=True))
for name in property_names
]
statement = dedent(
f"""\
INSERT INTO {full_table_name}
({", ".join(property_names)})
({", ".join(column_identifiers)})
VALUES ({", ".join([f":{name}" for name in property_names])})
""", # noqa: S608
)
Expand Down
5 changes: 3 additions & 2 deletions tests/samples/test_target_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,15 @@ def test_record_with_missing_properties(
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"table": {"type": "string"},
},
},
[],
dedent(
"""\
INSERT INTO test_stream
(id, name)
VALUES (:id, :name)""",
(id, name, "table")
VALUES (:id, :name, :table)""",
),
),
],
Expand Down

0 comments on commit bfc1bc1

Please sign in to comment.