cardinal_pythonlib.sql.literals


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Functions to manipulate raw SQL.

cardinal_pythonlib.sql.literals.gen_items_from_sql_csv(s: str) Generator[str, None, None][source]

Splits a comma-separated list of quoted SQL values, with ' as the quote character. Allows escaping of the quote character by doubling it. Returns the quotes (and escaped quotes) as part of the result. Allows newlines etc. within the string passed.

cardinal_pythonlib.sql.literals.sql_comment(comment: str) str[source]

Transforms a single- or multi-line string into an ANSI SQL comment, prefixed by --.

cardinal_pythonlib.sql.literals.sql_date_literal(dt: date | DateTime | Arrow) str[source]

Transforms a Python object that is of duck type datetime.date into an ANSI SQL literal string, like ‘2000-12-31’.

cardinal_pythonlib.sql.literals.sql_datetime_literal(dt: datetime | DateTime | Arrow, subsecond: bool = False) str[source]

Transforms a Python object that is of duck type datetime.datetime into an ANSI SQL literal string, like '2000-12-31 23:59:59', or if subsecond=True, into the (non-ANSI) format '2000-12-31 23:59:59.123456' or similar.

cardinal_pythonlib.sql.literals.sql_dequote_string(s: str) str[source]

Reverses sql_quote_string().

cardinal_pythonlib.sql.literals.sql_quote_string(text: str) str

Transforms text into its ANSI SQL-quoted version, e.g. (in Python repr() format):

"some string"   -> "'some string'"
"Jack's dog"    -> "'Jack''s dog'"
cardinal_pythonlib.sql.literals.sql_string_literal(text: str) str[source]

Transforms text into its ANSI SQL-quoted version, e.g. (in Python repr() format):

"some string"   -> "'some string'"
"Jack's dog"    -> "'Jack''s dog'"