sql – SQL string composition#
The module contains objects and functions useful to generate SQL dynamically,
in a convenient and safe way. SQL identifiers (e.g. names of tables and
fields) cannot be passed to the execute() method like query
arguments:
# This will not work
table_name = 'my_table'
cur.execute("INSERT INTO %s VALUES (%s, %s)", [table_name, 10, 20])
The SQL query should be composed before the arguments are merged, for instance:
# This works, but it is not optimal
table_name = 'my_table'
cur.execute(
"INSERT INTO %s VALUES (%%s, %%s)" % table_name,
[10, 20])
This sort of works, but it is an accident waiting to happen: the table name
may be an invalid SQL literal and need quoting; even more serious is the
security problem in case the table name comes from an untrusted source. The
name should be escaped using escape_identifier():
from psycopg.pq import Escaping
# This works, but it is not optimal
table_name = 'my_table'
cur.execute(
"INSERT INTO %s VALUES (%%s, %%s)" % Escaping.escape_identifier(table_name),
[10, 20])
This is now safe, but it somewhat ad-hoc. In case, for some reason, it is
necessary to include a value in the query string (as opposite as in a value)
the merging rule is still different. It is also still relatively dangerous: if
escape_identifier() is forgotten somewhere, the program will usually work,
but will eventually crash in the presence of a table or field name with
containing characters to escape, or will present a potentially exploitable
weakness.
The objects exposed by the psycopg.sql module allow generating SQL
statements on the fly, separating clearly the variable parts of the statement
from the query parameters:
from psycopg import sql
cur.execute(
sql.SQL("INSERT INTO {} VALUES (%s, %s)")
.format(sql.Identifier('my_table')),
[10, 20])
Module usage#
Usually you should express the template of your query as an SQL instance
with {}-style placeholders and use format() to merge the variable
parts into them, all of which must be Composable subclasses. You can still
have %s-style placeholders in your query and pass values to
execute(): such value placeholders will be untouched by
format():
query = sql.SQL("SELECT {field} FROM {table} WHERE {pkey} = %s").format(
field=sql.Identifier('my_name'),
table=sql.Identifier('some_table'),
pkey=sql.Identifier('id'))
The resulting object is meant to be passed directly to cursor methods such as
execute(), executemany(),
copy(), but can also be used to compose a query as a Python
string, using the as_string() method:
cur.execute(query, (42,))
full_query = query.as_string(cur)
If part of your query is a variable sequence of arguments, such as a
comma-separated list of field names, you can use the SQL.join() method to
pass them to the query:
query = sql.SQL("SELECT {fields} FROM {table}").format(
fields=sql.SQL(',').join([
sql.Identifier('field1'),
sql.Identifier('field2'),
sql.Identifier('field3'),
]),
table=sql.Identifier('some_table'))
sql objects#
The sql objects are in the following inheritance hierarchy:
Composable: the base class exposing the common interface|__ SQL: a literal snippet of an SQL query|__ Identifier: a PostgreSQL identifier or dot-separated sequence of identifiers|__ Literal: a value hardcoded into a query- class psycopg.sql.Composable#
Abstract base class for objects that can be used to compose an SQL string.
Composableobjects can be joined using the+operator: the result will be aComposedinstance containing the objects joined. The operator*is also supported with an integer argument: the result is aComposedinstance containing the left argument repeated as many times as requested.SQLandComposedobjects can be passed directly toexecute(),executemany(),copy()in place of the query string.- as_string(context: Optional[AdaptContext] = None) str#
Return the value of the object as string.
- Parameters:
context (
connectionorcursor) – the context to evaluate the string into.
Changed in version 3.2: The
contextparameter is optional.Warning
If a context is not specified, the results are “generic” and not tailored for a specific target connection. Details such as the connection encoding and escaping style will not be taken into account.
- abstract as_bytes(context: Optional[AdaptContext] = None) bytes#
Return the value of the object as bytes.
- Parameters:
context (
connectionorcursor) – the context to evaluate the object into.
The method is automatically invoked by
execute(),executemany(),copy()if aComposableis passed instead of the query string.
Changed in version 3.2: The
contextparameter is optional. Seeas_stringfor details.
- class psycopg.sql.SQL(obj: LiteralString)#
A
Composablerepresenting a snippet of SQL statement.SQLexposesjoin()andformat()methods useful to create a template where to merge variable parts of a query (for instance field or table names).The
objstring doesn’t undergo any form of escaping, so it is not suitable to represent variable identifiers or values: you should only use it to pass constant strings representing templates or snippets of SQL statements; use other objects such asIdentifierorLiteralto represent variable parts.SQLobjects can be passed directly toexecute(),executemany(),copy()in place of the query string.Example:
>>> query = sql.SQL("SELECT {0} FROM {1}").format( ... sql.SQL(', ').join([sql.Identifier('foo'), sql.Identifier('bar')]), ... sql.Identifier('table')) >>> print(query.as_string(conn)) SELECT "foo", "bar" FROM "table"
Changed in version 3.1: The input object should be a
LiteralString. See PEP 675 for details.- format(*args: Any, **kwargs: Any) Composed#
Merge
Composableobjects into a template.- Parameters:
args – parameters to replace to numbered (
{0},{1}) or auto-numbered ({}) placeholderskwargs – parameters to replace to named (
{name}) placeholders
- Returns:
the union of the
SQLstring with placeholders replaced- Return type:
The method is similar to the Python
str.format()method: the string template supports auto-numbered ({}), numbered ({0},{1}…), and named placeholders ({name}), with positional arguments replacing the numbered placeholders and keywords replacing the named ones. However placeholder modifiers ({0!r},{0:<10}) are not supported.If a
Composableobjects is passed to the template it will be merged according to itsas_string()method. If any other Python object is passed, it will be wrapped in aLiteralobject and so escaped according to SQL rules.Example:
>>> print(sql.SQL("SELECT * FROM {} WHERE {} = %s") ... .format(sql.Identifier('people'), sql.Identifier('id')) ... .as_string(conn)) SELECT * FROM "people" WHERE "id" = %s >>> print(sql.SQL("SELECT * FROM {tbl} WHERE name = {name}") ... .format(tbl=sql.Identifier('people'), name="O'Rourke")) ... .as_string(conn)) SELECT * FROM "people" WHERE name = 'O''Rourke'
- join(seq: Iterable[Template]) Template#
- join(seq: Iterable[Any]) Composed
Join a sequence of
Composable.- Parameters:
seq – the elements to join.
Use the
SQLobject’s string to separate the elements inseq. Elements that are notComposablewill be consideredLiteral.If the arguments are
Templateinstance, return aTemplatejoining all the items. Note that arguments must either be all templates or none should be.Note that
Composedobjects are iterable too, so they can be used as argument for this method.Example:
>>> snip = sql.SQL(', ').join( ... sql.Identifier(n) for n in ['foo', 'bar', 'baz']) >>> print(snip.as_string(conn)) "foo", "bar", "baz"
Changed in version 3.3: Added support for
Templatesequences. See nested template strings.
- class psycopg.sql.Identifier(*strings: str)#
A
Composablerepresenting an SQL identifier or a dot-separated sequence.Identifiers usually represent names of database objects, such as tables or fields. PostgreSQL identifiers follow different rules than SQL string literals for escaping (e.g. they use double quotes instead of single).
Example:
>>> t1 = sql.Identifier("foo") >>> t2 = sql.Identifier("ba'r") >>> t3 = sql.Identifier('ba"z') >>> print(sql.SQL(', ').join([t1, t2, t3]).as_string(conn)) "foo", "ba'r", "ba""z"
Multiple strings can be passed to the object to represent a qualified name, i.e. a dot-separated sequence of identifiers.
Example:
>>> query = sql.SQL("SELECT {} FROM {}").format( ... sql.Identifier("table", "field"), ... sql.Identifier("schema", "table")) >>> print(query.as_string(conn)) SELECT "table"."field" FROM "schema"."table"
- class psycopg.sql.Literal(obj: Any)#
A
Composablerepresenting an SQL value to include in a query.Usually you will want to include placeholders in the query and pass values as
execute()arguments. If however you really really need to include a literal value in the query you can use this object.The string returned by
as_string()follows the normal adaptation rules for Python objects.Example:
>>> s1 = sql.Literal("fo'o") >>> s2 = sql.Literal(42) >>> s3 = sql.Literal(date(2000, 1, 1)) >>> print(sql.SQL(', ').join([s1, s2, s3]).as_string(conn)) 'fo''o', 42, '2000-01-01'::date
Changed in version 3.1: Add a type cast to the representation if useful in ambiguous context (e.g.
'2000-01-01'::date)
- class psycopg.sql.Placeholder(name: str = '', format: str | psycopg.adapt.PyFormat = PyFormat.AUTO)#
A
Composablerepresenting a placeholder for query parameters.If the name is specified, generate a named placeholder (e.g.
%(name)s,%(name)b), otherwise generate a positional placeholder (e.g.%s,%b).The object is useful to generate SQL queries with a variable number of arguments.
Examples:
>>> names = ['foo', 'bar', 'baz'] >>> q1 = sql.SQL("INSERT INTO my_table ({}) VALUES ({})").format( ... sql.SQL(', ').join(map(sql.Identifier, names)), ... sql.SQL(', ').join(sql.Placeholder() * len(names))) >>> print(q1.as_string(conn)) INSERT INTO my_table ("foo", "bar", "baz") VALUES (%s, %s, %s) >>> q2 = sql.SQL("INSERT INTO my_table ({}) VALUES ({})").format( ... sql.SQL(', ').join(map(sql.Identifier, names)), ... sql.SQL(', ').join(map(sql.Placeholder, names))) >>> print(q2.as_string(conn)) INSERT INTO my_table ("foo", "bar", "baz") VALUES (%(foo)s, %(bar)s, %(baz)s)
- class psycopg.sql.Composed(seq: Sequence[Any])#
A
Composableobject made of a sequence ofComposable.The object is usually created using
Composableoperators and methods (such as theSQL.format()method).Composedobjects can be passed directly toexecute(),executemany(),copy()in place of the query string.It is also possible to create a
Composeddirectly specifying a sequence of objects as arguments: if they are notComposablethey will be wrapped in aLiteral.Example:
>>> comp = sql.Composed( ... [sql.SQL("INSERT INTO "), sql.Identifier("table")]) >>> print(comp.as_string(conn)) INSERT INTO "table"
Composedobjects are iterable (so they can be used inSQL.joinfor instance).- join(joiner: Union[SQL, LiteralString]) Composed#
Return a new
Composedinterposing thejoinerwith theComposeditems.The
joinermust be aSQLor a string which will be interpreted as anSQL.Example:
>>> fields = sql.Identifier('foo') + sql.Identifier('bar') # a Composed >>> print(fields.join(', ').as_string(conn)) "foo", "bar"
Utility functions#
- psycopg.sql.as_string(obj: Any, context: Optional[AdaptContext] = None) str#
Convert an object to a string according to SQL rules.
- Parameters:
obj – the object to convert
context (
AdaptContext|None) – the context in which to convert the object
Adaptation happens according to the type of
obj:Composableobjects are converted according to theiras_string()method;Templatestrings are converted according to the rules documented in Template string queries;every other object is converted as it was a parameter passed to a query.
If
contextis specified then it is be used to customize the conversion. for example using the encoding of a connection or the dumpers registered.New in version 3.3.
- psycopg.sql.as_bytes(obj: Any, context: Optional[AdaptContext] = None) bytes#
Convert an object to a bytes string according to SQL rules.
- Parameters:
obj – the object to convert
context (
AdaptContext|None) – the context in which to convert the object
See
as_string()for details.New in version 3.3.
- psycopg.sql.quote(obj: Any, context: Optional[AdaptContext] = None) str#
Adapt a Python object to a quoted SQL string.
Use this function only if you absolutely want to convert a Python string to an SQL quoted literal to use e.g. to generate batch SQL and you won’t have a connection available when you will need to use it.
This function is relatively inefficient, because it doesn’t cache the adaptation rules. If you pass a
contextyou can adapt the adaptation rules used, otherwise only global rules are used.