Pyesql¶
Pyesql is a Python clone of Yesql (https://github.com/krisajenkins/yesql)
Source: https://github.com/KMahoney/pyesql
Differences from Yesql:
- Python does not distinguish between a statement and a query, so there is no need for the ! suffix.
- Instead of the ? and :param syntax, Pyesql uses the %(param)s syntax
-
pyesql.parse_file(filename, name=None)¶ Parse a SQL source file into a python object.
Reads filename in to memory and returns a new class named after the filename. See
pyesql.parse_source()for details on the returned class.Parameters: - filename (str) – Source SQL filename
- name (str) – Optional name of the returned type. Uses filename by default.
Returns: A new Python type
Raises ParseError: If the Source SQL does not conform
-
pyesql.parse_source(name, source)¶ Parse a SQL source string into a python object.
Takes a source SQL string and parses it into a new class named name. The class is initialised with a database connection and has a method for each SQL statement.
Each SQL statement should take the form:
-- name: <name> -- optional documentation BODY %(parameter)s
For example given the SQL source:
-- name: test1 -- documentation test1 SELECT * FROM test -- name: test2 SELECT * FROM test WHERE x = %(y)s
we can produce this object:
Example = parse_object('Example', source) connection = MySQLdb.connect(...) example = Example(connection) example.test1() example.test2(y=1)
Parameters: - name (str) – Name of the returned type
- source (str) – Source SQL
Returns: A new Python type
Raises ParseError: If the Source SQL does not conform
-
exception
pyesql.ParseError¶ An error was encountered in the SQL source