Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Start of db API
- local args={...}
- _G.dbDebug=false
- dbfolder="flatcraftsql"
- _G.dbname=""
- dbpath=""
- table=""
- dbtable=""
- if(args[1]=="help") then help() end
- function help()
- print("help1")
- end
- --Basic function
- function setdb(dbname)
- dbpath=dbfolder.."/"..dbname
- --if(dbDebug==true) then print("Db is ",dbpath) end
- --if(fs.isDir(dbpath)~=true) then error("No scuh db ",dbname)
- --return nil
- --end
- --dbname=dbpath
- end
- function setdbtable(table)
- --if(dbDebug==true) then print("table is",table," dbpath: ",dbpath) end
- dbtable=dbpath.."/"..table..".tab"
- --if(fs.exists(dbtable)~=true) then error("No scuh table ",table)
- --return nil
- --end
- end
- function createdb(dbname)
- setdb(dbname)
- if(fs.isDir(dbpath)==true) then
- if(dbDebug==true) then print("DB ",dbname," alrady exists") end
- return false
- else
- fs.makeDir(dbpath)
- if(dbDebug==true) then print("DB ",dbpath, " was created") end
- return true
- end
- end
- function deletedb(dbname)
- setdb(dbname)
- if(fs.isDir(dbpath)==true) then
- fs.delete(dbpath)
- if(dbDebug==true) then print("DB ", dbname," was removed") end
- return true
- else
- if(dbDebug==true) then print("No DB ", dbname," was found") end
- return false
- end
- end
- function createtable(dbname,table)
- setdb(dbname)
- setdbtable(table)
- if(fs.isDir(dbpath)==true) then
- if(fs.exists(dbtable)) then
- if(dbDebug==true) then print("Table ",table," alrady exists") end
- return false
- else
- local htable = fs.open(dbtable, "w")
- htable.close()
- if(dbDebug==true) then print("Table ",dbtable," created") end
- return true
- end
- else
- if(dbDebug==true) then print("Create DB first !!!") end
- return false
- end
- end
- function deletetable(dbname,table)
- setdb(dbname)
- setdbtable(table)
- if(fs.isDir(dbpath)==true) then
- if(fs.exists(dbtable)) then
- fs.delete(table)
- if(dbDebug==true) then print("Table ",table," was deleted!!!") end
- return true
- else
- if(dbDebug==true) then print("No such table ",table," ???") end
- return false
- end
- else
- if(dbDebug==true) then print("No such DB !!!") end
- return false
- end
- end
- function connect(dbname)
- setdb(dbname)
- if(fs.isDir(dbpath)==true) then
- if(dbDebug==true) then print("DB ",dbname," found") end
- return true
- else
- if(dbDebug==true) then print("DB ",dbname," not found") end
- return false
- end
- end
- function dis()
- --lest save
- dbname=nil
- dbtable=nil
- setdb(dbname)
- setdbtable(table)
- end
- -------------------------------------
- --Data functions
- --Global data/bases multi array
- _G.dbases={}
- --_G.dbtabels={}
- function loaddbs()
- --set
- end
- function loaddb(dbname)
- setdb(dbname)
- local tabels=(fs.find(dbpath.."/*.tab"))
- this_db={}
- for key,value in pairs(tabels) do print(key, ": ", value) end
- for key,value in pairs(tabels) do
- hRead = fs.open(value, "r")
- fC = hRead.readAll()
- hRead.close()
- fCx=textutils.unserialize(fC)
- --print("DB: ",dbpath)
- key1=string.gsub(value,dbpath.."/","")
- key1=string.gsub(key1,".tab","")
- --print("K:", value)
- --print("Klucz: ",key1)
- this_db[key1]=fCx
- end
- return this_db
- end
- function savedb(dbname)
- end
- function loadtable(table,dbname)
- setdb(dbname)
- setdbtable(table)
- hRead = fs.open(dbtable, "r")
- fC = hRead.readAll()
- hRead.close()
- this_table=textutils.unserialize(fC)
- return this_table
- end
- function savetable(this_table,settable,dbname)
- settable=settable or this_table
- setdb(dbname)
- setdbtable(settable)
- result=textutils.serialize(this_table)
- result = string.gsub(result, "\n", "") -- remove line breaks
- result=string.gsub(result," ","")
- result=string.gsub(result,"{{","{\n{")
- result=string.gsub(result,",},","},\n")
- hWrite = fs.open(dbtable, "w")
- hWrite.writeLine(result)
- hWrite.close()
- end
- function record()
- end
- --------------------------------------
- --Operations
- function getId(table,cond)
- end
- function select(table,cond)
- end
- function insert(table,data)
- end
- function delete(table,cond,id)
- end
- function update(table,id,data)
- end
- function clean(table)
- end
- -------------------------------------
- --copy backup
- function makecopy(path,table,dbname)
- end
Advertisement
Add Comment
Please, Sign In to add comment