Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tmpExt = ".dbtmp"
- dbExt = ".crdb"
- programData = "/system/data/crdb/dbtmp/"
- -- Function by Colandus
- local function trim(str)
- return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
- end
- local function explode(str, sep)
- local pos, t = 1, {}
- if #sep == 0 or #str == 0 then return end
- for s, e in function() return string.find(str, sep, pos) end do
- table.insert(t, trim(string.sub(str, pos, s-1)))
- pos = e+1
- end
- table.insert(t, trim(string.sub(str, pos)))
- return t
- end
- -- end Colandus function
- function ver()
- return 0.1
- end
- function loadDb(loadDbPath)
- function parseLine(strLine)
- if string.sub(strLine, 1, 2) == "@@" then
- curParsTable = string.sub(strLine, 3)
- tables[tablesCount] = curParsTable
- tablesCount = tablesCount + 1
- fs.makeDir(programData..curParsTable)
- elseif string.sub(strLine, 1, 2) == "##" then
- curParsCol = string.sub(strLine, 3)
- curColWrite = io.open(programData..curParsTable.."/"..curParsCol..tmpExt, "w")
- curColWrite:write("")
- elseif string.sub(strLine, 1, 2) == "*'" then
- curParsLn = string.sub(strLine, 3, (string.len(strLine) - 1))
- curColWrite:write(curParsLn.."\n")
- elseif string.sub(strLine, 1, 2) == "#!" then
- curColWrite:close()
- curParsCol = nil
- elseif string.sub(strLine, 1, 2) == "@!" then
- curParsTable = nil
- end
- end
- if string.find(loadDbPath, "%.") ~= nil then
- loadDbPath = string.sub(loadDbPath, 1,(string.find(loadDbPath, "%.")-1))
- curDb = fs.open(loadDbPath..dbExt, "r")
- tables = {}
- lineDb = {}
- curParsTable = nil
- curParsCol = nil
- local curLine = 1
- tablesCount = 1
- while true do
- loadLine = curDb.readLine()
- if loadLine == nil then
- break
- end
- parseLine(loadLine)
- curLine = curLine + 1
- end
- return true
- else
- return false
- end
- end
- function listDir(listDirPath)
- listDirArray = {}
- listFileArray = {}
- listDir_i = 1
- listFile_i = 1
- for listEv, listItem in ipairs(fs.list(listDirPath)) do
- if fs.isDir(listDirPath..listItem) then
- listDirArray[listDir_i] = listItem
- listDir_i = listDir_i + 1
- else
- listFileArray[listFile_i] = listItem
- listFile_i = listFile_i + 1
- end
- end
- return listDirArray, listFileArray
- end
- function closeDb(closeDbPath)
- function closeCompileNWrite()
- closeGetTables = listDir(programData)
- closeTmpToCompile = {}
- closeDbTree = {}
- closeTmpNil = nil
- if table.getn(closeGetTables) ~= 0 and table.getn(closeGetTables) ~= nil then
- for i=1, table.getn(closeGetTables) do
- closeTmpDb.writeLine("@@"..closeGetTables[i])
- closeTmpNil, closeDbCurCols = listDir(programData..closeGetTables[i])
- if table.getn(closeDbCurCols) ~= 0 and table.getn(closeDbCurCols) ~= nil then
- for i2=1, table.getn(closeDbCurCols) do
- closeDbCurCols[i2] = string.sub(closeDbCurCols[i2],1 , string.len(closeDbCurCols[i2]) - string.len(tmpExt))
- closeTmpDb.writeLine("##"..closeDbCurCols[i2])
- closeDbColCurFile = fs.open(programData..closeGetTables[i].."/"..closeDbCurCols[i2]..tmpExt, "r")
- while true do
- closeTmpLoadLn = closeDbColCurFile.readLine()
- if closeTmpLoadLn == nil then
- break
- end
- closeTmpDb.writeLine("*'"..closeTmpLoadLn.."'")
- end
- closeDbColCurFile.close()
- closeTmpDb.writeLine("#!"..closeDbCurCols[i2])
- end
- end
- closeTmpDb.writeLine("@!"..closeGetTables[i])
- end
- end
- return true
- end
- curDb.close()
- fs.delete(closeDbPath..tmpExt)
- closeTmpDb = fs.open(closeDbPath..tmpExt, "w")
- closeTmpDb.write("")
- closeCompileNWrite()
- closeTmpDb.close()
- fs.delete(closeDbPath..dbExt)
- fs.move(closeDbPath..tmpExt, closeDbPath..dbExt)
- fs.delete(programData)
- sleep(0.05)
- fs.makeDir(programData)
- end
- function queryDb(query)
- if query == nil then
- return false, false
- end
- queryData = explode(query, " | ")
- query = explode(queryData[1], " ")
- queryData = queryData[2]
- local queryCount = 1
- queryResult1 = {}
- queryLoadLn = {}
- queryResultTmp = nil
- queryCurFilePath = nil
- if string.upper(query[1]) == "SELECT" then
- if query[3] == nil then
- query[3] = ""
- end
- if string.upper(query[3]) == "FROM" then
- queryCurFilePath = programData..query[4].."/"..query[2]..tmpExt
- if fs.exists(programData..query[4]) then
- if fs.exists(queryCurFilePath) then
- queryCurFile = fs.open(queryCurFilePath, "r")
- while true do
- queryResultTmp = queryCurFile.readLine()
- if queryResultTmp == nil then
- break
- end
- queryResult1[queryCount] = queryResultTmp
- queryCount = queryCount + 1
- end
- queryCurFile.close()
- return queryResult1
- else
- return {false, "CRDB error: no column "..query[2].." in "..query[4]}
- end
- else
- return {false, "CRDB error: no table "..query[4]}
- end
- elseif query[3] == "" and query[2] ~= nil then
- queryCurFilePath = programData..query[2]
- if fs.exists(queryCurFilePath) then
- tmpSelectResult, queryResult1 = listDir(queryCurFilePath)
- for i=1, table.getn(queryResult1) do
- tmpExplode = explode(queryResult1[i], "%.")
- queryResult1[i] = tmpExplode[1]
- end
- return queryResult1
- else
- return {false, "CRDB error: no table "..query[2]}
- end
- end
- elseif string.upper(query[1]) == "INSERT" and string.upper(query[2]) == "INTO" then
- queryInsertArray = explode(query[3], ">")
- queryCurFilePath = programData..queryInsertArray[1].."/"..queryInsertArray[2]..tmpExt
- if queryData == nil then
- queryData = ""
- end
- if fs.exists(programData..queryInsertArray[1]) then
- if fs.exists(queryCurFilePath) then
- queryCurFile = fs.open(queryCurFilePath, "a")
- queryCurFile.writeLine(queryData)
- queryCurFile.close()
- return {true}
- else
- return {false, "CRDB error: no column "..queryInsertArray[2].." in "..queryInsertArray[1]}
- end
- else
- return {false, "CRDB error: no table "..queryInsertArray[1]}
- end
- elseif string.upper(query[1]) == "DELETE" and string.upper(query[2]) == "FROM" then
- if fs.exists(programData..query[3]) then
- if string.upper(query[4]) == "WHERE" then
- queryDeleteArray = explode(query[5], "=")
- if fs.exists(programData..query[3].."/"..queryDeleteArray[1]..tmpExt) then
- queryCurFile = fs.open(programData..query[3].."/"..queryDeleteArray[1]..tmpExt, "r")
- while true do
- queryLoadLnTmp = queryCurFile.readLine()
- if queryLoadLnTmp == nil then
- break
- end
- if queryLoadLnTmp ~= queryDeleteArray[2] then
- queryLoadLn[queryCount] = queryLoadLnTmp
- end
- queryCount = queryCount + 1
- end
- queryCurFile.close()
- queryCurFile = nil -- unload file
- fs.delete(programData..query[3].."/"..queryDeleteArray[1]..tmpExt)
- queryCurFile = io.open(programData..query[3].."/"..queryDeleteArray[1]..tmpExt, "w")
- queryCurFile:write("")
- for i=1, table.getn(queryLoadLn) do
- queryCurFile:write(queryLoadLn[i].."\n")
- end
- queryCurFile:close()
- return{true}
- else
- return {false, "CRDB error: no column "..queryDeleteArray[1].." in "..query[3], programData..query[3].."/"..queryDeleteArray[1]}
- end
- else
- return {false, "CRDB error: unknown query '"..query[4].."'"}
- end
- else
- return {false, "CRDB error: no table "..query[3]}
- end
- elseif string.upper(query[1]) == "SHOW" and string.upper(query[2]) == "TABLES" then
- queryResult1 = listDir(programData)
- return queryResult1
- else
- return {false, "CRDB error: unknown command"}
- end
- end
Add Comment
Please, Sign In to add comment