-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathresty_formatter.lua
More file actions
41 lines (30 loc) · 817 Bytes
/
Copy pathresty_formatter.lua
File metadata and controls
41 lines (30 loc) · 817 Bytes
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
local type = type
local tostring = tostring
local string_gsub = string.gsub
local error = error
local _M = {}
local quote_sql_str = ngx and ngx.quote_sql_str
if not quote_sql_str then
quote_sql_str = function(s)
return "'" .. string_gsub(s, "'", "''") .. "'"
end
end
local ngx_null = ngx and ngx.null
local function escape_value(val)
local t = type(val)
if t == "string" then
return quote_sql_str(val)
elseif t == "number" then
return tostring(val)
elseif t == "boolean" then
return val and "TRUE" or "FALSE"
elseif val == nil or val == ngx_null then
return "NULL"
else
error("resty_formatter: cannot escape sql value of type '" .. t .. "'")
end
end
function _M.format(idx, arg)
return escape_value(arg)
end
return _M