for multiple sql command execute

This commit is contained in:
yumoqing 2019-10-22 11:16:46 +08:00
parent 1a6e6fa9a5
commit 88909b1b01
2 changed files with 151 additions and 102 deletions

View File

@ -41,29 +41,33 @@ class CRUD(object):
self.primary_data = None self.primary_data = None
self.oa = ObjectAction() self.oa = ObjectAction()
async def primaryKey(self): async def primaryKey(self,**kw):
if self.primary_data is None: if self.primary_data is None:
self.primary_data = await self.pool.getTablePrimaryKey(self.dbname, self.primary_data = await self.pool.getTablePrimaryKey(self.dbname,
self.tablename) self.tablename,**kw)
return self.primary_data return self.primary_data
async def forignKeys(self): async def forignKeys(self,**kw):
data = self.pool.getTableForignKeys(self.dbname,self.tablename) data = self.pool.getTableForignKeys(self.dbname,self.tablename,**kw)
return data return data
async def I(self): async def I(self,**kw):
""" """
fields information fields information
""" """
pkdata = await self.primaryKey() @self.pool.inSQLor
pks = [ i.field_name for i in pkdata ] async def main(dbname,NS,**kw):
data = await self.pool.getTableFields(self.dbname,self.tablename) pkdata = await self.primaryKey(**kw)
for d in data: pks = [ i.field_name for i in pkdata ]
if d.name in pks: data = await self.pool.getTableFields(self.dbname,self.tablename,**kw)
d.update({'primarykey':True}) for d in data:
data = self.oa.execute(self.dbname+'_'+self.tablename,'tableInfo',data) if d.name in pks:
return data d.update({'primarykey':True})
data = self.oa.execute(self.dbname+'_'+self.tablename,'tableInfo',data)
return data
return await main(self.dbname,{},**kw)
async def fromStr(self,data): async def fromStr(self,data):
fields = await self.pool.getTableFields(self.dbname,self.tablename) fields = await self.pool.getTableFields(self.dbname,self.tablename)
@ -90,11 +94,11 @@ class CRUD(object):
ret[k] = f(data[k]) ret[k] = f(data[k])
return ret return ret
async def datagrid(self,request,target): async def datagrid(self,request,targeti,**kw):
fields = await self.I() fields = await self.I()
fs = [ self.defaultIOField(f) for f in fields ] fs = [ self.defaultIOField(f) for f in fields ]
id = self.dbname+':'+ self.tablename id = self.dbname+':'+ self.tablename
pk = await self.primaryKey() pk = await self.primaryKey(**kw)
idField = pk[0]['field_name'] idField = pk[0]['field_name']
data = { data = {
"tmplname":"widget_js.tmpl", "tmplname":"widget_js.tmpl",
@ -178,18 +182,12 @@ class CRUD(object):
"iotype":"text" "iotype":"text"
} }
async def C(self,rec): async def C(self,rec,**kw):
""" """
create new data create new data
""" """
fields = await self.pool.getTableFields(self.dbname,self.tablename)
flist = [ f['name'] for f in fields ]
fns = ','.join(flist)
vfs = ','.join([ '${' + f + '}$' for f in flist ])
data = {}
[ data.update({k.lower():v}) for k,v in rec.items() ]
@self.pool.runSQL @self.pool.runSQL
async def addSQL(dbname,data,callback=None): async def addSQL(dbname,data,**kw):
sqldesc={ sqldesc={
"sql_string" : """ "sql_string" : """
insert into %s (%s) values (%s) insert into %s (%s) values (%s)
@ -197,31 +195,40 @@ class CRUD(object):
} }
return sqldesc return sqldesc
pk = await self.primaryKey() @self.pool.inSQLor
k = pk[0]['field_name'] async def main(dbname,NS,**kw):
if not data.get(k): fields = await self.pool.getTableFields(self.dbname,self.tablename,**kw)
v = getID() flist = [ f['name'] for f in fields ]
data[k] = v fns = ','.join(flist)
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeAdd',data) vfs = ','.join([ '${' + f + '}$' for f in flist ])
await addSQL(self.dbname,data) data = {}
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterAdd',data) [ data.update({k.lower():v}) for k,v in NS.items() ]
return {k:data[k]} pk = await self.primaryKey(**kw)
return data k = pk[0]['field_name']
if not data.get(k):
v = getID()
data[k] = v
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeAdd',data)
await addSQL(self.dbname,data,**kw)
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterAdd',data)
return {k:data[k]}
return await main(self.dbname,rec,**kw)
async def defaultFilter(self,NS): async def defaultFilter(self,NS,**kw):
fields = await self.pool.getTableFields(self.dbname,self.tablename) fields = await self.pool.getTableFields(self.dbname,self.tablename,**kw)
d = [ '%s = ${%s}$' % (f['name'],f['name']) for f in fields if f['name'] in NS.keys() ] d = [ '%s = ${%s}$' % (f['name'],f['name']) for f in fields if f['name'] in NS.keys() ]
if len(d) == 0: if len(d) == 0:
return '' return ''
ret = ' and ' + ' and '.join(d) ret = ' and ' + ' and '.join(d)
return ret return ret
async def R(self,filters=None,NS={}): async def R(self,filters=None,NS={},**kw):
""" """
retrieve data retrieve data
""" """
@self.pool.runSQL @self.pool.runSQL
async def retrieve(dbname,data,callback=None): async def retrieve(dbname,data,**kw):
fstr = '' fstr = ''
if filters is not None: if filters is not None:
fstr = ' and ' fstr = ' and '
@ -235,7 +242,7 @@ class CRUD(object):
return sqldesc return sqldesc
@self.pool.runSQLPaging @self.pool.runSQLPaging
async def pagingdata(dbname,data,filters=None): async def pagingdata(dbname,data,filters=None,**kw):
fstr = "" fstr = ""
if filters is not None: if filters is not None:
fstr = ' and ' fstr = ' and '
@ -250,31 +257,35 @@ class CRUD(object):
} }
return sqldesc return sqldesc
p = await self.primaryKey() @self.pool.inSQLor
if NS.get('__id') is not None: async def main(dbname,NS,**kw):
NS[p[0]['field_name']] = NS['__id'] p = await self.primaryKey(**kw)
del NS['__id'] if NS.get('__id') is not None:
NS[p[0]['field_name']] = NS['__id']
del NS['__id']
if NS.get('page'):
del NS['page']
if NS.get('page'): if NS.get('page'):
del NS['page'] if NS.get('sort',None) is None:
NS['sort'] = p[0]['field_name']
if NS.get('page'): data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeRetrieve',NS)
if NS.get('sort',None) is None: if NS.get('page'):
NS['sort'] = p[0]['field_name'] data = await pagingdata(self.dbname,data,**kw)
else:
data = await retrieve(self.dbname,data,**kw)
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterRetrieve',data)
return data
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeRetrieve',NS) return await main(self.dbname,NS,**kw)
if NS.get('page'):
data = await pagingdata(self.dbname,data)
else:
data = await retrieve(self.dbname,data)
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterRetrieve',data)
return data
async def U(self,data): async def U(self,data, **kw):
""" """
update data update data
""" """
@self.pool.runSQL @self.pool.runSQL
async def update(dbname,NS,callback=None): async def update(dbname,NS,**kw):
condi = [ i['field_name'] for i in self.primary_data ] condi = [ i['field_name'] for i in self.primary_data ]
newData = [ i for i in NS.keys() if i not in condi ] newData = [ i for i in NS.keys() if i not in condi ]
c = [ '%s = ${%s}$' % (i,i) for i in condi ] c = [ '%s = ${%s}$' % (i,i) for i in condi ]
@ -286,20 +297,23 @@ class CRUD(object):
} }
return sqldesc return sqldesc
pk = await self.primaryKey() @self.pool.inSQLor
pkfields = [k.field_name for k in pk ] async def main(dbname,NS,**kw):
newData = [ k for k in data if k not in pkfields ] pk = await self.primaryKey(**kw)
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeUpdate',data) pkfields = [k.field_name for k in pk ]
await update(self.dbname,data) newData = [ k for k in data if k not in pkfields ]
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterUpdate',data) data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeUpdate',data)
return data await update(self.dbname,data,**kw)
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterUpdate',data)
return data
return await main(self.dbname,data,**kw)
async def D(self,data): async def D(self,data,**kw):
""" """
delete data delete data
""" """
@self.pool.runSQL @self.pool.runSQL
def delete(dbname,data): def delete(dbname,data,**kw):
pnames = [ i['field_name'] for i in self.primary_data ] pnames = [ i['field_name'] for i in self.primary_data ]
c = [ '%s = ${%s}$' % (i,i) for i in pnames ] c = [ '%s = ${%s}$' % (i,i) for i in pnames ]
cs = ' and '.join(c) cs = ' and '.join(c)
@ -308,10 +322,13 @@ class CRUD(object):
} }
return sqldesc return sqldesc
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeDelete',data) @self.pool.inSQLor
await delete(self.dbname,data,pkfields) async def main(dbname,NS,**kw):
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterDelete',data) data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeDelete',data)
return data await delete(self.dbname,data,pkfields,**kw)
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterDelete',data)
return data
return await main(self.dbname,data,**kw)
if __name__ == '__main__': if __name__ == '__main__':
DBPools({ DBPools({

View File

@ -210,12 +210,24 @@ class DBPools:
raise Exception('database (%s) not connected'%dbname) raise Exception('database (%s) not connected'%dbname)
await p.release(conn) await p.release(conn)
async def useOrGetSor(self,dbname,**kw):
commit = False
if kw.get('sor'):
sor = kw['sor']
else:
sor = await self.getSqlor(dbname)
commit = True
return sor, commit
def inSqlor(self,func): def inSqlor(self,func):
@wraps(func) @wraps(func)
async def wrap_func(sor,dbname,*args,**kw): async def wrap_func(dbname,NS,*args,**kw):
sor = await self.getSqlor(dbname) sor, commit = self.useOrGetSor(dbname, **kw)
kw['sor'] = sor
try: try:
ret = await func(sor,dbname,*args,**kw) ret = await func(dbname,NS,*args,**kw)
if not commit:
return ret
try: try:
await sor.conn.commit() await sor.conn.commit()
except: except:
@ -223,49 +235,61 @@ class DBPools:
return ret return ret
except Exception as e: except Exception as e:
print('error',sor) print('error',sor)
if not commit:
raise e
try: try:
await sor.conn.rollback() await sor.conn.rollback()
except: except:
pass pass
raise e raise e
finally: finally:
await self.freeSqlor(sor) if commit:
await self.freeSqlor(sor)
return wrap_func return wrap_func
def runSQL(self,func): def runSQL(self,func):
@wraps(func) @wraps(func)
async def wrap_func(dbname,NS,*,callback=None,**kw): async def wrap_func(dbname,NS,*args,**kw):
sor = await self.getSqlor(dbname) sor, commit = self.useOrGetSor(dbname,**kw)
kw['sor'] = sor
ret = None ret = None
try: try:
desc = await func(dbname,NS,callback=callback,**kw) desc = await func(dbname,NS,*args,**kw)
ret = await sor.runSQL(desc,NS,callback,**kw) callback = kw.get('callback',None)
try: kw1 = {}
await sor.conn.commit() [ kw1.update({k:v}) for k,v in kw.items if k!='callback' ]
except: ret = await sor.runSQL(desc,NS,callback,**kw1)
pass if commit:
try:
await sor.conn.commit()
except:
pass
if NS.get('dummy'): if NS.get('dummy'):
return NS['dummy'] return NS['dummy']
else: else:
return [] return []
except Exception as e: except Exception as e:
print('error:',e) print('error:',e)
if not commit:
raise e
try: try:
await sor.conn.rollback() await sor.conn.rollback()
except: except:
pass pass
raise e raise e
finally: finally:
await self.freeSqlor(sor) if commit:
await self.freeSqlor(sor)
return wrap_func return wrap_func
def runSQLPaging(self,func): def runSQLPaging(self,func):
@wraps(func) @wraps(func)
async def wrap_func(dbname,NS,**kw): async def wrap_func(dbname,NS,*args,**kw):
sor = await self.getSqlor(dbname) sor, commit = self.useOrGetSor(dbname,**kw)
kw['sor'] = sor
try: try:
desc = await func(dbname,NS,**kw) desc = await func(dbname,NS,*args,**kw)
total = await sor.record_count(desc,NS) total = await sor.record_count(desc,NS)
recs = await sor.pagingdata(desc,NS) recs = await sor.pagingdata(desc,NS)
data = { data = {
@ -277,56 +301,64 @@ class DBPools:
print('error',e) print('error',e)
raise e raise e
finally: finally:
await self.freeSqlor(sor) if commit:
await self.freeSqlor(sor)
return wrap_func return wrap_func
def runSQLResultFields(self, func): async def runSQLResultFields(self, func):
@wraps(func) @wraps(func)
async def wrap_func(dbname,NS,**kw): async def wrap_func(dbname,NS,*args,**kw):
sor = await self.getSqlor(dbname) sor, commit = self.useOrGetSor(dbname,**kw)
kw['sor'] = sor
try: try:
desc = await func(dbname,NS,**kw) desc = await func(dbname,NS,*args,**kw)
ret = await sor.resultFields(desc,NS) ret = await sor.resultFields(desc,NS)
return ret return ret
except Exception as e: except Exception as e:
print('error=',e) print('error=',e)
raise e raise e
finally: finally:
await self.freeSqlor(sor) if commit:
await self.freeSqlor(sor)
return wrap_func return wrap_func
async def getTables(self,dbname): async def getTables(self,dbname,**kw):
@self.inSqlor @self.inSqlor
async def _getTables(sor,dbname): async def _getTables(dbname,NS,**kw):
sor = kw['sor']
ret = await sor.tables() ret = await sor.tables()
return ret return ret
return await _getTables(None,dbname) return await _getTables(dbname,{},**kw)
async def getTableFields(self,dbname,tblname): async def getTableFields(self,dbname,tblname,**kw):
@self.inSqlor @self.inSqlor
async def _getTableFields(sor,dbname,tblname): async def _getTableFields(dbname,NS,tblname,**kw):
sor = kw['sor']
ret = await sor.fields(tblname) ret = await sor.fields(tblname)
return ret return ret
return await _getTableFields(None,dbname,tblname) return await _getTableFields(dbname,{},tblname,**kw)
async def getTablePrimaryKey(self,dbname,tblname): async def getTablePrimaryKey(self,dbname,tblname):
@self.inSqlor @self.inSqlor
async def _getTablePrimaryKey(sor,dbname,tblname): async def _getTablePrimaryKey(dbname,NS,tblname,**kw):
sor = kw['sor']
ret = await sor.primary(tblname) ret = await sor.primary(tblname)
return ret return ret
return await _getTablePrimaryKey(None,dbname,tblname) return await _getTablePrimaryKey(dbname,{},tblname,**kw)
async def getTableIndexes(self,dbname,tblname): async def getTableIndexes(self,dbname,tblname):
@self.inSqlor @self.inSqlor
async def _getTablePrimaryKey(sor,dbname,tblname): async def _getTablePrimaryKey(dbname,NS,tblname,**kw):
sor = kw['sor']
ret = await sor.indexes(tblname) ret = await sor.indexes(tblname)
return ret return ret
return await _getTablePrimaryKey(None,dbname,tblname) return await _getTablePrimaryKey(dbname,{},tblname,**kw)
async def getTableForignKeys(self,dbname,tblname): async def getTableForignKeys(self,dbname,tblname):
@self.inSqlor @self.inSqlor
async def _getTableForignKeys(sor,dbname,tblname): async def _getTableForignKeys(dbname,NS,tblname,**kw):
sor = kw['sor']
ret = await sor.fkeys(tblname) ret = await sor.fkeys(tblname)
return ret return ret
return await _getTableForignKeys(None,dbname,tblname) return await _getTableForignKeys(dbname,{},tblname,**kw)