2019-06-03 18:07:21 +08:00
|
|
|
import re
|
|
|
|
from .sor import SQLor
|
|
|
|
|
|
|
|
class SQLite3or(SQLor):
|
|
|
|
db2modelTypeMapping = {
|
2021-07-22 16:24:34 +08:00
|
|
|
'text':'str',
|
2019-06-03 18:07:21 +08:00
|
|
|
'blob':'file',
|
2021-07-22 16:24:34 +08:00
|
|
|
'int':'long',
|
2019-06-03 18:07:21 +08:00
|
|
|
'integer':'long',
|
2021-07-22 16:24:34 +08:00
|
|
|
'real':'float',
|
2019-06-03 18:07:21 +08:00
|
|
|
}
|
|
|
|
model2dbTypemapping = {
|
2021-07-22 16:24:34 +08:00
|
|
|
'date':'text',
|
|
|
|
'time':'text',
|
|
|
|
'timestamp':'text',
|
|
|
|
'str':'text',
|
|
|
|
'char':'text',
|
2019-06-03 18:07:21 +08:00
|
|
|
'short':'int',
|
2021-07-22 16:24:34 +08:00
|
|
|
'long':'int',
|
|
|
|
'float':'real',
|
|
|
|
'text':'text',
|
2019-06-03 18:07:21 +08:00
|
|
|
'file':'blob',
|
|
|
|
}
|
|
|
|
@classmethod
|
|
|
|
def isMe(self,name):
|
|
|
|
return name=='sqlite3'
|
|
|
|
|
2020-10-15 14:39:31 +08:00
|
|
|
def placeHolder(self,varname,pos=None):
|
2019-06-03 18:07:21 +08:00
|
|
|
if varname=='__mainsql__' :
|
|
|
|
return ''
|
|
|
|
return '?'
|
|
|
|
|
|
|
|
def dataConvert(self,dataList):
|
|
|
|
if type(dataList) == type({}):
|
|
|
|
d = [ i for i in dataList.values()]
|
|
|
|
else:
|
|
|
|
d = [ i['value'] for i in dataList]
|
|
|
|
return tuple(d)
|
|
|
|
|
|
|
|
def pagingSQLmodel(self):
|
|
|
|
sql = u"""select * from (%s) order by $[sort]$ $[order]$ limit $[from_line]$,$[end_line]$"""
|
|
|
|
return sql
|
|
|
|
|
|
|
|
def tablesSQL(self):
|
|
|
|
sqlcmd = u"""select name, tbl_name as title from sqlite_master where upper(type) = 'TABLE'"""
|
|
|
|
return sqlcmd
|
|
|
|
|
|
|
|
def fieldsSQL(self,tablename):
|
2021-07-22 16:24:34 +08:00
|
|
|
# sqlcmd="""PRAGMA table_info('%s')""" % tablename.lower()
|
2019-06-03 18:07:21 +08:00
|
|
|
return sqlcmd
|
|
|
|
|
|
|
|
def fields(self,tablename):
|
|
|
|
m = u'(\w+)\(((\d+)(,(\d+)){0,1})\){0,1}'
|
|
|
|
k = re.compile(m)
|
|
|
|
def typesplit(typ):
|
|
|
|
d = k.search(typ)
|
|
|
|
if d is None:
|
|
|
|
return typ,0,0
|
|
|
|
|
|
|
|
return d.group(1),int(d.group(3) if d.group(3) is not None else 0 ),int(d.group(5) if d.group(5) is not None else 0)
|
|
|
|
|
|
|
|
sqlstring = self.fieldsSQL(tablename)
|
|
|
|
recs = []
|
|
|
|
self.execute(sqlstring,callback=lambda x:recs.append(x))
|
|
|
|
for r in recs:
|
|
|
|
t,l,d = typesplit(r['type'])
|
|
|
|
r['type'] = t
|
|
|
|
r['length'] = int(l)
|
|
|
|
r['dec'] = int(d)
|
|
|
|
r['title'] = r['name']
|
|
|
|
ret = []
|
|
|
|
for r in recs:
|
2021-07-22 16:24:34 +08:00
|
|
|
r.update({'type':self.db2modelTypeMapping.get(r['type'].lower(),'text')})
|
2019-06-03 18:07:21 +08:00
|
|
|
r.update({'name':r['name'].lower()})
|
|
|
|
ret.append(r)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def fkSQL(self,tablename):
|
|
|
|
sqlcmd = ""
|
|
|
|
return sqlcmd
|
|
|
|
|
|
|
|
def fkeys(self,tablename):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def primary(self,tablename):
|
|
|
|
recs = self.fields(tablename)
|
|
|
|
ret = [ {'field':r['name']} for r in recs if r['pk'] == 1 ]
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def pkSQL(self,tablename):
|
|
|
|
sqlcmd = ""
|
|
|
|
return sqlcmd
|
2021-07-22 16:24:34 +08:00
|
|
|
|
|
|
|
def indexesSQL(self,tablename=None):
|
|
|
|
sqlcmd = """select * from sqlite_master
|
|
|
|
where lower(type) = 'index'
|
|
|
|
"""
|
|
|
|
if tablename:
|
|
|
|
sqlcmd += "and lower(tbl_name)='" + tablename.lower() + "' "
|
|
|
|
return sqlcmd
|