From a18b9c381ced0b479ad4926d644a6de64242f4d6 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 6 May 2020 11:48:26 +0800 Subject: [PATCH] add sqlor context --- sqlor/dbpools.py | 9 +++++++++ sqlor/sor.py | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/sqlor/dbpools.py b/sqlor/dbpools.py index 426019b..ed018d5 100644 --- a/sqlor/dbpools.py +++ b/sqlor/dbpools.py @@ -2,6 +2,7 @@ import asyncio from functools import wraps import codecs +from contextlib import asynccontextmanager from appPublic.myImport import myImport from appPublic.dictObject import DictObject @@ -186,6 +187,14 @@ class DBPools: async def freeSqlor(self,sor): await self._releaseConn(sor.name,sor.conn,sor.cur) + @asynccontextmanager + async def sqlorContext(self,name): + sqlor = await self.getSqlor(name) + try: + yield sqlor + finally: + await self.freeSqlor(sqlor) + async def _aquireConn(self,dbname): p = self._cpools.get(dbname) if p == None: diff --git a/sqlor/sor.py b/sqlor/sor.py index e87da83..7fddb65 100644 --- a/sqlor/sor.py +++ b/sqlor/sor.py @@ -455,5 +455,4 @@ class SQLor(object): desc['validation'].append(idx) return desc -