MCPcopy Index your code
hub / github.com/dataease/SQLBot / get_schema

Function get_schema

backend/apps/db/db.py:341–389  ·  view source on GitHub ↗
(ds: CoreDatasource)

Source from the content-addressed store, hash-verified

339
340
341def get_schema(ds: CoreDatasource):
342 conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
343 db = DB.get_db(ds.type)
344 if db.connect_type == ConnectType.sqlalchemy:
345 with get_session(ds) as session:
346 sql: str = ''
347 if equals_ignore_case(ds.type, "sqlServer"):
348 sql = """select name
349 from sys.schemas"""
350 elif equals_ignore_case(ds.type, "pg", "excel"):
351 sql = """SELECT nspname
352 FROM pg_namespace"""
353 elif equals_ignore_case(ds.type, "oracle"):
354 sql = """select *
355 from all_users"""
356 with session.execute(text(sql)) as result:
357 res = result.fetchall()
358 res_list = [item[0] for item in res]
359 return res_list
360 else:
361 extra_config_dict = get_extra_config(conf)
362 if equals_ignore_case(ds.type, 'dm'):
363 with dmPython.connect(user=conf.username, password=conf.password, server=conf.host,
364 port=conf.port, **extra_config_dict) as conn, conn.cursor() as cursor:
365 cursor.execute("""select OBJECT_NAME
366 from all_objects
367 where object_type = 'SCH'""", timeout=conf.timeout)
368 res = cursor.fetchall()
369 res_list = [item[0] for item in res]
370 return res_list
371 elif equals_ignore_case(ds.type, 'redshift'):
372 with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
373 password=conf.password,
374 timeout=conf.timeout, **extra_config_dict) as conn, conn.cursor() as cursor:
375 cursor.execute("""SELECT nspname
376 FROM pg_namespace""")
377 res = cursor.fetchall()
378 res_list = [item[0] for item in res]
379 return res_list
380 elif equals_ignore_case(ds.type, 'kingbase'):
381 with psycopg2.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
382 password=conf.password,
383 options=f"-c statement_timeout={conf.timeout * 1000}",
384 **extra_config_dict) as conn, conn.cursor() as cursor:
385 cursor.execute("""SELECT nspname
386 FROM pg_namespace""")
387 res = cursor.fetchall()
388 res_list = [item[0] for item in res]
389 return res_list
390
391
392def get_tables(ds: CoreDatasource):

Callers 1

innerFunction · 0.90

Calls 7

DatasourceConfClass · 0.90
aes_decryptFunction · 0.90
get_engine_configFunction · 0.90
equals_ignore_caseFunction · 0.90
get_extra_configFunction · 0.85
get_dbMethod · 0.80
get_sessionFunction · 0.70

Tested by

no test coverage detected