Advertisement
Streetboy_xD

[Computercraft] CCDB - WIP V0.63.24

May 12th, 2017
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.48 KB | None | 0 0
  1. database = {};
  2. database_mt = { __index = database };
  3. log_print = false;
  4.  
  5. -- Table: {column_name = {data}, column_name = {data}, ...}
  6. -- Config: {table = {column = {type, key}, column = {type, key}}, table = {column = {type, key}, column = {type, key}}, ...}
  7.  
  8. -- CREATE DATABASE dbname
  9. -- CREATE TABLE tablename (col, col, col)
  10. -- INSERT tablename (col, col, col) VALUES (val, val, val), (val, val, val), (val, val, val)
  11. -- SELECT * or colname FROM tablename [WHERE col='val']
  12. -- UPDATE
  13. -- DELETE ---- coming soon
  14. -- USE dbname
  15.  
  16. function database:new( ip, username, password )
  17. local self = setmetatable({}, database_mt);
  18. self.ip = ip;
  19. self.username = username;
  20. self.password = password;
  21. self.db_accesscode = '000000';
  22. self.used_db = "";
  23. self:create_empty_config( );
  24. return self;
  25. end
  26.  
  27. function database:create( name )
  28. fs.makeDir("databases/" .. name);
  29. self.used_db = name;
  30. end
  31.  
  32. function database:log_( console )
  33. logfile = fs.open(".temp/log.txt", "a");
  34. logfile.writeLine(console);
  35. logfile.close();
  36. if(log_print) then
  37. print(console);
  38. end
  39. end
  40.  
  41. function database:tr_command( command )
  42. local commands = {};
  43. local return_array = {};
  44. command = command:gsub(" ", "");
  45.  
  46. if(command:find(";")) then
  47. for i in command:gmatch("([^;]+)") do
  48. table.insert(commands, i);
  49. end
  50. else
  51. table.insert(commands, command);
  52. end
  53.  
  54. command = command:gsub(";", "");
  55.  
  56. for c = 1, #commands do
  57. local curCommand = commands[c];
  58. local CREATE = curCommand:find("CREATE");
  59. local INSERT = curCommand:find("INSERT");
  60. local SELECT = curCommand:find("SELECT");
  61. local UPDATE = curCommand:find("UPDATE");
  62. local DELETE = curCommand:find("DELETE");
  63. local USE = curCommand:find("USE");
  64. if(CREATE) then -- ************ CREATE FUNCTION **************
  65. local DATABASE = curCommand:find("DATABASE");
  66. local TABLE = curCommand:find("TABLE");
  67.  
  68. if(DATABASE) then
  69. local f, l = curCommand:find("DATABASE");
  70. local db_name = curCommand:sub(l+1, -1);
  71. self:create( db_name );
  72. table.insert(return_array, true);
  73. elseif(TABLE) then
  74. if(fs.exists("databases/" .. self.used_db) and self.used_db ~= "") then
  75. local f, l = curCommand:find("TABLE");
  76. local f_, l_ = curCommand:find("%(");
  77. local f__, l__ = curCommand:find("%)");
  78. local table_name = curCommand:sub(l+1, f_-1);
  79. local columns = curCommand:sub(l_+1, f__-1);
  80. local columns_ = {};
  81. local table_data = {};
  82. local config_data = {[table_name] = {}};
  83.  
  84. for i in columns:gmatch("([^,]+)") do
  85. table.insert(columns_, i);
  86. end
  87.  
  88. columns = columns_;
  89.  
  90. for i,v in pairs(columns) do
  91. table_data[v] = {};
  92. config_data[table_name][v] = {false,false};
  93. end
  94.  
  95. self:edit_table( table_name, table_data, false );
  96. self:edit_config( self.used_db, table_name, config_data );
  97. table.insert(return_array, true);
  98. else
  99. self:log_("database doesn't exists");
  100. table.insert(return_array, false);
  101. end
  102. end
  103. elseif(INSERT) then -- ************ INSERT FUNCTION **************
  104. if(fs.exists("databases/" .. self.used_db) and self.used_db ~= "") then
  105. local f, l = curCommand:find("INSERT");
  106. local f_, l_ = curCommand:find("%(");
  107. local f__, l__ = curCommand:find("%)");
  108. local f___, l___ = curCommand:find("VALUES");
  109. local table_name = curCommand:sub(l+1, f_-1);
  110. local columns = curCommand:sub(l_+1, f__-1);
  111. local values = curCommand:sub(l___+1, -1);
  112. local columns_ = {};
  113. local values_ = {};
  114. local split_values= {};
  115. local table_data = {};
  116.  
  117. for i in columns:gmatch("([^,]+)") do
  118. table.insert(columns_, i);
  119. end
  120.  
  121. for i in values:gmatch("%(([^%)]+)%)") do
  122. table.insert(values_, i);
  123. end
  124.  
  125. for j,v in pairs(columns_) do
  126. local cur = {};
  127. for i in values_[j]:gmatch("([^,]+)") do
  128. table.insert(cur, i);
  129. end
  130. table.insert(split_values, cur);
  131. end
  132.  
  133. values_ = split_values;
  134. table_data = self:read_table( table_name );
  135.  
  136. for i,v in pairs(columns_) do
  137. if(self:search_key_in_table( v, table_data )) then
  138. for j,k in pairs(values_[i]) do
  139. table.insert(table_data[v], k);
  140. end
  141. else
  142. self:log_("error with insert: " .. v);
  143. end
  144. end
  145.  
  146. self:edit_table( table_name, table_data, true );
  147. table.insert(return_array, true);
  148. end
  149. table.insert(return_array, false);
  150. elseif(SELECT) then -- ************ SELECT FUNCTION **************
  151. local WHERE = curCommand:find("WHERE");
  152. local f, l = curCommand:find("SELECT");
  153. local f_, l_ = curCommand:find("FROM");
  154. local columns = curCommand:sub(l+1, f_-1);
  155. local columns_ = {};
  156. local table_data = {};
  157. local return_arr = {};
  158.  
  159. if(WHERE) then
  160. local f__, l__ = curCommand:find("WHERE");
  161. local filter = curCommand:sub(l__+1, -1);
  162. local f___, l___ = filter:find("=");
  163. local filter_col = filter:sub(1,f___-1);
  164. local filter_val = filter:sub(l___+1, -1);
  165. local filter_vals = {};
  166. local table_name = curCommand:sub(l_+1, f__-1);
  167.  
  168. if(self:table_exists( table_name )) then
  169. table_data = self:read_table( table_name );
  170. filter_val = filter_val:gsub("'", "");
  171.  
  172. for i in filter_val:gmatch("([^,]+)") do
  173. table.insert(filter_vals, i);
  174. end
  175.  
  176. if(self:search_val_in_table( filter_col, table_data )) then
  177. if(columns == "*") then
  178. for index, value in pairs(table_data) do
  179. local ret_vals = {};
  180. for index_, value_ in pairs(value) do
  181. if(self:search_val_in_val_table( table_data[filter_col][index_], filter_vals )) then
  182. table.insert(ret_vals, value_);
  183. end
  184. end
  185. return_arr[index] = ret_vals;
  186. end
  187. table.insert(return_array, return_arr);
  188. else
  189. for i in columns:gmatch("([^,]+)") do
  190. table.insert(columns_, i);
  191. end
  192.  
  193. for index, value in pairs(table_data) do
  194. local ret_vals = {};
  195. if(self:search_val_in_val_table( index, columns_ )) then
  196. for index_, value_ in pairs(value) do
  197. if(self:search_val_in_val_table( table_data[filter_col][index_], filter_vals )) then
  198. table.insert(ret_vals, value_);
  199. end
  200. end
  201. return_arr[index] = ret_vals;
  202. end
  203. end
  204. table.insert(return_array, return_arr);
  205. end
  206. else
  207. table.insert(return_array, false);
  208. end
  209. end
  210. else
  211. local table_name = curCommand:sub(l_+1, -1);
  212.  
  213. if(self:table_exists( table_name )) then
  214. table_data = self:read_table( table_name );
  215.  
  216. if(columns == "*") then
  217. table.insert(return_array, table_data);
  218. else
  219. for i in columns:gmatch("([^,]+)") do
  220. table.insert(columns_, i);
  221. end
  222.  
  223. for i,v in pairs(columns_) do
  224. if(self:search_val_in_table( v, table_data )) then
  225. return_arr[v] = table_data[v];
  226. end
  227. end
  228.  
  229. table.insert(return_array, return_arr);
  230. end
  231. end
  232. end
  233. elseif(UPDATE) then -- ************ UPDATE FUNCTION **************
  234. local WHERE = curCommand:find("WHERE");
  235. local f, l = curCommand:find("UPDATE");
  236. local f_, l_ = curCommand:find("SET");
  237. local columns = curCommand:sub(l_+1, -1);
  238. local columns_ = {};
  239. local table_data = {};
  240. local return_arr = {};
  241.  
  242. if(WHERE) then
  243. local f__, l__ = curCommand:find("WHERE");
  244. local columns = curCommand:sub(l_+1, f__-1);
  245. local filter = curCommand:sub(l__+1, -1);
  246. local f___, l___ = filter:find("=");
  247. local filter_col = filter:sub(1,f___-1);
  248. local filter_val = filter:sub(l___+1, -1);
  249. local filter_vals = {};
  250. local table_name = curCommand:sub(l_+1, f__-1);
  251.  
  252. if(self:table_exists( table_name )) then
  253. table_data = self:read_table( table_name );
  254. filter_val = filter_val:gsub("'", "");
  255.  
  256. for i in filter_val:gmatch("([^,]+)") do
  257. table.insert(filter_vals, i);
  258. end
  259.  
  260. if(self:search_val_in_table( filter_col, table_data )) then
  261. for i in columns:gmatch("([^,]+)") do
  262. table.insert(columns_, i);
  263. end
  264.  
  265. for index, value in pairs(table_data) do
  266. local ret_vals = {};
  267. if(self:search_val_in_val_table( index, columns_ )) then
  268. for index_, value_ in pairs(value) do
  269. if(self:search_val_in_val_table( table_data[filter_col][index_], filter_vals )) then
  270. table.insert(ret_vals, value_);
  271. end
  272. end
  273. return_arr[index] = ret_vals;
  274. end
  275. end
  276. table.insert(return_array, return_arr);
  277. else
  278. table.insert(return_array, false);
  279. end
  280. end
  281. else
  282. local table_name = curCommand:sub(l_+1, -1);
  283.  
  284. if(self:table_exists( table_name )) then
  285. table_data = self:read_table( table_name );
  286.  
  287. if(columns == "*") then
  288. table.insert(return_array, table_data);
  289. else
  290. for i in columns:gmatch("([^,]+)") do
  291. table.insert(columns_, i);
  292. end
  293.  
  294. for i,v in pairs(columns_) do
  295. if(self:search_val_in_table( v, table_data )) then
  296. return_arr[v] = table_data[v];
  297. end
  298. end
  299.  
  300. table.insert(return_array, return_arr);
  301. end
  302. end
  303. end
  304. elseif(DELETE) then -- ************ DELETE FUNCTION **************
  305. local WHERE = curCommand:find("WHERE");
  306. local f, l = curCommand:find("DELETE");
  307. local f_, l_ = curCommand:find("FROM");
  308. local table_name = curCommand:sub(l_+1, -1);
  309. local table_data = {};
  310.  
  311. if(WHERE) then
  312. local f__, l__ = curCommand:find("WHERE");
  313. local AND = curCommand:find("AND");
  314. local filter = curCommand:sub(l__+1, -1);
  315. table_name = curCommand:sub(l_+1, f__-1);
  316. filter = filter:gsub("'", "");
  317.  
  318. if(AND) then
  319. -- AND noch Buggy, Funktion muss noch überarbeitet werden.
  320. local filter_vals = {};
  321. local to_delete = {};
  322. for _ in filter:gmatch("AND") do
  323. local _f,_l = filter:find("AND");
  324. local cur = filter:sub(1, _f-1);
  325. filter_vals[cur:sub(1,cur:find("=")-1)] = cur:sub(cur:find("=")+1,-1);
  326. filter = filter:sub(_l+1, -1);
  327. end
  328.  
  329. if(self:table_exists( table_name )) then
  330. table_data = self:read_table( table_name );
  331. local temp_table_data = table_data;
  332. for index, value in pairs(table_data) do
  333. for index_, value_ in pairs(value) do
  334. local filtered = false;
  335. for _i, _v in pairs(filter_vals) do
  336. if(self:search_val_in_val_table( table_data[_i][index_], filter_vals )) then
  337. filtered = true;
  338. else
  339. filtered = false;
  340. break;
  341. end
  342. end
  343.  
  344. if(filtered) then
  345. if( index == temp_table_data[1] ) then
  346. table.insert(to_delete,index_-#to_delete);
  347. end
  348. end
  349. end
  350. end
  351.  
  352. print(textutils.serialize(to_delete));
  353.  
  354. for _i, _v in pairs(temp_table_data) do
  355. for __i, __v in pairs(to_delete) do
  356. table.remove(temp_table_data[_i], __v);
  357. end
  358. end
  359.  
  360. self:edit_table( table_name, temp_table_data, true);
  361. table.insert(return_array, true);
  362. else
  363. table.insert(return_array, false);
  364. end
  365. else
  366. local f___, l___ = filter:find("=");
  367. local filter_col = filter:sub(1,f___-1);
  368. local filter_val = filter:sub(l___+1, -1);
  369. local to_delete = {};
  370.  
  371. if(self:table_exists( table_name )) then
  372. table_data = self:read_table( table_name );
  373. local temp_table_data = self:read_table( table_name );
  374. for index, value in pairs(table_data) do
  375. for index_, value_ in pairs(value) do
  376. if( table_data[filter_col][index_] == filter_val ) then
  377. if( index == filter_col ) then
  378. table.insert(to_delete,index_-#to_delete);
  379. end
  380. end
  381. end
  382. end
  383.  
  384. for _i, _v in pairs(temp_table_data) do
  385. for __i, __v in pairs(to_delete) do
  386. table.remove(temp_table_data[_i], __v);
  387. end
  388. end
  389.  
  390. self:edit_table( table_name, temp_table_data, true);
  391. table.insert(return_array, true);
  392. else
  393. table.insert(return_array, false);
  394. end
  395. end
  396. else
  397. local table_name = curCommand:sub(l_+1, -1);
  398.  
  399. self:edit_table( table_name, table_data, true);
  400. table.insert(return_array, true);
  401. end
  402. elseif(USE) then -- ************ USE FUNCTION **************
  403. local f, l = curCommand:find("USE");
  404. local db = curCommand:sub(l+1, -1);
  405. if(fs.exists("databases/" .. db)) then
  406. self.used_db = db;
  407. table.insert(return_array, true);
  408. else
  409. table.insert(return_array, false);
  410. end
  411. else
  412. self:log_("unkonwn command");
  413. table.insert(return_array, false);
  414. end
  415. end
  416. if(#commands == 1) then
  417. return return_array[1];
  418. end
  419. return return_array;
  420. end
  421.  
  422. function database:table_exists( table_name )
  423. if(fs.exists("databases/" .. self.used_db .. "/" .. table_name) and self.used_db ~= "") then
  424. return true;
  425. end
  426. return false;
  427. end
  428.  
  429. function database:search_key_in_table( key, table_ )
  430. if(table_ ~= nil) then
  431. for i, v in pairs(table_) do
  432. if key == i then
  433. return true;
  434. end
  435. end
  436. end
  437. return false;
  438. end
  439.  
  440. function database:search_val_in_table( val, table_ )
  441. if(table_ ~= nil) then
  442. for i, v in pairs(table_) do
  443. if val == i then
  444. return true;
  445. end
  446. end
  447. end
  448. return false;
  449. end
  450.  
  451. function database:search_val_in_val_table( val, table_ )
  452. if(table_ ~= nil) then
  453. for i, v in pairs(table_) do
  454. if val == v then
  455. return true;
  456. end
  457. end
  458. end
  459. return false;
  460. end
  461.  
  462. function database:create_empty_config( )
  463. if(not fs.exists("databases/config.ccsql")) then
  464. config = fs.open("databases/config.ccsql", "w");
  465. config.write("{}");
  466. config.close();
  467. end
  468. end
  469.  
  470. function database:read_table( table_name )
  471. table_file = fs.open("databases/" .. self.used_db .. "/" .. table_name, "r");
  472. table_data = textutils.unserialize(table_file.readAll());
  473. table_file.close();
  474. return table_data;
  475. end
  476.  
  477. function database:edit_table( table_name, data, overwrite )
  478. if(not fs.exists("databases/" .. self.used_db .. "/" .. table_name) or overwrite) then
  479. table_file = fs.open("databases/" .. self.used_db .. "/" .. table_name, "w");
  480. table_file.write(textutils.serialize(data));
  481. table_file.close();
  482. else
  483. self:log_("table already exists");
  484. end
  485. end
  486.  
  487. function database:edit_config( database_name, table_name, data )
  488. config_file = fs.open("databases/config.ccsql", "r");
  489. config_data = config_file.readAll();
  490. config_data = textutils.unserialize(config_data);
  491. config_file.close();
  492.  
  493. if(config_data[database_name] ~= "") then empty_arr = config_data[database_name] else empty_arr = {} end;
  494. empty_arr[table_name] = data[table_name];
  495. config_data[database_name] = empty_arr;
  496.  
  497. config_file = fs.open("databases/config.ccsql", "w");
  498. config_file.write(textutils.serialize(config_data));
  499. config_file.close();
  500. end
  501.  
  502. function database:refresh_accesscode( )
  503. while (true) do
  504. local accesscode = "";
  505. local gen_number = 0;
  506. for i = 1,6 do
  507. gen_number = math.random(0,9);
  508. accesscode = accesscode .. tostring(gen_number);
  509. end
  510. self.db_accesscode = accesscode;
  511.  
  512. sleep(300);
  513. end
  514. end
  515.  
  516. -- CCExpert 2016
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement