Example #1
0
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()}
Example #2
0
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]
Example #3
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]
Example #4
0
 def cb(cursor: psycopg2._psycopg.cursor) -> None:
     cursor.execute(q_upsert, q_upsert_values)
Example #5
0
 def query_callback(cursor: psycopg2._psycopg.cursor) -> None:
     cursor.execute(sql_query, sql_values)
Example #6
0
 def cb(cursor: psycopg2._psycopg.cursor) -> None:
     cursor.execute(q_select, q_select_values)
Example #7
0
 def cb(cursor: psycopg2._psycopg.cursor) -> None:
     cursor.execute(q, {'id_val': str(uuid)})
Example #8
0
 def cb(cursor: psycopg2._psycopg.cursor) -> None:
     cursor.execute(q)
Example #9
0
 def cb(cursor: psycopg2._psycopg.cursor) -> None:
     cursor.execute(count_query)