The Utilities Module¶
List To SQL String¶
-
list_to_sql_string(lst)¶ turns python lists into a string that can be put into a postgres tuple and serve as a list in postgres
- Parameters
lst (
Tuple[str]) – a list of strings like['pg_catalog','information_schema']- Return type
str- Returns
a string of items in SQL format
"'pg_catalog','information_schema','hi'"
Make Qualified Schema Name¶
-
make_qualified_schema_name(schema_name, table_name)¶ produces a qualified schema name given a schema and table
- Parameters
schema_name (
str) – the schema nametable_name (
str) – the table name
- Return type
str- Returns
a quoted dot separated qualified schema name
Check Nth Arg¶
-
check_nth_arg_is_of_type(n, type_to_check)¶ this decorator allows us to do some basic type checking
- Parameters
n (
int) – the “nth” argument of the decorated functiontype_to_check (
type) – the type to ensure that the “nth” value is of
- Raises
Create PLPython Triggers¶
-
create_plpython_triggers(plpy_wrapper, schema, table_name, trigger_template_path=PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/plpy-wrapper/envs/latest/lib/python3.7/site-packages/plpy_wrapper/trigger_process_template.txt'), trigger_func_definition='', trigger_func_name='')¶ sets up triggers and the trigger function for a table. After running this function, triggers will now be routed to the custom function where you can run arbitrary python inside of the
plpython.trigger.Triggermethods. Should be executed from the DB like so:do $$ import plpy_wrapper wrapper = plpy_wrapper.PLPYWrapper(globals()) plpy_wrapper.utilities.create_plpython_triggers(wrapper,schema,table_name) $$ language plpython3u;
- Parameters
trigger_func_name (
Optional[str]) – if you provided atrigger_func_definitionand your function name is different than the default function name generated by this function, provide the value for the function nametrigger_func_definition (
Optional[str]) – if provided, the value fromtrigger_template_pathis ignored. Thetrigger_func_definitionis sql that will be run before the CREATE trigger statementstrigger_template_path (
Union[str,Path,None]) – the path of the template that will be the base for trigger procedures. The file text must contain the following keyword parameters:schema,table,func_nameotherwise unexpected behavior can be expectedplpy_wrapper (
PLPYWrapper) – instance ofplpy_wrapper.plpy_wrapper.PLPYWrapperschema (
str) – the schema name the table is located intable_name (
str) – the table to add the triggers and function to
Get All Tables¶
-
get_all_tables(plpy_wrapper, exclude_schemas=, exclude_tables=)¶ Queries postgres for all tables
- Parameters
plpy_wrapper (
PLPYWrapper) – PLPYWrapper instanceexclude_schemas (
Tuple[str]) – schemas to exclude from the queryexclude_tables (
Tuple[str]) – tables to exclude from the query
- Return type
- Returns
Execute Per Table¶
-
execute_per_table(plpy_wrapper, execute_definition, exclude_schemas='pg_catalog', 'information_schema', exclude_tables=)¶ Run an SQL command per table. The execute_definition string must contain either
{table}and/or{schema_qualified_table_name}as keyword parameters- Parameters
plpy_wrapper (
PLPYWrapper) – PLPYWrapper instanceexecute_definition (
str) – the SQL to run per tableexclude_schemas (
Tuple[str]) – schemas to exclude from the executionexclude_tables (
Tuple[str]) – tables to exclude from the execution
- Return type
Dict[str,ResultSet]- Returns
a dictionary with the table’s qualified schema name as the key and the ResultSet as the value