DEv0on

crdb

May 13th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.01 KB | None | 0 0
  1. tmpExt = ".dbtmp"
  2. dbExt = ".crdb"
  3. programData = "/system/data/crdb/dbtmp/"
  4. -- Function by Colandus
  5. local function trim(str)
  6.   return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
  7. end
  8.  
  9. local function explode(str, sep)
  10.   local pos, t = 1, {}
  11.   if #sep == 0 or #str == 0 then return end
  12.   for s, e in function() return string.find(str, sep, pos) end do
  13.     table.insert(t, trim(string.sub(str, pos, s-1)))
  14.     pos = e+1
  15.   end
  16.   table.insert(t, trim(string.sub(str, pos)))
  17.   return t
  18. end
  19. -- end Colandus function
  20.  
  21. function ver()
  22.   return 0.1
  23. end
  24.  
  25. function loadDb(loadDbPath)
  26.   function parseLine(strLine)
  27.     if string.sub(strLine, 1, 2) == "@@" then
  28.       curParsTable = string.sub(strLine, 3)
  29.       tables[tablesCount] = curParsTable
  30.       tablesCount = tablesCount + 1
  31.       fs.makeDir(programData..curParsTable)
  32.     elseif string.sub(strLine, 1, 2) == "##" then
  33.       curParsCol = string.sub(strLine, 3)
  34.       curColWrite = io.open(programData..curParsTable.."/"..curParsCol..tmpExt, "w")
  35.       curColWrite:write("")
  36.     elseif string.sub(strLine, 1, 2) == "*'" then
  37.       curParsLn = string.sub(strLine, 3, (string.len(strLine) - 1))
  38.       curColWrite:write(curParsLn.."\n")
  39.     elseif string.sub(strLine, 1, 2) == "#!" then
  40.       curColWrite:close()
  41.       curParsCol = nil
  42.     elseif string.sub(strLine, 1, 2) == "@!" then
  43.       curParsTable = nil
  44.     end
  45.   end
  46.  if string.find(loadDbPath, "%.") ~= nil then
  47.   loadDbPath = string.sub(loadDbPath, 1,(string.find(loadDbPath, "%.")-1))
  48.   curDb = fs.open(loadDbPath..dbExt, "r")
  49.   tables = {}
  50.   lineDb = {}
  51.   curParsTable = nil
  52.   curParsCol = nil
  53.   local curLine = 1
  54.   tablesCount = 1
  55.   while true do
  56.     loadLine = curDb.readLine()
  57.     if loadLine == nil then
  58.       break
  59.     end
  60.     parseLine(loadLine)
  61.     curLine = curLine + 1
  62.   end
  63.   return true
  64.  else
  65.   return false
  66.  end
  67. end
  68.  
  69. function listDir(listDirPath)
  70.   listDirArray = {}
  71.   listFileArray = {}
  72.   listDir_i = 1
  73.   listFile_i = 1
  74.   for listEv, listItem in ipairs(fs.list(listDirPath)) do
  75.     if fs.isDir(listDirPath..listItem) then
  76.       listDirArray[listDir_i] = listItem
  77.       listDir_i = listDir_i + 1
  78.     else
  79.       listFileArray[listFile_i] = listItem
  80.       listFile_i = listFile_i + 1
  81.     end
  82.   end
  83.   return listDirArray, listFileArray
  84. end
  85.  
  86. function closeDb(closeDbPath)
  87.   function closeCompileNWrite()
  88.     closeGetTables = listDir(programData)
  89.     closeTmpToCompile = {}
  90.     closeDbTree = {}
  91.     closeTmpNil = nil
  92.     if table.getn(closeGetTables) ~= 0 and table.getn(closeGetTables) ~= nil then
  93.       for i=1, table.getn(closeGetTables) do
  94.         closeTmpDb.writeLine("@@"..closeGetTables[i])
  95.         closeTmpNil, closeDbCurCols = listDir(programData..closeGetTables[i])
  96.         if table.getn(closeDbCurCols) ~= 0 and table.getn(closeDbCurCols) ~= nil then
  97.           for i2=1, table.getn(closeDbCurCols) do
  98.             closeDbCurCols[i2] = string.sub(closeDbCurCols[i2],1 , string.len(closeDbCurCols[i2]) - string.len(tmpExt))
  99.              closeTmpDb.writeLine("##"..closeDbCurCols[i2])
  100.             closeDbColCurFile = fs.open(programData..closeGetTables[i].."/"..closeDbCurCols[i2]..tmpExt, "r")
  101.             while true do
  102.               closeTmpLoadLn = closeDbColCurFile.readLine()
  103.               if closeTmpLoadLn == nil then
  104.                 break
  105.               end
  106.               closeTmpDb.writeLine("*'"..closeTmpLoadLn.."'")
  107.             end
  108.             closeDbColCurFile.close()
  109.             closeTmpDb.writeLine("#!"..closeDbCurCols[i2])
  110.           end
  111.         end
  112.         closeTmpDb.writeLine("@!"..closeGetTables[i])
  113.       end
  114.     end
  115.     return true
  116.   end
  117.  
  118.   curDb.close()
  119.   fs.delete(closeDbPath..tmpExt)
  120.   closeTmpDb = fs.open(closeDbPath..tmpExt, "w")
  121.   closeTmpDb.write("")
  122.   closeCompileNWrite()
  123.   closeTmpDb.close()
  124.   fs.delete(closeDbPath..dbExt)
  125.   fs.move(closeDbPath..tmpExt, closeDbPath..dbExt)
  126.   fs.delete(programData)
  127.   sleep(0.05)
  128.   fs.makeDir(programData)
  129. end
  130.  
  131. function queryDb(query)
  132.   if query == nil then
  133.     return false, false
  134.   end
  135.   queryData = explode(query, " | ")
  136.   query = explode(queryData[1], " ")
  137.   queryData = queryData[2]
  138.   local queryCount = 1
  139.   queryResult1 = {}
  140.   queryLoadLn = {}
  141.   queryResultTmp = nil
  142.   queryCurFilePath = nil
  143.   if string.upper(query[1]) == "SELECT" then
  144.     if query[3] == nil then
  145.       query[3] = ""
  146.     end
  147.     if string.upper(query[3]) == "FROM" then
  148.       queryCurFilePath = programData..query[4].."/"..query[2]..tmpExt
  149.       if fs.exists(programData..query[4]) then
  150.         if fs.exists(queryCurFilePath) then
  151.           queryCurFile = fs.open(queryCurFilePath, "r")
  152.           while true do
  153.             queryResultTmp = queryCurFile.readLine()
  154.               if queryResultTmp == nil then
  155.                 break
  156.               end
  157.             queryResult1[queryCount] = queryResultTmp
  158.             queryCount = queryCount + 1
  159.           end
  160.           queryCurFile.close()
  161.           return queryResult1
  162.         else
  163.           return {false, "CRDB error: no column "..query[2].." in "..query[4]}
  164.         end
  165.       else
  166.        return {false, "CRDB error: no table "..query[4]}
  167.       end
  168.     elseif query[3] == "" and query[2] ~= nil then
  169.       queryCurFilePath = programData..query[2]
  170.       if fs.exists(queryCurFilePath) then
  171.         tmpSelectResult, queryResult1 = listDir(queryCurFilePath)
  172.         for i=1, table.getn(queryResult1) do
  173.           tmpExplode = explode(queryResult1[i], "%.")
  174.           queryResult1[i] = tmpExplode[1]
  175.         end
  176.         return queryResult1
  177.       else
  178.         return {false, "CRDB error: no table "..query[2]}
  179.       end
  180.     end
  181.   elseif string.upper(query[1]) == "INSERT" and string.upper(query[2]) == "INTO" then
  182.     queryInsertArray = explode(query[3], ">")
  183.     queryCurFilePath = programData..queryInsertArray[1].."/"..queryInsertArray[2]..tmpExt
  184.     if queryData == nil then
  185.       queryData = ""
  186.     end
  187.     if fs.exists(programData..queryInsertArray[1]) then
  188.       if fs.exists(queryCurFilePath) then
  189.         queryCurFile = fs.open(queryCurFilePath, "a")
  190.         queryCurFile.writeLine(queryData)
  191.         queryCurFile.close()
  192.         return {true}
  193.       else
  194.         return {false, "CRDB error: no column "..queryInsertArray[2].." in "..queryInsertArray[1]}
  195.       end
  196.     else
  197.       return {false, "CRDB error: no table "..queryInsertArray[1]}
  198.     end
  199.   elseif string.upper(query[1]) == "DELETE" and string.upper(query[2]) == "FROM" then
  200.     if fs.exists(programData..query[3]) then
  201.       if string.upper(query[4]) == "WHERE" then
  202.         queryDeleteArray = explode(query[5], "=")
  203.         if fs.exists(programData..query[3].."/"..queryDeleteArray[1]..tmpExt) then
  204.           queryCurFile = fs.open(programData..query[3].."/"..queryDeleteArray[1]..tmpExt, "r")
  205.           while true do
  206.             queryLoadLnTmp = queryCurFile.readLine()
  207.             if queryLoadLnTmp == nil then
  208.               break
  209.             end
  210.             if queryLoadLnTmp ~= queryDeleteArray[2] then
  211.               queryLoadLn[queryCount] = queryLoadLnTmp
  212.             end
  213.             queryCount = queryCount + 1
  214.           end
  215.           queryCurFile.close()
  216.           queryCurFile = nil -- unload file
  217.           fs.delete(programData..query[3].."/"..queryDeleteArray[1]..tmpExt)
  218.           queryCurFile = io.open(programData..query[3].."/"..queryDeleteArray[1]..tmpExt, "w")
  219.           queryCurFile:write("")
  220.           for i=1, table.getn(queryLoadLn) do
  221.             queryCurFile:write(queryLoadLn[i].."\n")
  222.           end
  223.           queryCurFile:close()
  224.           return{true}
  225.         else
  226.           return {false, "CRDB error: no column "..queryDeleteArray[1].." in "..query[3], programData..query[3].."/"..queryDeleteArray[1]}
  227.         end
  228.       else
  229.         return {false, "CRDB error: unknown query '"..query[4].."'"}
  230.       end
  231.     else
  232.       return {false, "CRDB error: no table "..query[3]}
  233.     end
  234.   elseif string.upper(query[1]) == "SHOW" and string.upper(query[2]) == "TABLES" then
  235.     queryResult1 = listDir(programData)
  236.     return queryResult1
  237.   else
  238.     return {false, "CRDB error: unknown command"}
  239.   end
  240. end
Add Comment
Please, Sign In to add comment