lesscode-database 0.0.5__tar.gz → 0.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of lesscode-database might be problematic. Click here for more details.

Files changed (18) hide show
  1. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/PKG-INFO +37 -40
  2. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/README.md +36 -39
  3. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database/db_options.py +1 -1
  4. lesscode_database-0.0.6/lesscode_database/ds_helper.py +98 -0
  5. lesscode_database-0.0.6/lesscode_database/version.py +1 -0
  6. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database.egg-info/PKG-INFO +37 -40
  7. lesscode_database-0.0.5/lesscode_database/ds_helper.py +0 -36
  8. lesscode_database-0.0.5/lesscode_database/version.py +0 -1
  9. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/LICENSE +0 -0
  10. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database/__init__.py +0 -0
  11. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database/connect_pool.py +0 -0
  12. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database/connection_info.py +0 -0
  13. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database/dynamic_import_package.py +0 -0
  14. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database.egg-info/SOURCES.txt +0 -0
  15. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database.egg-info/dependency_links.txt +0 -0
  16. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/lesscode_database.egg-info/top_level.txt +0 -0
  17. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/setup.cfg +0 -0
  18. {lesscode_database-0.0.5 → lesscode_database-0.0.6}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lesscode_database
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: lesscode_database是数据库连接工具包
5
5
  Author: navysummer
6
6
  Author-email: navysummer@yeah.net
