Package: lesscode-database
lesscode_database数据库连接工具包示例代码importasynciofromsqlalchemyimportselect,MetaData,Table,Column,VARCHAR,INTEGERfromlesscode_database.connection_infoimportConnectionInfofromlesscode_database.db_optionsimportdb_optionsfromlesscode_database.ds_helperimportDsHelperdb_options.conn_list=[ConnectionInfo(dialect="mysql",name="mysql",host="127.0.0.1",port=3306,user="root",password="root",db_name="test",enable=False,params={"pool_recycle":3600},async_enable=True),ConnectionInfo(dialect="doris",name="doris",host="127.0.0.1",port=9030,user="root",password="root",db_name="test",enable=False,params={"pool_recycle":3600},async_enable=True),ConnectionInfo(dialect="ocean_base",name="ocean_base",host="127.0.0.1",port=2883,user="root",password="root",db_name="test",enable=False,params={"pool_recycle":3600},async_enable=True),ConnectionInfo(dialect="tidb",name="tidb",host="127.0.0.1",port=4000,user="root",password="root",db_name="test",enable=False,params={"pool_recycle":3600},async_enable=True),ConnectionInfo(dialect="mssql",name="mssql",host="127.0.0.1",port=1433,user="root",password="root",db_name="test",enable=False,async_enable=False),ConnectionInfo(dialect="oracle",name="oracle",host="127.0.0.1",port=1521,user="root",password="root",db_name="test",enable=False,async_enable=False),ConnectionInfo(dialect="sqlite3",name="sqlite3",dsn="/test/test.db",async_enable=False),ConnectionInfo(dialect="elasticsearch",name="es",host="127.0.0.1",port=9210,user="root",password="root",enable=False,async_enable=True),ConnectionInfo(dialect="esapi",name="esapi",host="127.0.0.1",port=9210,user="root",password="root",enable=True,async_enable=True),ConnectionInfo(dialect="mongo",name="mongo",host="127.0.0.1",port=27027,user="root",password="root",enable=False,async_enable=True),ConnectionInfo(dialect="nebula",name="nebula",host="127.0.0.1",port=9669,user="root",password="nebula",db_name="nebula",enable=False),ConnectionInfo(dialect="postgresql",host="127.0.0.1",port=5454,user="root",password="root",db_name="root",enable=False,async_enable=True),ConnectionInfo(dialect="redis",name="redis",host="127.0.0.1",port=6379,user=None,password=None,db_name=1,enable=False,async_enable=True),# sqlalchemy 异步支持mysql，postgresql，tidb，ocean_base，doris;# 同步支持mysql，postgresql，tidb，ocean_base，doris，sqlite3，mssql，oracleConnectionInfo(dialect="sqlalchemy",name="sqlalchemy",host="127.0.0.1",port=3306,user="root",password="root",db_name="test",enable=False,async_enable=False,params={"db_type":"mysql"}),ConnectionInfo(dialect="neo4j",name="neo4j",host="127.0.0.1",port=7687,user="neo4j",password="neo4j",db_name=None,enable=False,async_enable=True),ConnectionInfo(dialect="clickhouse",name="clickhouse",dsn="clickhouse://localhost",host="127.0.0.1",port=9000,user="default",password="",db_name='',enable=True,async_enable=False),]# mysql 同步测试，async_enable=FalsewithDsHelper("mysql").pool.dedicated_connection()asconn:conn.ping(reconnect=True)withconn.cursor()ascursor:cursor.execute("select 1")description=cursor.descriptionrs=cursor.fetchone()print(rs)# mysql 异步测试，async_enable=Trueasyncdefasync_mysql_test():asyncwithDsHelper("mysql").pool.acquire()asconn:asyncwithconn.cursor()ascursor:awaitcursor.execute("select 1")rs=awaitcursor.fetchone()print(rs)loop=asyncio.get_event_loop()loop.run_until_complete(async_mysql_test())# doris 同步测试，async_enable=FalsewithDsHelper("doris").pool.dedicated_connection()asconn:conn.ping(reconnect=True)withconn.cursor()ascursor:cursor.execute("select 1")description=cursor.descriptionrs=cursor.fetchone()print(rs)# doris 异步测试，async_enable=Trueasyncdefasync_doris_test():asyncwithDsHelper("doris").pool.acquire()asconn:asyncwithconn.cursor()ascursor:awaitcursor.execute("select 1")rs=awaitcursor.fetchone()print(rs)loop=asyncio.get_event_loop()loop.run_until_complete(async_doris_test())# ocean_base 同步测试，async_enable=FalsewithDsHelper("ocean_base").pool.dedicated_connection()asconn:conn.ping(reconnect=True)withconn.cursor()ascursor:cursor.execute("select 1")description=cursor.descriptionrs=cursor.fetchone()print(rs)# ocean_base 异步测试，async_enable=Trueasyncdefasync_ocean_base_test():asyncwithDsHelper("ocean_base").pool.acquire()asconn:asyncwithconn.cursor()ascursor:awaitcursor.execute("select 1")rs=awaitcursor.fetchone()print(rs)loop=asyncio.get_event_loop()loop.run_until_complete(async_ocean_base_test())# tidb 同步测试，async_enable=FalsewithDsHelper("tidb").pool.dedicated_connection()asconn:conn.ping(reconnect=True)withconn.cursor()ascursor:cursor.execute("select 1")description=cursor.descriptionrs=cursor.fetchone()print(rs)# tidb 异步测试，async_enable=Trueasyncdefasync_tidb_test():asyncwithDsHelper("tidb").pool.acquire()asconn:asyncwithconn.cursor()ascursor:awaitcursor.execute("select 1")rs=awaitcursor.fetchone()print(rs)loop=asyncio.get_event_loop()loop.run_until_complete(async_tidb_test())# sqlite3 异步测试asyncdefasync_sqlite3_test():cur=awaitDsHelper("sqlite3").pool.cursor()awaitcur.execute("SELECT 1")row=awaitcur.fetchone()print(row)loop=asyncio.get_event_loop()loop.run_until_complete(async_sqlite3_test())# sqlite3 同步测试cursor=DsHelper("sqlite3").pool.cursor()cursor.execute('select 1')row=cursor.fetchone()print(row)# es同步测试，async_enable=Falsebody={"query":{"bool":{"must":[]}},"size":1}resp=DsHelper("es").pool.search(index="test",body=body)print(resp)# es异步测试，async_enable=Trueasyncdefasync_es_test():resp=awaitDsHelper("es").pool.search(index="test",body={"query":{"match_all":{}}},size=1,)print(resp)loop=asyncio.get_event_loop()loop.run_until_complete(async_es_test())# sql server同步测试cursor=DsHelper("mssql").pool.cursor()cursor.execute('SELECT 1')row=cursor.fetchone()print(row)# sql server暂不支持异步测试# oracle 同步测试cursor=DsHelper("oracle").pool.cursor()cursor.execute('select 1')row=cursor.fetchone()print(row)# oracle暂不支持异步测试# mongo同步测试，async_enable=Falseprint(DsHelper("mongo").pool.test.test.find_one())# mongo异步测试，async_enable=Trueasyncdefasync_mongo_test():resp=awaitDsHelper("mongo").pool.test.test.find_one()print(resp)loop=asyncio.get_event_loop()loop.run_until_complete(async_mongo_test())# nebula同步测试，async_enable=FalsewithDsHelper("nebula").make_nebula_session()assession:session.execute(f'USE core')result=session.execute("match (c:Company) return c limit 1")print(result)print(DsHelper("nebula").exec_nebula_gql("match (c:Company) return c limit 1","core"))# nebula暂不支持异步# postgresql同步测试，async_enable=FalsewithDsHelper("postgresql").pool.connection()asconn:withconn.cursor()ascursor:cursor.execute("select 1")rs=cursor.fetchone()print(rs)# postgresql异步测试，async_enable=Trueasyncdefasync_pg_test():asyncwithDsHelper("postgresql").pool.acquire()asconn:asyncwithconn.cursor()ascur:awaitcur.execute("select 1")rs=awaitcur.fetchone()print(rs)loop=asyncio.get_event_loop()loop.run_until_complete(async_pg_test())# redis 同步测试，async_enable=Falseprint(DsHelper("redis").pool.exists("test"))# redis 异步测试，async_enable=Trueasyncdefasync_redis_test():rs=awaitDsHelper("redis").pool.exists("test")print(rs)loop=asyncio.get_event_loop()loop.run_until_complete(async_redis_test())# sqlalchemy tablemeta=MetaData()t1=Table("test_user",meta,Column("name",VARCHAR(collation='utf8mb3_bin',length=255)),Column(name='id',comment=None,nullable=False,autoincrement=False,primary_key=True,type_=INTEGER(),server_default=None))# sqlalchemy同步测试，async_enable=FalsewithDsHelper("sqlalchemy").make_sqlalchemy_session()assession:cur=session.execute(select(t1))print(cur.fetchall())# sqlalchemy异步测试，async_enable=Trueasyncdefasync_sqlalchemy_test():asyncwithDsHelper("sqlalchemy").async_make_sqlalchemy_session()assession:cur=awaitsession.execute(select(t1))print(cur.fetchall())loop=asyncio.get_event_loop()loop.run_until_complete(async_sqlalchemy_test())# neo4j同步测试defquery(tx):nql="match (p:Person) return p"forrecordintx.run(nql):print(record)withDsHelper("neo4j").make_neo4j_session()assession:session.execute_read(query)# neo4j异步测试asyncdefquery(tx):nql="match (p:Person) return p"res=awaittx.run(nql)asyncforrecordinres:print(record)asyncdefasync_neo4j_test():asyncwithDsHelper("neo4j").async_make_neo4j_session()assession:awaitsession.execute_read(query)loop=asyncio.get_event_loop()loop.run_until_complete(async_neo4j_test())# clickhouse 同步测试withDsHelper("clickhouse").pool.dedicated_connection()asconn:withconn.cursor()ascursor:cursor.execute('SELECT 1')print(cursor.fetchall())# clickhouse 异步测试测试asyncdefasync_clickhouse_test():asyncwithDsHelper("clickhouse").pool.acquire()asconn:asyncwithconn.cursor()ascursor:awaitcursor.execute("SELECT 1")ret=awaitcursor.fetchone()print(ret)loop=asyncio.get_event_loop()loop.run_until_complete(async_clickhouse_test())# esapi测试res=DsHelper("esapi").esapi("POST","/core.company_lite/_search",json={"query":{"bool":{"must":[]}},"size":1})print(res)# esapi异步测试asyncdefasync_esapi_test():res=awaitDsHelper("esapi").async_esapi("POST","/core.company_lite/_search",json={"query":{"bool":{"must":[]}},"size":1})print(res)loop=asyncio.get_event_loop()loop.run_until_complete(async_esapi_test())