gravitowl

GraviDB

Feb 1st, 2021 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. -- GraviDB API for databases
  2. -- Free to use by everybody
  3.  
  4. -- Variables
  5. local standardPath = "databases"
  6.  
  7. -- Functions
  8.  
  9. ---- dbExists() returns true if database exists.
  10. function dbExists(dbName)
  11. if (fs.isDir(standardPath .. "/" .. dbName)) then
  12. return true
  13. else
  14. return false
  15. end
  16. end
  17.  
  18. ---- tblExists() returns true if table in database exists.
  19. function tblExists(dbName, tblName)
  20. if(dbExists(dbName)) then
  21. if (fs.isDir(standardPath .. "/" .. dbName .. "/" .. tblName)) then
  22. return true
  23. else
  24. return false
  25. end
  26. else
  27. print("[ERROR] Invalid DB Name.")
  28. return false
  29. end
  30. end
  31.  
  32. ---- createDB() creates a database with the supplied name.
  33. function createDB(dbName)
  34. if(not fs.isDir(standardPath)) then
  35. fs.makeDir(standardPath)
  36. end
  37.  
  38. if(not dbExists(dbName)) then
  39. fs.makeDir(standardPath .. "/" .. dbName)
  40. else
  41. print("[ERROR] Database already exists.")
  42. end
  43. end
  44.  
  45. ---- deleteDB() deletes the supplied database
  46. function deleteDB(dbName)
  47. if(dbExists(dbName)) then
  48. fs.delete(standardPath .. "/" .. dbName)
  49. end
  50. end
  51.  
  52. ---- createTbl() creates a new table with the supplied name, inside the supplied database
  53. function createTbl(dbName, tblName)
  54. if(dbExists(dbName)) then
  55. if(not tblExists(dbName, tblName)) then
  56. fs.makeDir(standardPath .. "/" .. dbName .. "/" .. tblName)
  57. else
  58. print("[ERROR] Table already exists.")
  59. end
  60. else
  61. print("[ERROR] Invalid DB Name.")
  62. end
  63. end
  64.  
  65. ---- renameTbl() renames an already existing table to the new supplied name in the supplied database
  66. function renameTbl(dbName, oldTblName, newTblName)
  67. if(dbExists(dbName)) then
  68. if(tblExists(dbName, oldTblName)) then
  69. if(not tblExists(dbName, newTblName)) then
  70. fs.move(standardPath .. "/" .. dbName .. "/" .. oldTblName, standardPath .. "/" .. dbName "/" .. newTblName)
  71. else
  72. print("[ERROR] Table with new name already exists.")
  73. end
  74. else
  75. print("[Error] Invalid old DB Name.")
  76. end
  77. else
  78. print("[ERROR] Invalid DB Name.")
  79. end
  80. end
  81.  
  82. ---- deleteTbl() removes an already existing table within the supplied database
  83. function deleteTbl(dbName, tblName)
  84. if(dbExists(dbName)) then
  85. if(tblExists(dbName, tblName)) then
  86. fs.delete(standardPath .. "/" .. dbName .. "/" ..tblName)
  87. else
  88. print("[ERROR] Invalid Table Name.")
  89. end
  90. else
  91. print("[ERROR] Invalid DB Name.")
  92. end
  93. end
  94.  
  95. ---- getEntries() returns the amount of keys in the supplied table in the supplied database
  96. function getEntries(dbName, tblName)
  97. if(dbExists(dbName)) then
  98. if(tblExists(dbName, tblName)) then
  99. keys = fs.list(standardPath .. "/" .. dbName .. "/" .. tblName)
  100. entries = 0
  101.  
  102. for i in pairs(keys) do entries = entries + 1 end
  103. return entries
  104. else
  105. print("[ERROR] Invalid Table Name.")
  106. end
  107. else
  108. print("[ERROR] Invalid DB Name.")
  109. end
  110. end
  111.  
  112. ---- set() sets the value of the supplied key, in the supplied table, in the supplied database
  113. function set(dbName, tblName, id, key, value)
  114. if(dbExists(dbName)) then
  115. if(tblExists(dbName, tblName)) then
  116. if(not fs.isDir(standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id)) then
  117. fs.makeDir(standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id)
  118. end
  119.  
  120. keyFile = fs.open(standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id .. "/" .. key, "w")
  121. keyFile.write(value)
  122. keyFile.close()
  123. else
  124. print("[ERROR] Invalid Table Name.")
  125. end
  126. else
  127. print("[ERROR] Invalid DB Name.")
  128. end
  129. end
  130.  
  131. ---- get() returns the value of the requested key
  132. function get(dbName, tblName, id, key)
  133. if(dbExists(dbName)) then
  134. if(tblExists(dbName, tblName)) then
  135. path = standardPath .. "/" .. dbName .. "/" .. tblName .. "/" .. id .. "/" .. key
  136. if(fs.exists(path)) then
  137. keyFile = fs.open(path, "r")
  138. value = keyFile.readAll()
  139. keyFile.close()
  140. return value
  141. else
  142. return nil
  143. end
  144. else
  145. print("[ERROR] Invalid Table Name.")
  146. end
  147. else
  148. print("[ERROR] Invalid DB Name.")
  149. end
  150. end
  151.  
  152. ---- delete() deletes the supplied id.
  153. function delete(dbName, tblName, id)
  154. if(dbExists(dbName)) then
  155. if(tblExists(dbName, tblName)) then
  156. path = standardPath .. "/" .. dbName .. "/" .. tblName "/" .. id
  157. if(fs.exists(path)) then
  158. fs.delete(path)
  159. end
  160. else
  161. print("[ERROR] Invalid Table Name.")
  162. end
  163. else
  164. print("[ERROR] Invalid DB Name.")
  165. end
  166. end
Add Comment
Please, Sign In to add comment