Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GraviDB API for databases
- -- Free to use by everybody
- -- Variables
- local standardPath = "databases"
- -- Functions
- ---- dbExists() returns true if database exists.
- function dbExists(dbName)
- if (fs.isDir(standardPath .. "/" .. dbName)) then
- return true
- else
- return false
- end
- end
- ---- tblExists() returns true if table in database exists.
- function tblExists(dbName, tblName)
- if(dbExists(dbName)) then
- if (fs.isDir(standardPath .. "/" .. dbName .. "/" .. tblName)) then
- return true
- else
- return false
- end
- else
- print("[ERROR] Invalid DB Name.")
- return false
- end
- end
- ---- createDB() creates a database with the supplied name.
- function createDB(dbName)
- if(not fs.isDir(standardPath)) then
- fs.makeDir(standardPath)
- end
- if(not dbExists(dbName)) then
- fs.makeDir(standardPath .. "/" .. dbName)
- else
- print("[ERROR] Database already exists.")
- end
- end
- ---- deleteDB() deletes the supplied database
- function deleteDB(dbName)
- if(dbExists(dbName)) then
- fs.delete(standardPath .. "/" .. dbName)
- end
- end
- ---- createTbl() creates a new table with the supplied name, inside the supplied database
- function createTbl(dbName, tblName)
- if(dbExists(dbName)) then
- if(not tblExists(dbName, tblName)) then
- fs.makeDir(standardPath .. "/" .. dbName .. "/" .. tblName)
- else
- print("[ERROR] Table already exists.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
- ---- renameTbl() renames an already existing table to the new supplied name in the supplied database
- function renameTbl(dbName, oldTblName, newTblName)
- if(dbExists(dbName)) then
- if(tblExists(dbName, oldTblName)) then
- if(not tblExists(dbName, newTblName)) then
- fs.move(standardPath .. "/" .. dbName .. "/" .. oldTblName, standardPath .. "/" .. dbName "/" .. newTblName)
- else
- print("[ERROR] Table with new name already exists.")
- end
- else
- print("[Error] Invalid old DB Name.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
- ---- deleteTbl() removes an already existing table within the supplied database
- function deleteTbl(dbName, tblName)
- if(dbExists(dbName)) then
- if(tblExists(dbName, tblName)) then
- fs.delete(standardPath .. "/" .. dbName .. "/" ..tblName)
- else
- print("[ERROR] Invalid Table Name.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
- ---- getEntries() returns the amount of keys in the supplied table in the supplied database
- function getEntries(dbName, tblName)
- if(dbExists(dbName)) then
- if(tblExists(dbName, tblName)) then
- keys = fs.list(standardPath .. "/" .. dbName .. "/" .. tblName)
- entries = 0
- for i in pairs(keys) do entries = entries + 1 end
- return entries
- else
- print("[ERROR] Invalid Table Name.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
- ---- set() sets the value of the supplied key, in the supplied table, in the supplied database
- function set(dbName, tblName, id, key, value)
- if(dbExists(dbName)) then
- if(tblExists(dbName, tblName)) then
- if(not fs.isDir(standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id)) then
- fs.makeDir(standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id)
- end
- keyFile = fs.open(standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id .. "/" .. key, "w")
- keyFile.write(value)
- keyFile.close()
- else
- print("[ERROR] Invalid Table Name.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
- ---- get() returns the value of the requested key
- function get(dbName, tblName, id, key)
- if(dbExists(dbName)) then
- if(tblExists(dbName, tblName)) then
- path = standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id .. "/" .. key
- if(fs.exists(path)) then
- keyFile = fs.open(path, "r")
- value = keyFile.readAll()
- keyFile.close()
- return value
- else
- return nil
- end
- else
- print("[ERROR] Invalid Table Name.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
- ---- delete() deletes the supplied id.
- function delete(dbName, tblName, id)
- if(dbExists(dbName)) then
- if(tblExists(dbName, tblName)) then
- path = standardPath .. "/" .. dbName .. "/" .. tblName "/" .. id
- if(fs.exists(path)) then
- fs.delete(path)
- end
- else
- print("[ERROR] Invalid Table Name.")
- end
- else
- print("[ERROR] Invalid DB Name.")
- end
- end
Add Comment
Please, Sign In to add comment