MrNice007

Lua db

Jun 21st, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. --Start of db API
  2. local args={...}
  3. _G.dbDebug=false
  4. dbfolder="flatcraftsql"
  5. _G.dbname=""
  6. dbpath=""
  7. table=""
  8. dbtable=""
  9.  
  10.  
  11. if(args[1]=="help") then help() end
  12. function help()
  13. print("help1")
  14. end
  15.  
  16. --Basic function
  17.  
  18. function setdb(dbname)
  19. dbpath=dbfolder.."/"..dbname
  20. --if(dbDebug==true) then print("Db is ",dbpath) end
  21. --if(fs.isDir(dbpath)~=true) then error("No scuh db ",dbname)
  22. --return nil
  23. --end
  24. --dbname=dbpath
  25. end
  26.  
  27. function setdbtable(table)
  28. --if(dbDebug==true) then print("table is",table," dbpath: ",dbpath) end
  29. dbtable=dbpath.."/"..table..".tab"
  30. --if(fs.exists(dbtable)~=true) then error("No scuh table ",table)
  31. --return nil
  32. --end
  33. end
  34.  
  35.  
  36. function createdb(dbname)
  37. setdb(dbname)
  38. if(fs.isDir(dbpath)==true) then
  39.  if(dbDebug==true) then print("DB ",dbname," alrady exists") end
  40.  return false
  41. else
  42.  fs.makeDir(dbpath)
  43.  if(dbDebug==true) then print("DB ",dbpath, " was created") end
  44.  return true
  45. end
  46. end
  47.  
  48. function deletedb(dbname)
  49. setdb(dbname)
  50. if(fs.isDir(dbpath)==true) then
  51. fs.delete(dbpath)
  52. if(dbDebug==true) then print("DB ", dbname," was removed") end
  53. return true
  54. else
  55. if(dbDebug==true) then print("No DB ", dbname," was found") end
  56. return false
  57. end
  58. end
  59.  
  60. function createtable(dbname,table)
  61. setdb(dbname)
  62. setdbtable(table)
  63. if(fs.isDir(dbpath)==true) then
  64.  if(fs.exists(dbtable)) then
  65.  if(dbDebug==true) then print("Table ",table," alrady exists") end
  66.  return false
  67. else
  68.  local htable = fs.open(dbtable, "w")
  69.  htable.close()
  70.  if(dbDebug==true) then print("Table ",dbtable," created") end
  71.  return true
  72.  end
  73. else
  74.  if(dbDebug==true) then print("Create DB first !!!") end
  75.  return false
  76. end
  77. end
  78.  
  79. function deletetable(dbname,table)
  80. setdb(dbname)
  81. setdbtable(table)
  82. if(fs.isDir(dbpath)==true) then
  83.  if(fs.exists(dbtable)) then
  84.  fs.delete(table)
  85.  if(dbDebug==true) then print("Table ",table," was deleted!!!") end
  86.  return true
  87. else
  88.  if(dbDebug==true) then print("No such table ",table," ???") end
  89.  return false
  90.  end
  91. else
  92.  if(dbDebug==true) then print("No such DB !!!") end
  93.  return false
  94. end
  95. end
  96.  
  97. function connect(dbname)
  98. setdb(dbname)
  99. if(fs.isDir(dbpath)==true) then
  100. if(dbDebug==true) then print("DB ",dbname," found") end
  101. return true
  102. else
  103.  if(dbDebug==true) then print("DB ",dbname," not found") end
  104.  return false
  105. end
  106. end
  107.  
  108. function dis()
  109. --lest save
  110. dbname=nil
  111. dbtable=nil
  112. setdb(dbname)
  113. setdbtable(table)
  114. end
  115.  
  116. -------------------------------------
  117. --Data functions
  118. --Global data/bases multi array
  119. _G.dbases={}
  120. --_G.dbtabels={}
  121.  
  122. function loaddbs()
  123. --set
  124. end
  125.  
  126. function loaddb(dbname)
  127. setdb(dbname)
  128. local tabels=(fs.find(dbpath.."/*.tab"))
  129. this_db={}
  130. for key,value in pairs(tabels) do print(key, ": ", value) end
  131. for key,value in pairs(tabels) do
  132. hRead = fs.open(value, "r")
  133. fC = hRead.readAll()
  134. hRead.close()
  135. fCx=textutils.unserialize(fC)
  136. --print("DB: ",dbpath)
  137. key1=string.gsub(value,dbpath.."/","")
  138. key1=string.gsub(key1,".tab","")
  139. --print("K:", value)
  140. --print("Klucz: ",key1)
  141. this_db[key1]=fCx
  142. end
  143. return this_db
  144. end
  145.  
  146. function savedb(dbname)
  147. end
  148.  
  149. function loadtable(table,dbname)
  150. setdb(dbname)
  151. setdbtable(table)
  152. hRead = fs.open(dbtable, "r")
  153. fC = hRead.readAll()
  154. hRead.close()
  155. this_table=textutils.unserialize(fC)
  156. return this_table
  157. end
  158.  
  159. function savetable(this_table,settable,dbname)
  160. settable=settable or this_table
  161. setdb(dbname)
  162. setdbtable(settable)
  163. result=textutils.serialize(this_table)
  164. result = string.gsub(result, "\n", "") -- remove line breaks
  165. result=string.gsub(result," ","")
  166. result=string.gsub(result,"{{","{\n{")
  167. result=string.gsub(result,",},","},\n")
  168. hWrite = fs.open(dbtable, "w")
  169. hWrite.writeLine(result)
  170. hWrite.close()
  171. end
  172.  
  173. function record()
  174. end
  175.  
  176. --------------------------------------
  177. --Operations
  178.  
  179. function getId(table,cond)
  180. end
  181.  
  182. function select(table,cond)
  183. end
  184.  
  185. function insert(table,data)
  186. end
  187.  
  188. function delete(table,cond,id)
  189. end
  190.  
  191. function update(table,id,data)
  192. end
  193.  
  194. function clean(table)
  195. end
  196.  
  197. -------------------------------------
  198. --copy backup
  199.  
  200. function makecopy(path,table,dbname)
  201. end
Advertisement
Add Comment
Please, Sign In to add comment