bugfix
This commit is contained in:
parent
300e0745b0
commit
07c2848da3
@ -1,21 +1,18 @@
|
|||||||
import openpyxl as xlsx
|
import openpyxl as xlsx
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from sqlor.dbpools import DBPools
|
||||||
|
|
||||||
class CBObject:
|
class CBObject:
|
||||||
def __init__(self,name):
|
def __init__(self,db, name):
|
||||||
|
self.db = db
|
||||||
self.tbl = name
|
self.tbl = name
|
||||||
self.sql = ''
|
|
||||||
|
|
||||||
async def handle(self,dic):
|
|
||||||
names = ','.join(dic.keys())
|
|
||||||
vs = ["'%s'"%v if type(v)==type('') else str(v) for v in dic.values() ]
|
|
||||||
values = ','.join(vs)
|
|
||||||
self.sql = "insert into " + self.tbl + \
|
|
||||||
" (" + names + ") values (" + \
|
|
||||||
values + ");"
|
|
||||||
print(self.sql)
|
|
||||||
|
|
||||||
|
|
||||||
|
async def handle(self,ws):
|
||||||
|
db = DBPools()
|
||||||
|
meta = sor.I()
|
||||||
|
async with db.sqlorContext(self.db) as sor:
|
||||||
|
for rec in getRecord(ws):
|
||||||
|
sor.C(self.tbl, rec)
|
||||||
|
|
||||||
typesconv = {
|
typesconv = {
|
||||||
"int":int,
|
"int":int,
|
||||||
@ -23,7 +20,7 @@ typesconv = {
|
|||||||
"str":str,
|
"str":str,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def loadData(ws,callback):
|
async def getRecord(ws):
|
||||||
names = []
|
names = []
|
||||||
types = []
|
types = []
|
||||||
for i,r in enumerate(ws.rows):
|
for i,r in enumerate(ws.rows):
|
||||||
@ -40,21 +37,23 @@ async def loadData(ws,callback):
|
|||||||
for j,c in enumerate(r):
|
for j,c in enumerate(r):
|
||||||
tf = typesconv.get(types[j],None)
|
tf = typesconv.get(types[j],None)
|
||||||
v = c.value
|
v = c.value
|
||||||
if tf is not None:
|
|
||||||
v = tf(v)
|
|
||||||
dic[names[j]] = v
|
dic[names[j]] = v
|
||||||
await callback(dic)
|
yield rec
|
||||||
|
|
||||||
async def excel2db(xlsxfile):
|
async def excel2db(xlsxfile):
|
||||||
wb = xlsx.load_workbook(xlsxfile)
|
wb = xlsx.load_workbook(xlsxfile)
|
||||||
|
dbname = [ i[2:-3] for i in wb.sheetnames if i.startswith('__')[0]
|
||||||
for name in wb.sheetnames:
|
for name in wb.sheetnames:
|
||||||
|
if name.startswith('__'):
|
||||||
|
continue
|
||||||
ws = wb[name]
|
ws = wb[name]
|
||||||
cbobj = CBObject(name)
|
cbobj = CBObject(dbname, name)
|
||||||
await loadData(ws,cbobj.handle)
|
await cbobj.handle(ws)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
|
config = getConfig()
|
||||||
|
DBPools(config.databases)
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print('%s xlsxfile' % sys.argv[0])
|
print('%s xlsxfile' % sys.argv[0])
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -147,7 +147,7 @@ limit $[from_line]$,$[rows]$"""
|
|||||||
return sqlcmd
|
return sqlcmd
|
||||||
|
|
||||||
def pkSQL(self,tablename=None):
|
def pkSQL(self,tablename=None):
|
||||||
sqlcmd = """SELECT column_name as name FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` WHERE table_name='%s' AND constraint_name='PRIMARY'
|
sqlcmd = """SELECT distinct column_name as name FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` WHERE table_name='%s' AND constraint_name='PRIMARY'
|
||||||
""" % tablename.lower()
|
""" % tablename.lower()
|
||||||
return sqlcmd
|
return sqlcmd
|
||||||
|
|
||||||
|
@ -94,6 +94,7 @@ class SQLor(object):
|
|||||||
schemas = []
|
schemas = []
|
||||||
for t in tabs:
|
for t in tabs:
|
||||||
primary = await self.primary(t.name)
|
primary = await self.primary(t.name)
|
||||||
|
# print('primary=', primary)
|
||||||
indexes = concat_idx_info(await self.indexes(t.name))
|
indexes = concat_idx_info(await self.indexes(t.name))
|
||||||
fields = await self.fields(t.name)
|
fields = await self.fields(t.name)
|
||||||
primary_fields = [f.field_name for f in primary]
|
primary_fields = [f.field_name for f in primary]
|
||||||
@ -513,6 +514,7 @@ class SQLor(object):
|
|||||||
sqlstring = self.pkSQL(tablename)
|
sqlstring = self.pkSQL(tablename)
|
||||||
recs = []
|
recs = []
|
||||||
await self.execute(sqlstring,{},lambda x:recs.append(x))
|
await self.execute(sqlstring,{},lambda x:recs.append(x))
|
||||||
|
# print('sql=', sqlstring, 'recs=', recs)
|
||||||
return recs
|
return recs
|
||||||
|
|
||||||
async def fkeys(self,tablename):
|
async def fkeys(self,tablename):
|
||||||
@ -621,7 +623,7 @@ class SQLor(object):
|
|||||||
newData = [ i for i in ns.keys() if i not in condi and i in fields]
|
newData = [ i for i in ns.keys() if i not in condi and i in fields]
|
||||||
c = [ '%s = ${%s}$' % (i,i) for i in condi ]
|
c = [ '%s = ${%s}$' % (i,i) for i in condi ]
|
||||||
u = [ '%s = ${%s}$' % (i,i) for i in newData ]
|
u = [ '%s = ${%s}$' % (i,i) for i in newData ]
|
||||||
c_str = ','.join(c)
|
c_str = ' and '.join(c)
|
||||||
u_str = ','.join(u)
|
u_str = ','.join(u)
|
||||||
sql = 'update %s.%s set %s where %s' % (self.dbname, tablename,
|
sql = 'update %s.%s set %s where %s' % (self.dbname, tablename,
|
||||||
u_str,c_str)
|
u_str,c_str)
|
||||||
@ -633,7 +635,7 @@ class SQLor(object):
|
|||||||
fields = [ i['name'] for i in desc['fields']]
|
fields = [ i['name'] for i in desc['fields']]
|
||||||
condi = [ i for i in desc['summary'][0]['primary']]
|
condi = [ i for i in desc['summary'][0]['primary']]
|
||||||
c = [ '%s = ${%s}$' % (i,i) for i in condi ]
|
c = [ '%s = ${%s}$' % (i,i) for i in condi ]
|
||||||
c_str = ','.join(c)
|
c_str = ' and '.join(c)
|
||||||
sql = 'delete from %s.%s where %s' % (self.dbname, tablename,c_str)
|
sql = 'delete from %s.%s where %s' % (self.dbname, tablename,c_str)
|
||||||
await self.runSQL({'sql_string':sql},ns,None)
|
await self.runSQL({'sql_string':sql},ns,None)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user