def test_tables_equal_by_schema_and_name_using_schema_with_parent(self): parent = Schema("parent") a = Schema("a", parent=parent) t1 = Table("t", schema=a) t2 = Table("t", schema=a) self.assertEqual(t1, t2)
def test_tables_not_equal_by_schema_and_name_using_schema_with_different_parents( self, ): parent = Schema("parent") a = Schema("a", parent=parent) t1 = Table("t", schema=a) t2 = Table("t", schema=Schema("a")) self.assertNotEqual(t1, t2)
def main(): conn = setup_connection() cursor = conn.cursor() tbls = Schema('information_schema').tables cursor.execute( Query.from_(tbls).where(tbls.table_type == 'BASE TABLE').where( tbls.table_schema == 'public').select(tbls.table_name).get_sql(), ) rows = cursor.fetchall() rows = map(lambda r: r[0], rows) cursor.close() conn.close() cfg = settings.load_settings() db_host = cfg['DATABASE_HOST'] db_port = int(cfg['DATABASE_PORT']) db_user = cfg['DATABASE_USER'] db_pass = cfg['DATABASE_PASSWORD'] db_name = cfg['DATABASE_DBNAME'] old_pg_pass = os.environ.get('PGPASSWORD') os.environ['PGPASSWORD'] = db_pass for tbl in rows: print('=' * 50) print(f'DESCRIBE {tbl}') os.system(f'psql -d {db_name} -h {db_host} -p {db_port} ' f'-U {db_user} -c "\\d+ {tbl}"') print() if old_pg_pass is not None: os.environ['PGPASSWORD'] = old_pg_pass else: del os.environ['PGPASSWORD']
def build_table_by_topic_id(topic_id) -> Table: topic = get_topic_by_id(topic_id) topic_col_name = build_collection_name(topic.name) datasource: DataSource = load_data_source_by_id(topic.dataSourceId) catalog_name = datasource.dataSourceCode schema_name = datasource.name schema = Schema(schema_name, LiteralValue(catalog_name)) return Table(topic_col_name, schema)
def check_if_column_exist(cursor, tblname, colname): columns = Schema('information_schema').columns cursor.execute( Query.from_(columns).where( columns.table_name == Parameter('%s')).where( columns.column_name == Parameter('%s')).select(1).limit( 1).get_sql(), (tblname, colname)) result = cursor.fetchone() return result is not None
def check_if_table_exist(cursor, tblname): """Returns true if the given table exists and false otherwise""" info_schema = Schema('information_schema').tables cursor.execute( Query.from_(info_schema).where( info_schema.table_type == 'BASE TABLE').where( info_schema.table_schema == 'public').where( info_schema.table_name == Parameter('%s')).select(1).limit( 1).get_sql(), (tblname, )) result = cursor.fetchone() return result is not None
def drop_all_tables(conn): """Drop all the tables in the default database""" cursor = conn.cursor() info_schema = Schema('information_schema').tables cursor.execute( Query.from_(info_schema).where( info_schema.table_type == 'BASE TABLE').where( info_schema.table_schema == 'public').select( info_schema.table_name).get_sql()) table_names = cursor.fetchall() for (tbl_name, ) in table_names: cursor.execute(f'DROP TABLE {tbl_name} CASCADE') conn.commit() cursor.close()
def test_schema_included_in_function_sql(self): a = Schema('a') func = fn.Function('my_proc', 1, 2, 3, schema=a) self.assertEqual('"a".my_proc(1,2,3)', func.get_sql(quote_char='"'))
def test_table_with_schema_and_schema_parent_arg(self): table = Table("test_table", schema=Schema("x_schema", parent=Database("x_db"))) self.assertEqual('"x_db"."x_schema"."test_table"', str(table))
def test_table_with_schema_arg(self): table = Table("test_table", schema=Schema("x_schema")) self.assertEqual('"x_schema"."test_table"', str(table))
def test_schema_table_attr(self): table = Schema("x_schema").test_table self.assertEqual('"x_schema"."test_table"', str(table))
def test_tables_equal_by_schema_and_name_using_schema(self): a = Schema("a") t1 = Table("t", schema=a) t2 = Table("t", schema=a) self.assertEqual(t1, t2)
from pypika import Schema public = Schema('public') users = public.users sessions = public.sessions products = public.products currencies = public.currencies orders = public.orders order_product_link = public.order_product_link