@@ -20,12 +20,11 @@ License-File: LICENSE
20
20
  ```python
21
21
  import asyncio
22
22
 
23
- from sqlalchemy import select, Column, VARCHAR, INTEGER, Table, MetaData
24
- from sqlalchemy.ext.asyncio import async_sessionmaker
25
- from sqlalchemy.orm import sessionmaker
23
+ from sqlalchemy import select, MetaData, Table, Column, VARCHAR, INTEGER
26
24
 
27
25
  from lesscode_database.connection_info import ConnectionInfo
28
26
  from lesscode_database.db_options import db_options
27
+ from lesscode_database.ds_helper import DsHelper
29
28
 
30
29
  db_options.conn_list = [
31
30
  ConnectionInfo(dialect="mysql", name="mysql", host="127.0.0.1", port=3306, user="root",
@@ -66,8 +65,9 @@ db_options.conn_list = [
66
65
 
67
66
  ]
68
67
 
68
+
69
69
  # mysql 同步测试,async_enable=False
70
- with db_options.mysql.dedicated_connection() as conn:
70
+ with DsHelper("mysql").pool.dedicated_connection() as conn:
71
71
  conn.ping(reconnect=True)
72
72
  with conn.cursor() as cursor:
73
73
  cursor.execute("select 1")
@@ -78,7 +78,7 @@ with db_options.mysql.dedicated_connection() as conn:
78
78
 
79
79
  # mysql 异步测试,async_enable=True
80
80
  async def async_mysql_test():
81
- async with db_options.mysql.acquire() as conn:
81
+ async with DsHelper("mysql").pool.acquire() as conn:
82
82
  async with conn.cursor() as cursor:
83
83
  await cursor.execute("select 1")
84
84
  rs = await cursor.fetchone()
@@ -89,7 +89,7 @@ loop = asyncio.get_event_loop()
89
89
  loop.run_until_complete(async_mysql_test())
90
90
 
91
91
  # doris 同步测试,async_enable=False
92
- with db_options.doris.dedicated_connection() as conn:
92
+ with DsHelper("doris").pool.dedicated_connection() as conn:
93
93
  conn.ping(reconnect=True)
94
94
  with conn.cursor() as cursor:
95
95
  cursor.execute("select 1")
@@ -100,7 +100,7 @@ with db_options.doris.dedicated_connection() as conn:
100
100
 
101
101
  # doris 异步测试,async_enable=True
102
102
  async def async_doris_test():
103
- async with db_options.doris.acquire() as conn:
103
+ async with DsHelper("doris").pool.acquire() as conn:
104
104
  async with conn.cursor() as cursor:
105
105
  await cursor.execute("select 1")
106
106
  rs = await cursor.fetchone()
@@ -111,7 +111,7 @@ loop = asyncio.get_event_loop()
111
111
  loop.run_until_complete(async_doris_test())
112
112
 
113
113
  # ocean_base 同步测试,async_enable=False
114
- with db_options.ocean_base.dedicated_connection() as conn:
114
+ with DsHelper("ocean_base").pool.dedicated_connection() as conn:
115
115
  conn.ping(reconnect=True)
116
116
  with conn.cursor() as cursor:
117
117
  cursor.execute("select 1")
@@ -122,7 +122,7 @@ with db_options.ocean_base.dedicated_connection() as conn:
122
122
 
123
123
  # ocean_base 异步测试,async_enable=True
124
124
  async def async_ocean_base_test():
125
- async with db_options.ocean_base.acquire() as conn:
125
+ async with DsHelper("ocean_base").pool.acquire() as conn:
126
126
  async with conn.cursor() as cursor:
127
127
  await cursor.execute("select 1")
128
128
  rs = await cursor.fetchone()
@@ -133,7 +133,7 @@ loop = asyncio.get_event_loop()
133
133
  loop.run_until_complete(async_ocean_base_test())
134
134
 
135
135
  # tidb 同步测试,async_enable=False
136
- with db_options.tidb.dedicated_connection() as conn:
136
+ with DsHelper("tidb").pool.dedicated_connection() as conn:
137
137
  conn.ping(reconnect=True)
138
138
  with conn.cursor() as cursor:
139
139
  cursor.execute("select 1")
@@ -144,7 +144,7 @@ with db_options.tidb.dedicated_connection() as conn:
144
144
 
145
145
  # tidb 异步测试,async_enable=True
146
146
  async def async_tidb_test():
147
- async with db_options.tidb.acquire() as conn:
147
+ async with DsHelper("tidb").pool.acquire() as conn:
148
148
  async with conn.cursor() as cursor:
149
149
  await cursor.execute("select 1")
150
150
  rs = await cursor.fetchone()
@@ -157,7 +157,7 @@ loop.run_until_complete(async_tidb_test())
157
157
 
158
158
  # sqlite3 异步测试
159
159
  async def async_sqlite3_test():
160
- cur = await db_options.sqlite3.cursor()
160
+ cur = await DsHelper("sqlite3").pool.cursor()
161
161
  await cur.execute("SELECT 1")
162
162
  row = await cur.fetchone()
163
163
  print(row)
@@ -167,7 +167,7 @@ loop = asyncio.get_event_loop()
167
167
  loop.run_until_complete(async_sqlite3_test())
168
168
 
169
169
  # sqlite3 同步测试
170
- cursor = db_options.sqlite3.cursor()
170
+ cursor = DsHelper("sqlite3").pool.cursor()
171
171
  cursor.execute('select 1')
172
172
  row = cursor.fetchone()
173
173
  print(row)
@@ -181,7 +181,7 @@ body = {
181
181
  },
182
182
  "size": 1
183
183
  }
184
- resp = db_options.es.search(
184
+ resp = DsHelper("es").pool.search(
185
185
  index="test",
186
186
  body=body
187
187
  )
@@ -190,7 +190,7 @@ print(resp)
190
190
 
191
191
  # es异步测试,async_enable=True
192
192
  async def async_es_test():
193
- resp = await db_options.es.search(
193
+ resp = await DsHelper("es").pool.search(
194
194
  index="test",
195
195
  body={"query": {"match_all": {}}},
196
196
  size=1,
@@ -202,7 +202,7 @@ loop = asyncio.get_event_loop()
202
202
  loop.run_until_complete(async_es_test())
203
203
 
204
204
  # sql server同步测试
205
- cursor = db_options.mssql.cursor()
205
+ cursor = DsHelper("mssql").pool.cursor()
206
206
  cursor.execute('SELECT 1')
207
207
  row = cursor.fetchone()
208
208
  print(row)
@@ -210,7 +210,7 @@ print(row)
210
210
  # sql server暂不支持异步测试
211
211
 
212
212
  # oracle 同步测试
213
- cursor = db_options.oracle.cursor()
213
+ cursor = DsHelper("oracle").pool.cursor()
214
214
  cursor.execute('select 1')
215
215
  row = cursor.fetchone()
216
216
  print(row)
@@ -219,12 +219,12 @@ print(row)
219
219
 
220
220
 
221
221
  # mongo同步测试,async_enable=False
222
- print(db_options.mongo.test.test.find_one())
222
+ print(DsHelper("mongo").pool.test.test.find_one())
223
223
 
224
224
 
225
225
  # mongo异步测试,async_enable=True
226
226
  async def async_mongo_test():
227
- resp = await db_options.mongo.test.test.find_one()
227
+ resp = await DsHelper("mongo").pool.test.test.find_one()
228
228
  print(resp)
229
229
 
230
230
 
@@ -232,15 +232,16 @@ loop = asyncio.get_event_loop()
232
232
  loop.run_until_complete(async_mongo_test())
233
233
 
234
234
  # nebula同步测试,async_enable=False
235
- with db_options.nebula.session_context("root", "nebula") as session:
236
- session.execute(f'USE test')
237
- result = session.execute("match (t:Test) return t limit 1")
235
+ with DsHelper("nebula").make_nebula_session() as session:
236
+ session.execute(f'USE core')
237
+ result = session.execute("match (c:Company) return c limit 1")
238
238
  print(result)
239
239
 
240
+ print(DsHelper("nebula").exec_nebula_gql("match (c:Company) return c limit 1", "core"))
240
241
  # nebula暂不支持异步
241
242
 
242
243
  # postgresql同步测试,async_enable=False
243
- with db_options.postgresql.connection() as conn:
244
+ with DsHelper("postgresql").pool.connection() as conn:
244
245
  with conn.cursor() as cursor:
245
246
  cursor.execute("select 1")
246
247
  rs = cursor.fetchone()
@@ -249,7 +250,7 @@ with db_options.postgresql.connection() as conn:
249
250
 
250
251
  # postgresql异步测试,async_enable=True
251
252
  async def async_pg_test():
252
- async with db_options.postgresql.acquire() as conn:
253
+ async with DsHelper("postgresql").pool.acquire() as conn:
253
254
  async with conn.cursor() as cur:
254
255
  await cur.execute("select 1")
255
256
  rs = await cur.fetchone()
@@ -260,12 +261,12 @@ loop = asyncio.get_event_loop()
260
261
  loop.run_until_complete(async_pg_test())
261
262
 
262
263
  # redis 同步测试,async_enable=False
263
- print(db_options.redis.exists("test"))
264
+ print(DsHelper("redis").pool.exists("test"))
264
265
 
265
266
 
266
267
  # redis 异步测试,async_enable=True
267
268
  async def async_redis_test():
268
- rs = await db_options.redis.exists("test")
269
+ rs = await DsHelper("redis").pool.exists("test")
269
270
  print(rs)
270
271
 
271
272
 
@@ -279,52 +280,49 @@ t1 = Table("test_user", meta, Column("name", VARCHAR(collation='utf8mb3_bin', le
279
280
  server_default=None))
280
281
 
281
282
  # sqlalchemy同步测试,async_enable=False
282
- with sessionmaker(bind=db_options.sqlalchemy)() as session:
283
+ with DsHelper("sqlalchemy").make_sqlalchemy_session() as session:
283
284
  cur = session.execute(select(t1))
284
285
  print(cur.fetchall())
285
286
 
286
287
 
287
288
  # sqlalchemy异步测试,async_enable=True
288
289
  async def async_sqlalchemy_test():
289
- async with async_sessionmaker(db_options.sqlalchemy)() as session:
290
+ async with DsHelper("sqlalchemy").async_make_sqlalchemy_session() as session:
290
291
  cur = await session.execute(select(t1))
291
-
292
292
  print(cur.fetchall())
293
293
 
294
294
 
295
295
  loop = asyncio.get_event_loop()
296
296
  loop.run_until_complete(async_sqlalchemy_test())
297
297
 
298
-
299
298
  # neo4j同步测试
300
299
  def query(tx):
301
- nql = "MATCH (c:Company) return c limit 1"
300
+ nql = "match (p:Person) return p"
302
301
  for record in tx.run(nql):
303
302
  print(record)
304
303
 
305
304
 
306
- with db_options.neo4j.session(database="invest") as session:
305
+ with DsHelper("neo4j").make_neo4j_session() as session:
307
306
  session.execute_read(query)
308
307
 
309
-
310
308
  # neo4j异步测试
311
309
  async def query(tx):
312
- nql = "MATCH (c:Company) return c limit 1"
310
+ nql = "match (p:Person) return p"
313
311
  res = await tx.run(nql)
314
312
  async for record in res:
315
313
  print(record)
316
314
 
317
-
318
315
  async def async_neo4j_test():
319
- async with db_options.neo4j.session(database="invest") as session:
316
+ async with DsHelper("neo4j").async_make_neo4j_session() as session:
320
317
  await session.execute_read(query)
321
318
 
322
319
 
323
320
  loop = asyncio.get_event_loop()
324
321
  loop.run_until_complete(async_neo4j_test())
325
322
 
323
+
326
324
  # clickhouse 同步测试
327
- with db_options.clickhouse as conn:
325
+ with DsHelper("clickhouse").pool.dedicated_connection() as conn:
328
326
  with conn.cursor() as cursor:
329
327
  cursor.execute('SELECT 1')
330
328
  print(cursor.fetchall())
@@ -332,7 +330,7 @@ with db_options.clickhouse as conn:
332
330
 
333
331
  # clickhouse 异步测试测试
334
332
  async def async_clickhouse_test():
335
- async with db_options.clickhouse.acquire() as conn:
333
+ async with DsHelper("clickhouse").pool.acquire() as conn:
336
334
  async with conn.cursor() as cursor:
337
335
  await cursor.execute("SELECT 1")
338
336
  ret = await cursor.fetchone()
@@ -342,5 +340,4 @@ async def async_clickhouse_test():
342
340
  loop = asyncio.get_event_loop()
343
341
  loop.run_until_complete(async_clickhouse_test())
344
342
 
345
-
346
343
  ```
@@ -7,12 +7,11 @@
7
7
  ```python
8
8
  import asyncio
9
9
 
10
- from sqlalchemy import select, Column, VARCHAR, INTEGER, Table, MetaData
11
- from sqlalchemy.ext.asyncio import async_sessionmaker
12
- from sqlalchemy.orm import sessionmaker
10
+ from sqlalchemy import select, MetaData, Table, Column, VARCHAR, INTEGER
13
11
 
14
12
  from lesscode_database.connection_info import ConnectionInfo
15
13
  from lesscode_database.db_options import db_options
14
+ from lesscode_database.ds_helper import DsHelper
16
15
 
17
16
  db_options.conn_list = [
18
17
  ConnectionInfo(dialect="mysql", name="mysql", host="127.0.0.1", port=3306, user="root",
@@ -53,8 +52,9 @@ db_options.conn_list = [
53
52
 
54
53
  ]
55
54
 
55
+
56
56
  # mysql 同步测试,async_enable=False
57
- with db_options.mysql.dedicated_connection() as conn:
57
+ with DsHelper("mysql").pool.dedicated_connection() as conn:
58
58
  conn.ping(reconnect=True)
59
59
  with conn.cursor() as cursor:
60
60
  cursor.execute("select 1")
@@ -65,7 +65,7 @@ with db_options.mysql.dedicated_connection() as conn:
65
65
 
66
66
  # mysql 异步测试,async_enable=True
67
67
  async def async_mysql_test():
68
- async with db_options.mysql.acquire() as conn:
68
+ async with DsHelper("mysql").pool.acquire() as conn:
69
69
  async with conn.cursor() as cursor:
70
70
  await cursor.execute("select 1")
71
71
  rs = await cursor.fetchone()
@@ -76,7 +76,7 @@ loop = asyncio.get_event_loop()
76
76
  loop.run_until_complete(async_mysql_test())
77
77
 
78
78
  # doris 同步测试,async_enable=False
79
- with db_options.doris.dedicated_connection() as conn:
79
+ with DsHelper("doris").pool.dedicated_connection() as conn:
80
80
  conn.ping(reconnect=True)
81
81
  with conn.cursor() as cursor:
82
82
  cursor.execute("select 1")
@@ -87,7 +87,7 @@ with db_options.doris.dedicated_connection() as conn:
87
87
 
88
88
  # doris 异步测试,async_enable=True
89
89
  async def async_doris_test():
90
- async with db_options.doris.acquire() as conn:
90
+ async with DsHelper("doris").pool.acquire() as conn:
91
91
  async with conn.cursor() as cursor:
92
92
  await cursor.execute("select 1")
93
93
  rs = await cursor.fetchone()
@@ -98,7 +98,7 @@ loop = asyncio.get_event_loop()
98
98
  loop.run_until_complete(async_doris_test())
99
99
 
100
100
  # ocean_base 同步测试,async_enable=False
101
- with db_options.ocean_base.dedicated_connection() as conn:
101
+ with DsHelper("ocean_base").pool.dedicated_connection() as conn:
102
102
  conn.ping(reconnect=True)
103
103
  with conn.cursor() as cursor:
104
104
  cursor.execute("select 1")
@@ -109,7 +109,7 @@ with db_options.ocean_base.dedicated_connection() as conn:
109
109
 
110
110
  # ocean_base 异步测试,async_enable=True
111
111
  async def async_ocean_base_test():
112
- async with db_options.ocean_base.acquire() as conn:
112
+ async with DsHelper("ocean_base").pool.acquire() as conn:
113
113
  async with conn.cursor() as cursor:
114
114
  await cursor.execute("select 1")
115
115
  rs = await cursor.fetchone()
@@ -120,7 +120,7 @@ loop = asyncio.get_event_loop()
120
120
  loop.run_until_complete(async_ocean_base_test())
121
121
 
122
122
  # tidb 同步测试,async_enable=False
123
- with db_options.tidb.dedicated_connection() as conn:
123
+ with DsHelper("tidb").pool.dedicated_connection() as conn:
124
124
  conn.ping(reconnect=True)
125
125
  with conn.cursor() as cursor:
126
126
  cursor.execute("select 1")
@@ -131,7 +131,7 @@ with db_options.tidb.dedicated_connection() as conn:
131
131
 
132
132
  # tidb 异步测试,async_enable=True
133
133
  async def async_tidb_test():
134
- async with db_options.tidb.acquire() as conn:
134
+ async with DsHelper("tidb").pool.acquire() as conn:
135
135
  async with conn.cursor() as cursor:
136
136
  await cursor.execute("select 1")
137
137
  rs = await cursor.fetchone()
@@ -144,7 +144,7 @@ loop.run_until_complete(async_tidb_test())
144
144
 
145
145
  # sqlite3 异步测试
146
146
  async def async_sqlite3_test():
147
- cur = await db_options.sqlite3.cursor()
147
+ cur = await DsHelper("sqlite3").pool.cursor()
148
148
  await cur.execute("SELECT 1")
149
149
  row = await cur.fetchone()
150
150
  print(row)
@@ -154,7 +154,7 @@ loop = asyncio.get_event_loop()
154
154
  loop.run_until_complete(async_sqlite3_test())
155
155
 
156
156
  # sqlite3 同步测试
157
- cursor = db_options.sqlite3.cursor()
157
+ cursor = DsHelper("sqlite3").pool.cursor()
158
158
  cursor.execute('select 1')
159
159
  row = cursor.fetchone()
160
160
  print(row)
@@ -168,7 +168,7 @@ body = {
168
168
  },
169
169
  "size": 1
170
170
  }
171
- resp = db_options.es.search(
171
+ resp = DsHelper("es").pool.search(
172
172
  index="test",
173
173
  body=body
174
174
  )
@@ -177,7 +177,7 @@ print(resp)
177
177
 
178
178
  # es异步测试,async_enable=True
179
179
  async def async_es_test():
180
- resp = await db_options.es.search(
180
+ resp = await DsHelper("es").pool.search(
181
181
  index="test",
182
182
  body={"query": {"match_all": {}}},
183
183
  size=1,
@@ -189,7 +189,7 @@ loop = asyncio.get_event_loop()
189
189
  loop.run_until_complete(async_es_test())
190
190
 
191
191
  # sql server同步测试
192
- cursor = db_options.mssql.cursor()
192
+ cursor = DsHelper("mssql").pool.cursor()
193
193
  cursor.execute('SELECT 1')
194
194
  row = cursor.fetchone()
195
195
  print(row)
@@ -197,7 +197,7 @@ print(row)
197
197
  # sql server暂不支持异步测试
198
198
 
199
199
  # oracle 同步测试
200
- cursor = db_options.oracle.cursor()
200
+ cursor = DsHelper("oracle").pool.cursor()
201
201
  cursor.execute('select 1')
202
202
  row = cursor.fetchone()
203
203
  print(row)
@@ -206,12 +206,12 @@ print(row)
206
206
 
207
207
 
208
208
  # mongo同步测试,async_enable=False
209
- print(db_options.mongo.test.test.find_one())
209
+ print(DsHelper("mongo").pool.test.test.find_one())
210
210
 
211
211
 
212
212
  # mongo异步测试,async_enable=True
213
213
  async def async_mongo_test():
214
- resp = await db_options.mongo.test.test.find_one()
214
+ resp = await DsHelper("mongo").pool.test.test.find_one()
215
215
  print(resp)
216
216
 
217
217
 
@@ -219,15 +219,16 @@ loop = asyncio.get_event_loop()
219
219
  loop.run_until_complete(async_mongo_test())
220
220
 
221
221
  # nebula同步测试,async_enable=False
222
- with db_options.nebula.session_context("root", "nebula") as session:
223
- session.execute(f'USE test')
224
- result = session.execute("match (t:Test) return t limit 1")
222
+ with DsHelper("nebula").make_nebula_session() as session:
223
+ session.execute(f'USE core')
224
+ result = session.execute("match (c:Company) return c limit 1")
225
225
  print(result)
226
226
 
227
+ print(DsHelper("nebula").exec_nebula_gql("match (c:Company) return c limit 1", "core"))
227
228
  # nebula暂不支持异步
228
229
 
229
230
  # postgresql同步测试,async_enable=False
230
- with db_options.postgresql.connection() as conn:
231
+ with DsHelper("postgresql").pool.connection() as conn:
231
232
  with conn.cursor() as cursor:
232
233
  cursor.execute("select 1")
233
234
  rs = cursor.fetchone()
@@ -236,7 +237,7 @@ with db_options.postgresql.connection() as conn:
236
237
 
237
238
  # postgresql异步测试,async_enable=True
238
239
  async def async_pg_test():
239
- async with db_options.postgresql.acquire() as conn:
240
+ async with DsHelper("postgresql").pool.acquire() as conn:
240
241
  async with conn.cursor() as cur:
241
242
  await cur.execute("select 1")
242
243
  rs = await cur.fetchone()
@@ -247,12 +248,12 @@ loop = asyncio.get_event_loop()
247
248
  loop.run_until_complete(async_pg_test())
248
249
 
249
250
  # redis 同步测试,async_enable=False
250
- print(db_options.redis.exists("test"))
251
+ print(DsHelper("redis").pool.exists("test"))
251
252
 
252
253
 
253
254
  # redis 异步测试,async_enable=True
254
255
  async def async_redis_test():
255
- rs = await db_options.redis.exists("test")
256
+ rs = await DsHelper("redis").pool.exists("test")
256
257
  print(rs)
257
258
 
258
259
 
@@ -266,52 +267,49 @@ t1 = Table("test_user", meta, Column("name", VARCHAR(collation='utf8mb3_bin', le
266
267
  server_default=None))
267
268
 
268
269
  # sqlalchemy同步测试,async_enable=False
269
- with sessionmaker(bind=db_options.sqlalchemy)() as session:
270
+ with DsHelper("sqlalchemy").make_sqlalchemy_session() as session:
270
271
  cur = session.execute(select(t1))
271
272
  print(cur.fetchall())
272
273
 
273
274
 
274
275
  # sqlalchemy异步测试,async_enable=True
275
276
  async def async_sqlalchemy_test():
276
- async with async_sessionmaker(db_options.sqlalchemy)() as session:
277
+ async with DsHelper("sqlalchemy").async_make_sqlalchemy_session() as session:
277
278
  cur = await session.execute(select(t1))
278
-
279
279
  print(cur.fetchall())
280
280
 
281
281
 
282
282
  loop = asyncio.get_event_loop()
283
283
  loop.run_until_complete(async_sqlalchemy_test())
284
284
 
285
-
286
285
  # neo4j同步测试
287
286
  def query(tx):
288
- nql = "MATCH (c:Company) return c limit 1"
287
+ nql = "match (p:Person) return p"
289
288
  for record in tx.run(nql):
290
289
  print(record)
291
290
 
292
291
 
293
- with db_options.neo4j.session(database="invest") as session:
292
+ with DsHelper("neo4j").make_neo4j_session() as session:
294
293
  session.execute_read(query)
295
294
 
296
-
297
295
  # neo4j异步测试
298
296
  async def query(tx):
299
- nql = "MATCH (c:Company) return c limit 1"
297
+ nql = "match (p:Person) return p"
300
298
  res = await tx.run(nql)
301
299
  async for record in res:
302
300
  print(record)
303
301
 
304
-
305
302
  async def async_neo4j_test():
306
- async with db_options.neo4j.session(database="invest") as session:
303
+ async with DsHelper("neo4j").async_make_neo4j_session() as session:
307
304
  await session.execute_read(query)
308
305
 
309
306
 
310
307
  loop = asyncio.get_event_loop()
311
308
  loop.run_until_complete(async_neo4j_test())
312
309
 
310
+
313
311
  # clickhouse 同步测试
314
- with db_options.clickhouse as conn:
312
+ with DsHelper("clickhouse").pool.dedicated_connection() as conn:
315
313
  with conn.cursor() as cursor:
316
314
  cursor.execute('SELECT 1')
317
315
  print(cursor.fetchall())
@@ -319,7 +317,7 @@ with db_options.clickhouse as conn:
319
317
 
320
318
  # clickhouse 异步测试测试
321
319
  async def async_clickhouse_test():
322
- async with db_options.clickhouse.acquire() as conn:
320
+ async with DsHelper("clickhouse").pool.acquire() as conn:
323
321
  async with conn.cursor() as cursor:
324
322
  await cursor.execute("SELECT 1")
325
323
  ret = await cursor.fetchone()
@@ -329,5 +327,4 @@ async def async_clickhouse_test():
329
327
  loop = asyncio.get_event_loop()
330
328
  loop.run_until_complete(async_clickhouse_test())
331
329
 
332
-
333
330
  ```
