Advertisement
ranisalt

Lua SQL

Jan 28th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. function mod(key, _type, value)
  2.     if value == nil then
  3.         value = 1
  4.     end
  5.     return key .. _type .. value
  6. end
  7.  
  8. function inc(value)
  9.     return function(key)
  10.         return mod(key, [[+]], value)
  11.     end
  12. end
  13.  
  14. function dec(value)
  15.     return function(key)
  16.         return mod(key, [[-]], value)
  17.     end
  18. end
  19.  
  20. Query = {}
  21.  
  22. function Query:new()
  23.     return setmetatable({type = [[]], columns = [[]], tables = [[]], conditions = {}}, {__index = self})
  24. end
  25.  
  26. -- C
  27. function Query:insert_into(_table)
  28.     self.type, self.columns, self.tables, self.conditions = [[insert]], [[]], [[`]] .. _table .. [[`]], {}
  29.     return self
  30. end
  31.  
  32. function Query:values(values)
  33.     local _columns, _values = {}, {}
  34.     for k, v in pairs(values) do
  35.         table.insert(_columns, [[`]] .. k .. [[`]])
  36.         if type(v) == "string" then
  37.             v = [["]] .. v:gsub([[\"]], [[\"]]) .. [["]]
  38.         end
  39.         table.insert(_values, v)
  40.     end
  41.     self.columns, self.conditions = [[(]] .. table.concat(_columns, [[,]]) .. [[)]], _values
  42.     return self
  43. end
  44.  
  45. -- R
  46. function Query:select(...)
  47.     self.type, self.columns, self.tables, self.conditions = [[select]], [[*]], [[]], {}
  48.     local columns = {...}
  49.     if #columns > 0 then
  50.         for i, c in ipairs(columns) do
  51.             columns[i] = [[`]] .. c .. [[`]]
  52.         end
  53.         self.columns = table.concat(columns, [[,]])
  54.     end
  55.     return self
  56. end
  57.  
  58. -- U
  59. function Query:update(_table)
  60.     self.type, self.columns, self.tables, self.conditions = [[update]], [[]], _table, {}
  61.     return self
  62. end
  63.  
  64. function Query:set(columns)
  65.     local _columns = {}
  66.     for k, v in pairs(columns) do
  67.         k = [[`]] .. k:gsub([[\.]], [[`.`]]) .. [[`]]
  68.         if type(v) == "string" then
  69.             v = [["]] .. v:gsub([[\"]], [[\"]]) .. [["]]
  70.         elseif type(v) == "function" then
  71.             v = v(k)
  72.         end
  73.         table.insert(_columns, k .. [[=]] .. v)
  74.     end
  75.     self.columns = table.concat(_columns, [[,]])
  76.     return self
  77. end
  78.  
  79. -- D
  80. function Query:delete_from(_table)
  81.     self.type, self.columns, self.tables, self.conditions = [[delete]], [[]], _table, {}
  82.     return self
  83. end
  84.  
  85. -- RU
  86. function Query:where(...)
  87.     local params = {...}
  88.     local column, comparison, value = [[`]] .. params[1]:gsub([[%.]], [[`.`]]) .. [[`]], [[=]], nil
  89.     if #params == 2 then
  90.         value = params[2]
  91.     elseif #params == 3 then
  92.         if params[2]:find("[<>]=") then
  93.             comparison, value = params[2], params[3]
  94.         else
  95.             comparison = [[ ]] .. params[2] .. [[ ]]
  96.         end
  97.     else
  98.         return false
  99.     end
  100.     if type(value) == "string" then
  101.         value = [["]] .. value:gsub([[\"]], [[\"]]) .. [["]]
  102.     end
  103.     table.insert(self.conditions, column .. comparison .. value)
  104.     return self
  105. end
  106.  
  107. -- RD
  108. function Query:from(...)
  109.     local tables = {...}
  110.     for i, t in ipairs(tables) do
  111.         tables[i] = [[`]] .. t .. [[`]]
  112.     end
  113.     self.tables = table.concat(tables, [[,]])
  114.     return self
  115. end
  116.  
  117. function Query:query()
  118.     local query = [[]], {}
  119.     if self.type == [[insert]] then
  120.         query = [[INSERT INTO ]] .. self.tables .. self.columns .. [[VALUES(]] .. table.concat(self.conditions, [[,]]) .. [[)]]
  121.     elseif self.type == [[select]] then
  122.         local where = [[]]
  123.         if self.conditions ~= [[]] then
  124.             where = [[ WHERE ]] .. table.concat(self.conditions, [[ AND ]])
  125.         end
  126.         query = [[SELECT ]] .. self.columns .. [[ FROM ]] .. self.tables .. where
  127.     elseif self.type == [[update]] then
  128.         local where = [[]]
  129.         if self.conditions ~= [[]] then
  130.             where = table.concat({[[ WHERE]], table.concat(self.conditions, [[ AND ]])}, [[ ]])
  131.         end
  132.         query = [[UPDATE ]] .. self.tables .. [[ SET ]] .. self.columns .. where
  133.     elseif self.type == [[delete]] then
  134.         local where = [[]]
  135.         if self.conditions ~= [[]] then
  136.             where = table.concat({[[ WHERE]], table.concat(self.conditions, [[ AND ]])}, [[ ]])
  137.         end
  138.         query = [[DELETE FROM ]] .. self.tables .. where
  139.     end
  140.     return query .. [[;]]
  141. end
  142.  
  143. function Query:execute()
  144.     local query, result = self:query()
  145.     if self.type == [[insert]] then
  146.         local result = {}
  147.         query = db.getResult(query)
  148.         for k, v in pairs(query.val) do
  149.             result[k] = v
  150.         end
  151.         return result
  152.     end
  153.     return db.executeQuery(query)
  154. end
  155.  
  156. query = Query:new()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement