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