@@ -39,7 +39,7 @@ class DbOptions:
39
39
  for conn_info in value:
40
40
  if isinstance(conn_info, ConnectionInfo):
41
41
  if conn_info.enable:
42
- self.__setattr__(conn_info.name, get_pool(conn_info)[0])
42
+ self.__setattr__(conn_info.name, get_pool(conn_info))
43
43
 
44
44
  def __new__(cls):
45
45
  if cls._instance is None:
@@ -0,0 +1,98 @@
1
+ import importlib
2
+ from asyncio import current_task
3
+ from contextlib import contextmanager, asynccontextmanager
4
+
5
+ from lesscode_database.db_options import db_options
6
+
7
+
8
+ class DsHelper:
9
+ def __init__(self, pool_name):
10
+ self.pool, self.connect_info = getattr(db_options, pool_name)
11
+
12
+ def exec(self, method: str, *args, **kwargs):
13
+ return getattr(self.pool, method)(*args, **kwargs)
14
+
15
+ async def async_exec(self, method: str, *args, **kwargs):
16
+ return await getattr(self.pool, method)(*args, **kwargs)
17
+
18
+ @contextmanager
19
+ def make_sqlalchemy_session(self, **kwargs):
20
+ try:
21
+ sqlalchemy_orm = importlib.import_module("sqlalchemy.orm")
22
+ except ImportError:
23
+ raise Exception(f"sqlalchemy is not exist,run:pip install sqlalchemy==1.4.36")
24
+ session = None
25
+ try:
26
+ db_session = sqlalchemy_orm.scoped_session(sqlalchemy_orm.sessionmaker(bind=self.pool, **kwargs))
27
+ session = db_session()
28
+ yield session
29
+ except Exception:
30
+ if session:
31
+ session.rollback()
32
+ else:
33
+ session.commit()
34
+ finally:
35
+ if session:
36
+ session.close()
37
+
38
+ @asynccontextmanager
39
+ async def async_make_sqlalchemy_session(self, **kwargs):
40
+ try:
41
+ sqlalchemy_asyncio = importlib.import_module("sqlalchemy.ext.asyncio")
42
+ except ImportError:
43
+ raise Exception(f"sqlalchemy is not exist,run:pip install sqlalchemy==1.4.36")
44
+ session = None
45
+ try:
46
+ session = sqlalchemy_asyncio.async_scoped_session(
47
+ sqlalchemy_asyncio.async_sessionmaker(self.pool, **kwargs), current_task)
48
+ yield session
49
+ except Exception:
50
+ if session:
51
+ await session.rollback()
52
+ else:
53
+ await session.commit()
54
+ finally:
55
+ if session:
56
+ await session.close()
57
+
58
+ @contextmanager
59
+ def make_neo4j_session(self, **kwargs):
60
+ session = None
61
+ try:
62
+ session = self.pool.session(database=self.connect_info.db_name, **kwargs)
63
+ yield session
64
+ except Exception as e:
65
+ raise e
66
+ finally:
67
+ if session:
68
+ session.close()
69
+
70
+ @asynccontextmanager
71
+ async def async_make_neo4j_session(self, **kwargs):
72
+ session = None
73
+ try:
74
+ session = self.pool.session(database=self.connect_info.db_name, **kwargs)
75
+ yield session
76
+ except Exception as e:
77
+ raise e
78
+ finally:
79
+ if session:
80
+ await session.close()
81
+
82
+ # @contextmanager
83
+ def make_nebula_session(self, **kwargs):
84
+ try:
85
+ session = self.pool.session_context(user_name=self.connect_info.user, password=self.connect_info.password,
86
+ **kwargs)
87
+ return session
88
+ except Exception as e:
89
+ raise e
90
+
91
+ def exec_nebula_gql(self, sql, space=None):
92
+ space = space if space else self.connect_info.db_name
93
+ if not space:
94
+ raise Exception(f"nebula no selection space")
95
+ with self.make_nebula_session() as session:
96
+ session.execute(f'USE {space}')
97
+ result = session.execute(sql)
98
+ return result
@@ -0,0 +1 @@
1
+ __version__ = "0.0.6"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lesscode-database
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: lesscode_database是数据库连接工具包
5
5
  Author: navysummer
