def database_table_fields(cursor: psycopg2._psycopg.cursor, table: str) -> Dict[str, str]: """ field names and data types of the given table, within the given PostGreSQL database :param cursor: psycopg2 cursor :param table: name of table :return: mapping of column names to the PostGres data type """ cursor.execute( f'SELECT column_name, udt_name FROM information_schema.columns WHERE table_name=%s;', [table], ) return {record[0]: record[1] for record in cursor.fetchall()}
def database_table_is_inherited(cursor: psycopg2._psycopg.cursor, table: str) -> bool: """ whether the given PostGreSQL table is inherited :param cursor: psycopg2 cursor :param table: name of table :return: whether table is inherited """ cursor.execute( f'SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_inherits WHERE inhrelid=%s::regclass);', [f'public.{table}'], ) return cursor.fetchone()[0]
def database_has_table(cursor: psycopg2._psycopg.cursor, table: str) -> bool: """ whether the given table exists within the given PostGreSQL database :param cursor: psycopg2 cursor :param table: name of table :return: whether table exists """ cursor.execute( # f'SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name=%s);', f'SELECT EXISTS(SELECT 1 FROM pg_class WHERE relname=%s);', [table.lower()], ) return cursor.fetchone()[0]
def cb(cursor: psycopg2._psycopg.cursor) -> None: cursor.execute(q_upsert, q_upsert_values)
def query_callback(cursor: psycopg2._psycopg.cursor) -> None: cursor.execute(sql_query, sql_values)
def cb(cursor: psycopg2._psycopg.cursor) -> None: cursor.execute(q_select, q_select_values)
def cb(cursor: psycopg2._psycopg.cursor) -> None: cursor.execute(q, {'id_val': str(uuid)})
def cb(cursor: psycopg2._psycopg.cursor) -> None: cursor.execute(q)
def cb(cursor: psycopg2._psycopg.cursor) -> None: cursor.execute(count_query)