From 07c2848da3dd3212647845345d6430ebbcc5eee0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 11 Apr 2023 15:12:10 +0800 Subject: [PATCH] bugfix --- dataloader/dataloader.py | 37 ++++++++++++++++++------------------- sqlor/mysqlor.py | 2 +- sqlor/sor.py | 6 ++++-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/dataloader/dataloader.py b/dataloader/dataloader.py index e108d31..caaca43 100755 --- a/dataloader/dataloader.py +++ b/dataloader/dataloader.py @@ -1,21 +1,18 @@ import openpyxl as xlsx import asyncio +from sqlor.dbpools import DBPools class CBObject: - def __init__(self,name): + def __init__(self,db, name): + self.db = db 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 = { "int":int, @@ -23,7 +20,7 @@ typesconv = { "str":str, } -async def loadData(ws,callback): +async def getRecord(ws): names = [] types = [] for i,r in enumerate(ws.rows): @@ -40,21 +37,23 @@ async def loadData(ws,callback): for j,c in enumerate(r): tf = typesconv.get(types[j],None) v = c.value - if tf is not None: - v = tf(v) dic[names[j]] = v - await callback(dic) + yield rec async def excel2db(xlsxfile): wb = xlsx.load_workbook(xlsxfile) + dbname = [ i[2:-3] for i in wb.sheetnames if i.startswith('__')[0] for name in wb.sheetnames: + if name.startswith('__'): + continue ws = wb[name] - cbobj = CBObject(name) - await loadData(ws,cbobj.handle) + cbobj = CBObject(dbname, name) + await cbobj.handle(ws) if __name__ == '__main__': import sys - + config = getConfig() + DBPools(config.databases) if len(sys.argv) < 2: print('%s xlsxfile' % sys.argv[0]) sys.exit(1) diff --git a/sqlor/mysqlor.py b/sqlor/mysqlor.py index 3088771..f348ccb 100755 --- a/sqlor/mysqlor.py +++ b/sqlor/mysqlor.py @@ -147,7 +147,7 @@ limit $[from_line]$,$[rows]$""" return sqlcmd 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() return sqlcmd diff --git a/sqlor/sor.py b/sqlor/sor.py index 9d7f609..1fbc220 100755 --- a/sqlor/sor.py +++ b/sqlor/sor.py @@ -94,6 +94,7 @@ class SQLor(object): schemas = [] for t in tabs: primary = await self.primary(t.name) + # print('primary=', primary) indexes = concat_idx_info(await self.indexes(t.name)) fields = await self.fields(t.name) primary_fields = [f.field_name for f in primary] @@ -513,6 +514,7 @@ class SQLor(object): sqlstring = self.pkSQL(tablename) recs = [] await self.execute(sqlstring,{},lambda x:recs.append(x)) + # print('sql=', sqlstring, 'recs=', recs) return recs 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] c = [ '%s = ${%s}$' % (i,i) for i in condi ] u = [ '%s = ${%s}$' % (i,i) for i in newData ] - c_str = ','.join(c) + c_str = ' and '.join(c) u_str = ','.join(u) sql = 'update %s.%s set %s where %s' % (self.dbname, tablename, u_str,c_str) @@ -633,7 +635,7 @@ class SQLor(object): fields = [ i['name'] for i in desc['fields']] condi = [ i for i in desc['summary'][0]['primary']] 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) await self.runSQL({'sql_string':sql},ns,None)