6
6
  Author-email: navysummer@yeah.net
@@ -20,12 +20,11 @@ License-File: LICENSE
20
20
  ```python
21
21
  import asyncio
22
22
 
23
- from sqlalchemy import select, Column, VARCHAR, INTEGER, Table, MetaData
24
- from sqlalchemy.ext.asyncio import async_sessionmaker
25
- from sqlalchemy.orm import sessionmaker
23
+ from sqlalchemy import select, MetaData, Table, Column, VARCHAR, INTEGER
26
24
 
27
25
  from lesscode_database.connection_info import ConnectionInfo
28
26
  from lesscode_database.db_options import db_options
27
+ from lesscode_database.ds_helper import DsHelper
29
28
 
30
29
  db_options.conn_list = [
31
30
  ConnectionInfo(dialect="mysql", name="mysql", host="127.0.0.1", port=3306, user="root",
@@ -66,8 +65,9 @@ db_options.conn_list = [
66
65
 
67
66
  ]
68
67
 
68
+
69
69
  # mysql 同步测试,async_enable=False
70
- with db_options.mysql.dedicated_connection() as conn:
70
+ with DsHelper("mysql").pool.dedicated_connection() as conn:
71
71
  conn.ping(reconnect=True)
72
72
  with conn.cursor() as cursor:
73
73
  cursor.execute("select 1")
@@ -78,7 +78,7 @@ with db_options.mysql.dedicated_connection() as conn:
78
78
 
79
79
  # mysql 异步测试,async_enable=True
80
80
  async def async_mysql_test():
81
- async with db_options.mysql.acquire() as conn:
81
+ async with DsHelper("mysql").pool.acquire() as conn:
82
82
  async with conn.cursor() as cursor:
83
83
  await cursor.execute("select 1")
84
84
  rs = await cursor.fetchone()
@@ -89,7 +89,7 @@ loop = asyncio.get_event_loop()
89
89
  loop.run_until_complete(async_mysql_test())
90
90
 
91
91
  # doris 同步测试,async_enable=False
92
- with db_options.doris.dedicated_connection() as conn:
92
+ with DsHelper("doris").pool.dedicated_connection() as conn:
93
93
  conn.ping(reconnect=True)
94
94
  with conn.cursor() as cursor:
95
95
  cursor.execute("select 1")
@@ -100,7 +100,7 @@ with db_options.doris.dedicated_connection() as conn:
100
100
 
101
101
  # doris 异步测试,async_enable=True
102
102
  async def async_doris_test():
103
- async with db_options.doris.acquire() as conn:
103
+ async with DsHelper("doris").pool.acquire() as conn:
104
104
  async with conn.cursor() as cursor:
105
105
  await cursor.execute("select 1")
106
106
  rs = await cursor.fetchone()
@@ -111,7 +111,7 @@ loop = asyncio.get_event_loop()
111
111
  loop.run_until_complete(async_doris_test())
112
112
 
113
113
  # ocean_base 同步测试,async_enable=False
114
- with db_options.ocean_base.dedicated_connection() as conn:
114
+ with DsHelper("ocean_base").pool.dedicated_connection() as conn:
115
115
  conn.ping(reconnect=True)
116
116
  with conn.cursor() as cursor:
117
117
  cursor.execute("select 1")
@@ -122,7 +122,7 @@ with db_options.ocean_base.dedicated_connection() as conn:
122
122
 
123
123
  # ocean_base 异步测试,async_enable=True
124
124
  async def async_ocean_base_test():
125
- async with db_options.ocean_base.acquire() as conn:
125
+ async with DsHelper("ocean_base").pool.acquire() as conn:
126
126
  async with conn.cursor() as cursor:
127
127
  await cursor.execute("select 1")
128
128
  rs = await cursor.fetchone()
@@ -133,7 +133,7 @@ loop = asyncio.get_event_loop()
133
133
  loop.run_until_complete(async_ocean_base_test())
134
134
 
135
135
  # tidb 同步测试,async_enable=False
136
- with db_options.tidb.dedicated_connection() as conn:
136
+ with DsHelper("tidb").pool.dedicated_connection() as conn:
137
137
  conn.ping(reconnect=True)
138
138
  with conn.cursor() as cursor:
139
139
  cursor.execute("select 1")
@@ -144,7 +144,7 @@ with db_options.tidb.dedicated_connection() as conn:
144
144
 
145
145
  # tidb 异步测试,async_enable=True
146
146
  async def async_tidb_test():
147
- async with db_options.tidb.acquire() as conn:
147
+ async with DsHelper("tidb").pool.acquire() as conn:
148
148
  async with conn.cursor() as cursor:
149
149
  await cursor.execute("select 1")
150
150
  rs = await cursor.fetchone()
@@ -157,7 +157,7 @@ loop.run_until_complete(async_tidb_test())
157
157
 
158
158
  # sqlite3 异步测试
159
159
  async def async_sqlite3_test():
160
- cur = await db_options.sqlite3.cursor()
160
+ cur = await DsHelper("sqlite3").pool.cursor()
161
161
  await cur.execute("SELECT 1")
162
162
  row = await cur.fetchone()
163
163
  print(row)
@@ -167,7 +167,7 @@ loop = asyncio.get_event_loop()
167
167
  loop.run_until_complete(async_sqlite3_test())
168
168
 
169
169
  # sqlite3 同步测试
170
- cursor = db_options.sqlite3.cursor()
170
+ cursor = DsHelper("sqlite3").pool.cursor()
171
171
  cursor.execute('select 1')
172
172
  row = cursor.fetchone()
173
173
  print(row)
@@ -181,7 +181,7 @@ body = {
181
181
  },
182
182
  "size": 1
183
183
  }
184
- resp = db_options.es.search(
184
+ resp = DsHelper("es").pool.search(
185
185
  index="test",
186
186
  body=body
187
187
  )
@@ -190,7 +190,7 @@ print(resp)
190
190
 
191
191
  # es异步测试,async_enable=True
192
192
  async def async_es_test():
193
- resp = await db_options.es.search(
193
+ resp = await DsHelper("es").pool.search(
194
194
  index="test",
195
195
  body={"query": {"match_all": {}}},
196
196
  size=1,
@@ -202,7 +202,7 @@ loop = asyncio.get_event_loop()
202
202
  loop.run_until_complete(async_es_test())
203
203
 
204
204
  # sql server同步测试
205
- cursor = db_options.mssql.cursor()
205
+ cursor = DsHelper("mssql").pool.cursor()
206
206
  cursor.execute('SELECT 1')
207
207
  row = cursor.fetchone()
208
208
  print(row)
@@ -210,7 +210,7 @@ print(row)
210
210
  # sql server暂不支持异步测试
211
211
 
212
212
  # oracle 同步测试
213
- cursor = db_options.oracle.cursor()
213
+ cursor = DsHelper("oracle").pool.cursor()
214
214
  cursor.execute('select 1')
215
215
  row = cursor.fetchone()
216
216
  print(row)
@@ -219,12 +219,12 @@ print(row)
219
219
 
220
220
 
221
221
  # mongo同步测试,async_enable=False
222
- print(db_options.mongo.test.test.find_one())
222
+ print(DsHelper("mongo").pool.test.test.find_one())
223
223
 
224
224
 
225
225
  # mongo异步测试,async_enable=True
226
226
  async def async_mongo_test():
227
- resp = await db_options.mongo.test.test.find_one()
227
+ resp = await DsHelper("mongo").pool.test.test.find_one()
228
228
  print(resp)
229
229
 
230
230
 
@@ -232,15 +232,16 @@ loop = asyncio.get_event_loop()
232
232
  loop.run_until_complete(async_mongo_test())
233
233
 
234
234
  # nebula同步测试,async_enable=False
235
- with db_options.nebula.session_context("root", "nebula") as session:
236
- session.execute(f'USE test')
237
- result = session.execute("match (t:Test) return t limit 1")
235
+ with DsHelper("nebula").make_nebula_session() as session:
236
+ session.execute(f'USE core')
237
+ result = session.execute("match (c:Company) return c limit 1")
238
238
  print(result)
239
239
 
240
+ print(DsHelper("nebula").exec_nebula_gql("match (c:Company) return c limit 1", "core"))
240
241
  # nebula暂不支持异步
241
242
 
242
243
  # postgresql同步测试,async_enable=False
243
- with db_options.postgresql.connection() as conn:
244
+ with DsHelper("postgresql").pool.connection() as conn:
244
245
  with conn.cursor() as cursor:
245
246
  cursor.execute("select 1")
246
247
  rs = cursor.fetchone()
@@ -249,7 +250,7 @@ with db_options.postgresql.connection() as conn:
249
250
 
250
251
  # postgresql异步测试,async_enable=True
251
252
  async def async_pg_test():
252
- async with db_options.postgresql.acquire() as conn:
253
+ async with DsHelper("postgresql").pool.acquire() as conn:
253
254
  async with conn.cursor() as cur:
254
255
  await cur.execute("select 1")
255
256
  rs = await cur.fetchone()
@@ -260,12 +261,12 @@ loop = asyncio.get_event_loop()
260
261
  loop.run_until_complete(async_pg_test())
261
262
 
262
263
  # redis 同步测试,async_enable=False
263
- print(db_options.redis.exists("test"))
264
+ print(DsHelper("redis").pool.exists("test"))
264
265
 
265
266
 
266
267
  # redis 异步测试,async_enable=True
267
268
  async def async_redis_test():
268
- rs = await db_options.redis.exists("test")
269
+ rs = await DsHelper("redis").pool.exists("test")
269
270
  print(rs)
270
271
 
271
272
 
@@ -279,52 +280,49 @@ t1 = Table("test_user", meta, Column("name", VARCHAR(collation='utf8mb3_bin', le
279
280
  server_default=None))
280
281
 
281
282
  # sqlalchemy同步测试,async_enable=False
282
- with sessionmaker(bind=db_options.sqlalchemy)() as session:
283
+ with DsHelper("sqlalchemy").make_sqlalchemy_session() as session:
283
284
  cur = session.execute(select(t1))
284
285
  print(cur.fetchall())
285
286
 
286
287
 
287
288
  # sqlalchemy异步测试,async_enable=True
288
289
  async def async_sqlalchemy_test():
289
- async with async_sessionmaker(db_options.sqlalchemy)() as session:
290
+ async with DsHelper("sqlalchemy").async_make_sqlalchemy_session() as session:
290
291
  cur = await session.execute(select(t1))
291
-
292
292
  print(cur.fetchall())
293
293
 
294
294
 
295
295
  loop = asyncio.get_event_loop()
296
296
  loop.run_until_complete(async_sqlalchemy_test())
297
297
 
298
-
299
298
  # neo4j同步测试
300
299
  def query(tx):
301
- nql = "MATCH (c:Company) return c limit 1"
300
+ nql = "match (p:Person) return p"
302
301
  for record in tx.run(nql):
303
302
  print(record)
304
303
 
305
304
 
306
- with db_options.neo4j.session(database="invest") as session:
305
+ with DsHelper("neo4j").make_neo4j_session() as session:
307
306
  session.execute_read(query)
308
307
 
309
-
310
308
  # neo4j异步测试
311
309
  async def query(tx):
312
- nql = "MATCH (c:Company) return c limit 1"
310
+ nql = "match (p:Person) return p"
313
311
  res = await tx.run(nql)
314
312
  async for record in res:
315
313
  print(record)
316
314
 
317
-
318
315
  async def async_neo4j_test():
319
- async with db_options.neo4j.session(database="invest") as session:
316
+ async with DsHelper("neo4j").async_make_neo4j_session() as session:
320
317
  await session.execute_read(query)
321
318
 
322
319
 
323
320
  loop = asyncio.get_event_loop()
324
321
  loop.run_until_complete(async_neo4j_test())
325
322
 
323
+
326
324
  # clickhouse 同步测试
327
- with db_options.clickhouse as conn:
325
+ with DsHelper("clickhouse").pool.dedicated_connection() as conn:
328
326
  with conn.cursor() as cursor:
329
327
  cursor.execute('SELECT 1')
330
328
  print(cursor.fetchall())
@@ -332,7 +330,7 @@ with db_options.clickhouse as conn:
332
330
 
333
331
  # clickhouse 异步测试测试
334
332
  async def async_clickhouse_test():
335
- async with db_options.clickhouse.acquire() as conn:
333
+ async with DsHelper("clickhouse").pool.acquire() as conn:
336
334
  async with conn.cursor() as cursor:
337
335
  await cursor.execute("SELECT 1")
338
336
  ret = await cursor.fetchone()
@@ -342,5 +340,4 @@ async def async_clickhouse_test():
342
340
  loop = asyncio.get_event_loop()
343
341
  loop.run_until_complete(async_clickhouse_test())
344
342
 
345
-
346
343
  ```
@@ -1,36 +0,0 @@
1
- import importlib
2
- from contextlib import contextmanager
3
-
4
- from lesscode_database.db_options import db_options
5
-
6
-
7
- class DsHelper:
8
- def __init__(self, pool_name):
9
- self.pool, self.conn_info = getattr(db_options, pool_name)
10
-
11
- def exec(self, method: str, *args, **kwargs):
12
- return getattr(self.pool, method)(*args, **kwargs)
13
-
14
- async def async_exec(self, method: str, *args, **kwargs):
15
- return await getattr(self.pool, method)(*args, **kwargs)
16
-
17
- @contextmanager
18
- def make_session(self, **kwargs):
19
- try:
20
- sqlalchemy_orm = importlib.import_module("sqlalchemy.orm")
21
- except ImportError:
22
- raise Exception(f"sqlalchemy is not exist,run:pip install sqlalchemy==1.4.36")
23
- session = None
24
- try:
25
- db_session = sqlalchemy_orm.scoped_session(sqlalchemy_orm.sessionmaker(bind=self.pool, **kwargs))
26
- session = db_session()
27
- yield session
28
- except Exception:
29
- if session:
30
- session.rollback()
31
- raise
32
- else:
33
- session.commit()
34
- finally:
35
- if session:
36
- session.close()
@@ -1 +0,0 @@
1
- __version__ = "0.0.5"