for multiple sql command execute
This commit is contained in:
parent
1a6e6fa9a5
commit
88909b1b01
@ -41,30 +41,34 @@ 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
|
||||||
|
async def main(dbname,NS,**kw):
|
||||||
|
pkdata = await self.primaryKey(**kw)
|
||||||
pks = [ i.field_name for i in pkdata ]
|
pks = [ i.field_name for i in pkdata ]
|
||||||
data = await self.pool.getTableFields(self.dbname,self.tablename)
|
data = await self.pool.getTableFields(self.dbname,self.tablename,**kw)
|
||||||
for d in data:
|
for d in data:
|
||||||
if d.name in pks:
|
if d.name in pks:
|
||||||
d.update({'primarykey':True})
|
d.update({'primarykey':True})
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'tableInfo',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'tableInfo',data)
|
||||||
return 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)
|
||||||
ret = {}
|
ret = {}
|
||||||
@ -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
|
||||||
|
async def main(dbname,NS,**kw):
|
||||||
|
fields = await self.pool.getTableFields(self.dbname,self.tablename,**kw)
|
||||||
|
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 NS.items() ]
|
||||||
|
pk = await self.primaryKey(**kw)
|
||||||
k = pk[0]['field_name']
|
k = pk[0]['field_name']
|
||||||
if not data.get(k):
|
if not data.get(k):
|
||||||
v = getID()
|
v = getID()
|
||||||
data[k] = v
|
data[k] = v
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeAdd',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeAdd',data)
|
||||||
await addSQL(self.dbname,data)
|
await addSQL(self.dbname,data,**kw)
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterAdd',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterAdd',data)
|
||||||
return {k:data[k]}
|
return {k:data[k]}
|
||||||
return data
|
|
||||||
|
|
||||||
async def defaultFilter(self,NS):
|
return await main(self.dbname,rec,**kw)
|
||||||
fields = await self.pool.getTableFields(self.dbname,self.tablename)
|
|
||||||
|
async def defaultFilter(self,NS,**kw):
|
||||||
|
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,7 +257,9 @@ class CRUD(object):
|
|||||||
}
|
}
|
||||||
return sqldesc
|
return sqldesc
|
||||||
|
|
||||||
p = await self.primaryKey()
|
@self.pool.inSQLor
|
||||||
|
async def main(dbname,NS,**kw):
|
||||||
|
p = await self.primaryKey(**kw)
|
||||||
if NS.get('__id') is not None:
|
if NS.get('__id') is not None:
|
||||||
NS[p[0]['field_name']] = NS['__id']
|
NS[p[0]['field_name']] = NS['__id']
|
||||||
del NS['__id']
|
del NS['__id']
|
||||||
@ -263,18 +272,20 @@ class CRUD(object):
|
|||||||
|
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeRetrieve',NS)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeRetrieve',NS)
|
||||||
if NS.get('page'):
|
if NS.get('page'):
|
||||||
data = await pagingdata(self.dbname,data)
|
data = await pagingdata(self.dbname,data,**kw)
|
||||||
else:
|
else:
|
||||||
data = await retrieve(self.dbname,data)
|
data = await retrieve(self.dbname,data,**kw)
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterRetrieve',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterRetrieve',data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
async def U(self,data):
|
return await main(self.dbname,NS,**kw)
|
||||||
|
|
||||||
|
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
|
||||||
|
async def main(dbname,NS,**kw):
|
||||||
|
pk = await self.primaryKey(**kw)
|
||||||
pkfields = [k.field_name for k in pk ]
|
pkfields = [k.field_name for k in pk ]
|
||||||
newData = [ k for k in data if k not in pkfields ]
|
newData = [ k for k in data if k not in pkfields ]
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeUpdate',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeUpdate',data)
|
||||||
await update(self.dbname,data)
|
await update(self.dbname,data,**kw)
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterUpdate',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterUpdate',data)
|
||||||
return 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
|
||||||
|
|
||||||
|
@self.pool.inSQLor
|
||||||
|
async def main(dbname,NS,**kw):
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeDelete',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'beforeDelete',data)
|
||||||
await delete(self.dbname,data,pkfields)
|
await delete(self.dbname,data,pkfields,**kw)
|
||||||
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterDelete',data)
|
data = self.oa.execute(self.dbname+'_'+self.tablename,'afterDelete',data)
|
||||||
return data
|
return data
|
||||||
|
return await main(self.dbname,data,**kw)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
DBPools({
|
DBPools({
|
||||||
|
@ -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,24 +235,32 @@ 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:
|
||||||
|
if commit:
|
||||||
await self.freeSqlor(sor)
|
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)
|
||||||
|
kw1 = {}
|
||||||
|
[ kw1.update({k:v}) for k,v in kw.items if k!='callback' ]
|
||||||
|
ret = await sor.runSQL(desc,NS,callback,**kw1)
|
||||||
|
if commit:
|
||||||
try:
|
try:
|
||||||
await sor.conn.commit()
|
await sor.conn.commit()
|
||||||
except:
|
except:
|
||||||
@ -251,21 +271,25 @@ class DBPools:
|
|||||||
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:
|
||||||
|
if commit:
|
||||||
await self.freeSqlor(sor)
|
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:
|
||||||
|
if commit:
|
||||||
await self.freeSqlor(sor)
|
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:
|
||||||
|
if commit:
|
||||||
await self.freeSqlor(sor)
|
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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user