forked from tomiichx/tomic_querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.lua
More file actions
49 lines (40 loc) · 1.07 KB
/
Input.lua
File metadata and controls
49 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---@class Input
Input = setmetatable({}, Input)
Input.__name = "Input"
Input.__index = Input
---Sanitize a string value
---@param value string The value to sanitize
---@return string sanitized The sanitized value
function Input:sanitize(value)
if type(value) ~= 'string' then
return value
end
value = value:gsub('[%c]', '')
value = value:gsub('\'', '\'\'')
value = value:gsub('\\', '\\\\')
return value
end
---Sanitize a table of values recursively
---@param data table The table to sanitize
---@return table sanitized The sanitized table
function Input:sanitizeTable(data)
local sanitized = {}
for k, v in pairs(data) do
if type(v) == 'table' then
sanitized[k] = self:sanitizeTable(v)
else
sanitized[k] = self:sanitize(v)
end
end
return sanitized
end
---Bind a value for SQL query parameter
---@param value any The value to bind
---@param params table The parameters table to add the binding to
---@return string The parameter placeholder
function Input:bind(value, params)
value = self:sanitize(value)
params[#params + 1] = value
return '?'
end
return Input