Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mod(key, _type, value)
- if value == nil then
- value = 1
- end
- return key .. _type .. value
- end
- function inc(value)
- return function(key)
- return mod(key, [[+]], value)
- end
- end
- function dec(value)
- return function(key)
- return mod(key, [[-]], value)
- end
- end
- Query = {}
- function Query:new()
- return setmetatable({type = [[]], columns = [[]], tables = [[]], conditions = {}}, {__index = self})
- end
- -- C
- function Query:insert_into(_table)
- self.type, self.columns, self.tables, self.conditions = [[insert]], [[]], [[`]] .. _table .. [[`]], {}
- return self
- end
- function Query:values(values)
- local _columns, _values = {}, {}
- for k, v in pairs(values) do
- table.insert(_columns, [[`]] .. k .. [[`]])
- if type(v) == "string" then
- v = [["]] .. v:gsub([[\"]], [[\"]]) .. [["]]
- end
- table.insert(_values, v)
- end
- self.columns, self.conditions = [[(]] .. table.concat(_columns, [[,]]) .. [[)]], _values
- return self
- end
- -- R
- function Query:select(...)
- self.type, self.columns, self.tables, self.conditions = [[select]], [[*]], [[]], {}
- local columns = {...}
- if #columns > 0 then
- for i, c in ipairs(columns) do
- columns[i] = [[`]] .. c .. [[`]]
- end
- self.columns = table.concat(columns, [[,]])
- end
- return self
- end
- -- U
- function Query:update(_table)
- self.type, self.columns, self.tables, self.conditions = [[update]], [[]], _table, {}
- return self
- end
- function Query:set(columns)
- local _columns = {}
- for k, v in pairs(columns) do
- k = [[`]] .. k:gsub([[\.]], [[`.`]]) .. [[`]]
- if type(v) == "string" then
- v = [["]] .. v:gsub([[\"]], [[\"]]) .. [["]]
- elseif type(v) == "function" then
- v = v(k)
- end
- table.insert(_columns, k .. [[=]] .. v)
- end
- self.columns = table.concat(_columns, [[,]])
- return self
- end
- -- D
- function Query:delete_from(_table)
- self.type, self.columns, self.tables, self.conditions = [[delete]], [[]], _table, {}
- return self
- end
- -- RU
- function Query:where(...)
- local params = {...}
- local column, comparison, value = [[`]] .. params[1]:gsub([[%.]], [[`.`]]) .. [[`]], [[=]], nil
- if #params == 2 then
- value = params[2]
- elseif #params == 3 then
- if params[2]:find("[<>]=") then
- comparison, value = params[2], params[3]
- else
- comparison = [[ ]] .. params[2] .. [[ ]]
- end
- else
- return false
- end
- if type(value) == "string" then
- value = [["]] .. value:gsub([[\"]], [[\"]]) .. [["]]
- end
- table.insert(self.conditions, column .. comparison .. value)
- return self
- end
- -- RD
- function Query:from(...)
- local tables = {...}
- for i, t in ipairs(tables) do
- tables[i] = [[`]] .. t .. [[`]]
- end
- self.tables = table.concat(tables, [[,]])
- return self
- end
- function Query:query()
- local query = [[]], {}
- if self.type == [[insert]] then
- query = [[INSERT INTO ]] .. self.tables .. self.columns .. [[VALUES(]] .. table.concat(self.conditions, [[,]]) .. [[)]]
- elseif self.type == [[select]] then
- local where = [[]]
- if self.conditions ~= [[]] then
- where = [[ WHERE ]] .. table.concat(self.conditions, [[ AND ]])
- end
- query = [[SELECT ]] .. self.columns .. [[ FROM ]] .. self.tables .. where
- elseif self.type == [[update]] then
- local where = [[]]
- if self.conditions ~= [[]] then
- where = table.concat({[[ WHERE]], table.concat(self.conditions, [[ AND ]])}, [[ ]])
- end
- query = [[UPDATE ]] .. self.tables .. [[ SET ]] .. self.columns .. where
- elseif self.type == [[delete]] then
- local where = [[]]
- if self.conditions ~= [[]] then
- where = table.concat({[[ WHERE]], table.concat(self.conditions, [[ AND ]])}, [[ ]])
- end
- query = [[DELETE FROM ]] .. self.tables .. where
- end
- return query .. [[;]]
- end
- function Query:execute()
- local query, result = self:query()
- if self.type == [[insert]] then
- local result = {}
- query = db.getResult(query)
- for k, v in pairs(query.val) do
- result[k] = v
- end
- return result
- end
- return db.executeQuery(query)
- end
- query = Query:new()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement