add sqlor context
This commit is contained in:
parent
a18b9c381c
commit
2271bc4e15
@ -192,7 +192,12 @@ class DBPools:
|
|||||||
sqlor = await self.getSqlor(name)
|
sqlor = await self.getSqlor(name)
|
||||||
try:
|
try:
|
||||||
yield sqlor
|
yield sqlor
|
||||||
|
except:
|
||||||
|
if sqlor.dataChanged:
|
||||||
|
sqlor.rollback()
|
||||||
finally:
|
finally:
|
||||||
|
if sqlor.dataChanged:
|
||||||
|
sqlor.commit()
|
||||||
await self.freeSqlor(sqlor)
|
await self.freeSqlor(sqlor)
|
||||||
|
|
||||||
async def _aquireConn(self,dbname):
|
async def _aquireConn(self,dbname):
|
||||||
|
37
sqlor/sor.py
37
sqlor/sor.py
@ -63,6 +63,7 @@ class SQLor(object):
|
|||||||
self.writer = None
|
self.writer = None
|
||||||
self.convfuncs = {}
|
self.convfuncs = {}
|
||||||
self.cc = ConditionConvert()
|
self.cc = ConditionConvert()
|
||||||
|
self.dataChanged = False
|
||||||
|
|
||||||
def setCursor(self,async_mode,conn,cur):
|
def setCursor(self,async_mode,conn,cur):
|
||||||
self.async_mode = async_mode
|
self.async_mode = async_mode
|
||||||
@ -197,10 +198,28 @@ class SQLor(object):
|
|||||||
|
|
||||||
return (m_sql,newdata)
|
return (m_sql,newdata)
|
||||||
|
|
||||||
|
def getSQLType(self,sql):
|
||||||
|
"""
|
||||||
|
return one of "qry", "dml" and "ddl"
|
||||||
|
ddl change the database schema
|
||||||
|
dml change the database data
|
||||||
|
qry query data
|
||||||
|
"""
|
||||||
|
|
||||||
|
a = sql.lstrip(' \t\n\r')
|
||||||
|
a = a.lower()
|
||||||
|
al = a.split(' ')
|
||||||
|
if al[0] == 'select':
|
||||||
|
return 'qry'
|
||||||
|
if al[0] in ['update','delete','insert']:
|
||||||
|
return 'dml'
|
||||||
|
return 'ddl'
|
||||||
|
|
||||||
async def execute(self,sql,value,callback,**kwargs):
|
async def execute(self,sql,value,callback,**kwargs):
|
||||||
|
sqltype = self.getSqlType(sql)
|
||||||
cur = self.cursor()
|
cur = self.cursor()
|
||||||
await self.runVarSQL(cur,sql,value)
|
await self.runVarSQL(cur,sql,value)
|
||||||
if callback is not None:
|
if sqltype == 'qry' and callback is not None:
|
||||||
fields = [ i[0].lower() for i in cur.description ]
|
fields = [ i[0].lower() for i in cur.description ]
|
||||||
rec = None
|
rec = None
|
||||||
if self.async_mode:
|
if self.async_mode:
|
||||||
@ -217,7 +236,8 @@ class SQLor(object):
|
|||||||
rec = await cur.fetchone()
|
rec = await cur.fetchone()
|
||||||
else:
|
else:
|
||||||
rec = cur.fetchone()
|
rec = cur.fetchone()
|
||||||
|
if sqltype == 'dml':
|
||||||
|
self.dataChanged = True
|
||||||
|
|
||||||
async def executemany(self,sql,values):
|
async def executemany(self,sql,values):
|
||||||
cur = self.cursor()
|
cur = self.cursor()
|
||||||
@ -282,10 +302,7 @@ class SQLor(object):
|
|||||||
return await self.execute(sql,{},lambda x:ret.append(x))
|
return await self.execute(sql,{},lambda x:ret.append(x))
|
||||||
|
|
||||||
def isSelectSql(self,sql):
|
def isSelectSql(self,sql):
|
||||||
i = 0
|
return self.getSqlType(sql) == 'qry'
|
||||||
while sql[i] in "\r\n \t":
|
|
||||||
i = i + 1
|
|
||||||
return sql.lower().startswith('select ')
|
|
||||||
|
|
||||||
def getSQLfromDesc(self,desc):
|
def getSQLfromDesc(self,desc):
|
||||||
sql = ''
|
sql = ''
|
||||||
@ -455,4 +472,10 @@ class SQLor(object):
|
|||||||
desc['validation'].append(idx)
|
desc['validation'].append(idx)
|
||||||
return desc
|
return desc
|
||||||
|
|
||||||
|
def rollback(self):
|
||||||
|
self.conn.rollback()
|
||||||
|
self.dataChanged = False
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
self.conn.commit()
|
||||||
|
self.datachanged = False
|
||||||
|
Loading…
Reference in New Issue
Block a user