swh.core.db.db_utils module¶
-
swh.core.db.db_utils.
stored_procedure
(stored_proc)[source]¶ decorator to execute remote stored procedure, specified as argument
Generally, the body of the decorated function should be empty. If it is not, the stored procedure will be executed first; the function body then.
-
swh.core.db.db_utils.
connect_to_conninfo
(db_or_conninfo: Union[str, psycopg2.extensions.connection]) → psycopg2.extensions.connection[source]¶ Connect to the database passed in argument
- Parameters
db_or_conninfo – A database connection, or a database connection info string
- Returns
a connected database handle
- Raises
psycopg2.Error if the database doesn't exist –
-
swh.core.db.db_utils.
swh_db_version
(db_or_conninfo: Union[str, psycopg2.extensions.connection]) → Optional[int][source]¶ Retrieve the swh version of the database.
If the database is not initialized, this logs a warning and returns None.
- Parameters
db_or_conninfo – A database connection, or a database connection info string
- Returns
Either the version of the database, or None if it couldn’t be detected
-
swh.core.db.db_utils.
swh_db_flavor
(db_or_conninfo: Union[str, psycopg2.extensions.connection]) → Optional[str][source]¶ Retrieve the swh flavor of the database.
If the database is not initialized, or the database doesn’t support flavors, this returns None.
- Parameters
db_or_conninfo – A database connection, or a database connection info string
- Returns
The flavor of the database, or None if it could not be detected.
-
swh.core.db.db_utils.
execute_values_generator
(cur, sql, argslist, template=None, page_size=100)[source]¶ Execute a statement using SQL
VALUES
with a sequence of parameters. Rows returned by the query are returned through a generator. You need to consume the generator for the queries to be executed!- Parameters
cur – the cursor to use to execute the query.
sql – the query to execute. It must contain a single
%s
placeholder, which will be replaced by a VALUES list. Example:"INSERT INTO mytable (id, f1, f2) VALUES %s"
.argslist – sequence of sequences or dictionaries with the arguments to send to the query. The type and content must be consistent with template.
template –
the snippet to merge to every item in argslist to compose the query.
If the argslist items are sequences it should contain positional placeholders (e.g.
"(%s, %s, %s)"
, or"(%s, %s, 42)
” if there are constants value…).If the argslist items are mappings it should contain named placeholders (e.g.
"(%(id)s, %(f1)s, 42)"
).
If not specified, assume the arguments are sequence and use a simple positional template (i.e.
(%s, %s, ...)
), with the number of placeholders sniffed by the first element in argslist.page_size – maximum number of argslist items to include in every statement. If there are more items the function will execute more than one statement.
yield_from_cur – Whether to yield results from the cursor in this function directly.
After the execution of the function the cursor.rowcount property will not contain a total result.