local fieldName, value = reader:GetName(i), nil; localtype = reader:GetFieldType(i); type = tostring(type) iftype == "System.Int64"ortype == "System.Int32"ortype == "System.Int16"then value = tonumber(tostring(reader:get_Item(fieldName))) elseiftype == "System.Float"then value = tonumber(tostring(reader:GetFloat(i))) elseiftype == "System.Double"then value = tonumber(tostring(reader:GetDouble(i))) elseiftype == "System.Decimal"then value = tonumber(tostring(reader:GetDecimal(i))) elseiftype == "System.Boolean"then value = tostring(reader:GetBoolean(i))=="true"andtrueorfalse elseiftype == "System.String"then value = tostring(reader:get_Item(fieldName) if"[type]:" == string.sub(value, 1, 7) then value = GameUtils.json.decode(string.sub(value, 8)) end end
b.cvs2db数据解析导入工具
sqlite清空表不支持TRUNCATE TABLE语句,需用DELETE FROM TableName。
local M = class("SqliteMonoMgr") local TInt64 = "Int64" local TInt32 = "Int32" local TInt16 = "Int16" local TFloat = "Float" local TDouble = "Double" local TDecimal = "Decimal" local TBoolean = "Boolean" local TString = "String" local TArrayPrefix = "[type]:" functionM:loadConfigTable(tableName) local ret,reader = true xpcall(function() local pks = self:_getPrimaryKeys(tableName) reader = self._sqliteManager:SQLSelect(tableName) if reader andnot reader.IsClosed then self._cache[tableName] = self:_readConfigTable(reader, pks) self._loadedTable[tableName] = true reader:Close() else ret = false printWarn("not such table "..tableName) end end, function(err) ret = false if reader then reader:Close() end printError(err) -- self:close() end) return ret end functionM:_readConfigTable(reader, pks) local configTable = {} ifnot reader.HasRows then return configTable end local dataList, title = self:_readDataListAndTitle(reader) configTable.dataList = dataList -- 按索引构造字典 local pkCount,temp = #pks,nil if pkCount>0then for i,v inipairs(dataList) do temp = configTable for i=1,pkCount,1do local pk = pks[i] local pv = v[pk] if i==pkCount then temp[pv] = v break end ifnot temp[pv] then temp[pv] = {} end temp = temp[pv] end end end return configTable end functionM:_readDataListAndTitle(reader) local title = {} local dataList = {} while reader:Read() do local visibleFieldCount, row = reader.VisibleFieldCount, {} for i = 0, visibleFieldCount - 1do local fieldName, value = reader:GetName(i), nil; title[fieldName] = i+1 localtype = reader:GetFieldType(i); type = string.sub(tostring(type), 8) iftype == TInt64 ortype == TInt32 ortype == TInt16 then value = tonumber(tostring(reader:get_Item(fieldName))) elseiftype == TFloat then value = tonumber(tostring(reader:GetFloat(i))) elseiftype == TDouble then value = tonumber(tostring(reader:GetDouble(i))) elseiftype == TDecimal then value = tonumber(tostring(reader:GetDecimal(i))) elseiftype == TBoolean then value = tostring(reader:GetBoolean(i))=="true"andtrueorfalse elseiftype == TString then -- value = reader:GetString(i) value = tostring(reader:get_Item(fieldName)) if TArrayPrefix == string.sub(value, 1, 7) then value = GameUtils.json.decode(string.sub(value, 8)) end end table.insert(row, value) end table.insert(dataList, row) end -- 构造元表 local mt = { __index = function(t, key) local index = title[key] if index then returnrawget(t, index) end end } for i,v inipairs(dataList) do setmetatable(v, mt) end return dataList, title end functionM:_getPrimaryKeys(tableName) ifself._tablePrimaryKeyCache[tableName] then returnself._tablePrimaryKeyCache[tableName] end local pks = {} local reader = self._sqliteManager:ExecuteReader("PRAGMA table_info("..tableName..")") if reader then while reader:Read() do local isPk = reader:GetBoolean(reader:GetOrdinal("pk")) if isPk then table.insert(pks, reader:get_Item("name")) end end reader:Close() end self._tablePrimaryKeyCache[tableName] = pks return pks end
local configDictMetaTable = { __index = function(t, key) ifnotrawget(t, key) then print("sqlite 加载表:" .. key) local configTable = SqliteMonoMgr.instance:findAll(key) rawset(t, key, configTable) return configTable end end } ConfigMgr.instance.requestConfig = function(self, name, configObj) local mt = getmetatable(configObj._dict) if mt ~= configDictMetaTable then setmetatable(configObj._dict, configDictMetaTable) end local isExistInDB = SqliteMonoMgr.instance:isExistTable(name) ifself._configDict[name] then local content = self._configDict[name] configObj:handleConfig(name, content) else local content ifnot isExistInDB then localpath = self._pathPrefix .. name content = require(path) self._configDict[name] = content end configObj:handleConfig(name, content) end end
local M = class("SqliteMonoMgr") -- 根据id查询 -- @return 返回主键查询结果,只有一条数据返回数据本身,多条数据返回table(多个主键的情况) functionM:findById(tableName, value, pk) if GameUtils.isEmptyString(tableName) then printError("tableName is nil.") return end local t = os.clock() if pk==nilthen pk = "id" end ifself._cache[tableName] andself._cache[tableName][value] then print("findById 命中缓存耗时:"..os.clock()-t, tableName, value, pk) returnself._cache[tableName][value] end local reader = self._sqliteManager:SQLSelect(tableName, "*", string.format("where %s=%s", pk, value)) if reader andnot reader.IsClosed then local dataList = self:_readDataListAndTitle(reader) reader:Close() ifnotself._cache[tableName] then self._cache[tableName] = {} end if #dataList==1then self._cache[tableName][value] = dataList[1] print("findById 耗时:"..os.clock()-t, tableName, value, pk) return dataList[1] else print("findById 耗时:"..os.clock()-t, tableName, value, pk) self._cache[tableName][value] = dataList return dataList end end end -- 根据条件查询 -- @return 返回table functionM:findAll(tableName, ...) local t = os.clock() local conditions = {...} if #conditions==0then ifnotself._loadedTable[tableName] then self:loadConfigTable(tableName) end print("findAll 耗时:"..os.clock()-t, tableName) returnself._cache[tableName] end local conditionStr = "where" for i=1,#conditions,2do local key = conditions[i] local value = conditions[i+1] conditionStr = conditionStr..string.format(" %s=%s and", key, value) end conditionStr = string.sub(conditionStr, 1, -4) print("sql conditionStr:", conditionStr) local reader = self._sqliteManager:SQLSelect(tableName, "*", conditionStr) if reader andnot reader.IsClosed then local dataList = self:_readDataListAndTitle(reader) reader:Close() print("findAll 耗时:"..os.clock()-t, tableName) return dataList end end -- 通过输入sql查询 functionM:findAllBySql(sql) ifstring.isEmptyString(sql) thenreturnend local t = os.clock() local reader = self._sqliteManager:ExecuteReader(sql) if reader andnot reader.IsClosed then local dataList = self:_readDataListAndTitle(reader) reader:Close() print("findAllBySql 耗时:"..os.clock()-t) return dataList end end