This commit is contained in:
yumoqing 2019-05-14 11:20:28 +08:00
parent 13ed0dd15e
commit ffa87bfd59
4 changed files with 50 additions and 24 deletions

Binary file not shown.

View File

@ -210,42 +210,52 @@ class DBPools:
@wraps(func)
async def wrap_func(dbname,NS,callback,**kw):
sor = await self.getSqlor(dbname)
desc = func(dbname,NS,callback,**kw)
try:
desc = func(dbname,NS,callback,**kw)
ret = await sor.runSQL(desc,NS,callback,**kw)
except Exception as e:
print('error:',e)
raise e
finally:
await self.freeSqlor(sor)
return ret
return wrap_func
def runSQLPaging(self,func):
@wraps(func)
async def wrap_func(dbname,NS,**kw):
sor = await self.getSqlor(dbname)
desc = func(dbname,NS,**kw)
total = await sor.record_count(desc,NS)
recs = await sor.pagingdata(desc,NS)
data = {
"total":total,
"rows":recs
}
print(len(recs),'records return')
await self.freeSqlor(sor)
return data
try:
desc = func(dbname,NS,**kw)
total = await sor.record_count(desc,NS)
recs = await sor.pagingdata(desc,NS)
data = {
"total":total,
"rows":recs
}
print(len(recs),'records return')
return data
except Exception as e:
print('error',e)
raise e
finally:
await self.freeSqlor(sor)
return wrap_func
async def runSQLResultFields(self, dbname,NS,**kwargs):
sor = self.getSqlor(dbname)
desc = func(dbname,NS,**kw)
conn = await self._aquireConn(dbname)
async with conn.cursor() as cur:
sor.setCursor(conn,cur)
ret=await sor.sqlIterator(desc,NS)
ret = [ {'name':i[0],'type':i[1]} for i in cur.description ]
def runSQLResultFields(self, func):
@wraps(func)
async def wrap_func(dbname,NS,**kw):
sor = await self.getSqlor(dbname)
try:
desc = func(dbname,NS,**kw)
ret = await sor.resultFields(desc,NS)
return ret
await self._releaseConn(dbname,conn)
except Exception as e:
print('error=',e)
raise e
finally:
await self.freeSqlor(sor)
return wrap_func
async def getTables(self,dbname):
@self.inSqlor

View File

@ -309,7 +309,7 @@ class SQLor(object):
self.recs.append(rec)
c = Cnt()
await self.sqlIterator(cnt_desc,NS,c.handler)
await self.runSQL(cnt_desc,NS,c.handler)
print(c.recs[0])
t = c.recs[0]['rcnt']
return t
@ -341,8 +341,14 @@ class SQLor(object):
self.recs.append(rec)
c = Cnt()
await self.sqlIterator(paging_desc,NS,c.handler)
await self.runSQL(paging_desc,NS,c.handler)
return c.recs
async def resultFields(self,desc,NS):
NS.update(rows=1,page=1)
r = await self.pagingdata(desc,NS)
ret = [ {'name':i[0],'type':i[1]} for i in self.cur.description ]
return ret
async def runSQL(self,desc,NS,callback,**kw):
class RecordHandler:

View File

@ -31,7 +31,7 @@ dbs={
loop = asyncio.get_event_loop()
pool = DBPools(dbs,loop=loop)
async def testfunc():
async def testfunc1():
@pool.runSQL
def sql(db,ns,callback):
return {
@ -39,4 +39,14 @@ async def testfunc():
}
x = await sql('cfae',{},print)
loop.run_until_complete(testfunc())
async def testfunc2():
@pool.runSQLResultFields
def sql(db,NS):
return {
"sql_string":"select * from product",
}
x = await sql('cfae',{})
print(x)
loop.run_until_complete(testfunc1())
loop.run_until_complete(testfunc2())