diff --git a/scense_drag/init.py b/scense_drag/init.py index 1be2747..c45d304 100644 --- a/scense_drag/init.py +++ b/scense_drag/init.py @@ -42,6 +42,12 @@ def _dumps(obj): return json.dumps(obj, ensure_ascii=False) +def _sqlor_ctx(dbname): + """取 sqlor 连接上下文(必须用 DBPools,ServerEnv 无 sqlorContext)。""" + from sqlor.dbpools import DBPools + return DBPools().sqlorContext(dbname) + + async def get_drag_graph_dbname(): """drag 业务库名(宿主 get_module_dbname 决定)。""" try: @@ -64,7 +70,7 @@ async def list_drag_graphs(params=None): params = params or {} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: where, args = [], [] name = params.get('name') status = params.get('status') @@ -97,7 +103,7 @@ async def get_drag_graph(ns): return {'code': 'PARAM_REQUIRED', 'message': '缺少 id', 'field': 'id'} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: recs = await sor.R('drag_graph', {'id': gid}) if not recs: return {'code': 'NOT_FOUND', 'message': '画布不存在: %s' % gid} @@ -149,7 +155,7 @@ async def create_drag_graph(ns): content = result.get('content', '') if result.get('success') else '' dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: await sor.C('drag_graph', { 'id': gid, 'name': str(name).strip(), 'description': str(ns.get('description') or ''), @@ -189,7 +195,7 @@ async def update_drag_graph(ns): content = result.get('content', '') if result.get('success') else '' dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: recs = await sor.R('drag_graph', {'id': gid}) if not recs: return {'code': 'NOT_FOUND', 'message': '画布不存在: %s' % gid} @@ -215,7 +221,7 @@ async def delete_drag_graph(ns): return {'code': 'PARAM_REQUIRED', 'message': '缺少 id', 'field': 'id'} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: await sor.D('drag_graph', {'id': gid}) return {'success': True, 'id': gid} except Exception as e: @@ -315,7 +321,7 @@ async def list_drag_templates(params=None): params = params or {} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: where, args = [], [] name = params.get('name') if name: @@ -344,7 +350,7 @@ async def get_drag_template(ns): return {'code': 'PARAM_REQUIRED', 'message': '缺少 id', 'field': 'id'} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: recs = await sor.R('drag_template', {'id': tid}) if not recs: return {'code': 'NOT_FOUND', 'message': '模板不存在: %s' % tid} @@ -384,7 +390,7 @@ async def create_drag_template(ns): v = validate_graph({'blocks': blocks, 'connections': conns}) dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: tid = _new_id() await sor.C('drag_template', { 'id': tid, 'name': str(name).strip(), @@ -416,7 +422,7 @@ async def update_drag_template(ns): return {'code': 'PARAM_TYPE', 'message': 'connections 不是合法 JSON', 'field': 'connections'} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: recs = await sor.R('drag_template', {'id': tid}) if not recs: return {'code': 'NOT_FOUND', 'message': '模板不存在: %s' % tid} @@ -444,7 +450,7 @@ async def delete_drag_template(ns): return {'code': 'PARAM_REQUIRED', 'message': '缺少 id', 'field': 'id'} dbname = await get_drag_graph_dbname() try: - async with _server_env().sqlorContext(dbname) as sor: + async with _sqlor_ctx(dbname) as sor: await sor.D('drag_template', {'id': tid}) return {'success': True, 'id': tid} except Exception as e: