webmcp

view libraries/mondelefant/mondelefant_atom_connector.lua @ 507:ac5f7a40b8c4

Extended documentation (mostly on "Configuration, pre-fork and post-fork initializers")
author jbe
date Sun Aug 20 23:12:16 2017 +0200 (2017-08-20)
parents b29e082cafb8
children a006593b747c
line source
1 #!/usr/bin/env lua
3 local _G = _G
4 local _VERSION = _VERSION
5 local assert = assert
6 local error = error
7 local getmetatable = getmetatable
8 local ipairs = ipairs
9 local next = next
10 local pairs = pairs
11 local print = print
12 local rawequal = rawequal
13 local rawget = rawget
14 local rawlen = rawlen
15 local rawset = rawset
16 local select = select
17 local setmetatable = setmetatable
18 local tonumber = tonumber
19 local tostring = tostring
20 local type = type
22 local math = math
23 local string = string
24 local table = table
26 local mondelefant = require("mondelefant")
27 local atom = require("atom")
28 local json = require("json")
30 local _M = {}
31 if _ENV then
32 _ENV = _M
33 else
34 _G[...] = _M
35 setfenv(1, _M)
36 end
39 input_converters = setmetatable({}, { __mode = "k" })
41 input_converters["boolean"] = function(conn, value)
42 if value then return "TRUE" else return "FALSE" end
43 end
45 input_converters["number"] = function(conn, value)
46 if _VERSION == "Lua 5.2" then
47 -- TODO: remove following compatibility hack to allow large integers (e.g. 1e14) in Lua 5.2
48 local integer_string = string.format("%i", value)
49 if tonumber(integer_string) == value then
50 return integer_string
51 else
52 local number_string = tostring(value)
53 if string.find(number_string, "^[0-9.e+-]+$") then
54 return number_string
55 else
56 return "'NaN'"
57 end
58 end
59 end
60 local integer = math.tointeger(value)
61 if integer then
62 return tostring(integer)
63 end
64 local str = tostring(value)
65 if string.find(str, "^[0-9.e+-]+$") then
66 return str
67 end
68 return "'NaN'"
69 end
71 input_converters[atom.fraction] = function(conn, value)
72 if value.invalid then
73 return "'NaN'"
74 else
75 local n, d = tostring(value.numerator), tostring(value.denominator)
76 if string.find(n, "^%-?[0-9]+$") and string.find(d, "^%-?[0-9]+$") then
77 return "(" .. n .. "::numeric / " .. d .. "::numeric)"
78 else
79 return "'NaN'"
80 end
81 end
82 end
84 input_converters[atom.date] = function(conn, value)
85 return conn:quote_string(tostring(value)) .. "::date"
86 end
88 input_converters[atom.timestamp] = function(conn, value)
89 return conn:quote_string(tostring(value)) -- don't define type
90 end
92 input_converters[atom.time] = function(conn, value)
93 return conn:quote_string(tostring(value)) .. "::time"
94 end
97 output_converters = setmetatable({}, { __mode = "k" })
99 output_converters.int8 = function(str) return atom.integer:load(str) end
100 output_converters.int4 = function(str) return atom.integer:load(str) end
101 output_converters.int2 = function(str) return atom.integer:load(str) end
103 output_converters.numeric = function(str) return atom.number:load(str) end
104 output_converters.float4 = function(str) return atom.number:load(str) end
105 output_converters.float8 = function(str) return atom.number:load(str) end
107 output_converters.bool = function(str) return atom.boolean:load(str) end
109 output_converters.date = function(str) return atom.date:load(str) end
111 local timestamp_loader_func = function(str)
112 local year, month, day, hour, minute, second = string.match(
113 str,
114 "^([0-9][0-9][0-9][0-9])%-([0-9][0-9])%-([0-9][0-9]) ([0-9]?[0-9]):([0-9][0-9]):([0-9][0-9])"
115 )
116 if year then
117 return atom.timestamp{
118 year = tonumber(year),
119 month = tonumber(month),
120 day = tonumber(day),
121 hour = tonumber(hour),
122 minute = tonumber(minute),
123 second = tonumber(second)
124 }
125 else
126 return atom.timestamp.invalid
127 end
128 end
129 output_converters.timestamp = timestamp_loader_func
130 output_converters.timestamptz = timestamp_loader_func
132 local time_loader_func = function(str)
133 local hour, minute, second = string.match(
134 str,
135 "^([0-9]?[0-9]):([0-9][0-9]):([0-9][0-9])"
136 )
137 if hour then
138 return atom.time{
139 hour = tonumber(hour),
140 minute = tonumber(minute),
141 second = tonumber(second)
142 }
143 else
144 return atom.time.invalid
145 end
146 end
147 output_converters.time = time_loader_func
148 output_converters.timetz = time_loader_func
150 local json_loader_func = function(str)
151 return assert(json.import(str))
152 end
153 output_converters.json = json_loader_func
154 output_converters.jsonb = json_loader_func
156 mondelefant.postgresql_connection_prototype.type_mappings = {
157 int8 = atom.integer,
158 int4 = atom.integer,
159 int2 = atom.integer,
160 bool = atom.boolean,
161 date = atom.date,
162 timestamp = atom.timestamp,
163 time = atom.time,
164 text = atom.string,
165 varchar = atom.string,
166 json = json,
167 jsonb = json,
168 }
171 function mondelefant.postgresql_connection_prototype.input_converter(conn, value, info)
172 if value == nil then
173 return "NULL"
174 else
175 local converter =
176 input_converters[getmetatable(value)] or
177 input_converters[type(value)]
178 if converter then
179 return converter(conn, value)
180 else
181 return conn:quote_string(tostring(value))
182 end
183 end
184 end
186 function mondelefant.postgresql_connection_prototype.output_converter(conn, value, info)
187 if value == nil then
188 return nil
189 else
190 local converter = output_converters[info.type]
191 if converter then
192 return converter(value)
193 else
194 return value
195 end
196 end
197 end
200 function mondelefant.save_mutability_state(value)
201 local jsontype = json.type(value)
202 if jsontype == "object" or jsontype == "array" then
203 return tostring(value)
204 end
205 end
207 function mondelefant.verify_mutability_state(value, state)
208 return tostring(value) ~= state
209 end
212 return _M
215 --[[
217 db = assert(mondelefant.connect{engine='postgresql', dbname='test'})
218 result = db:query{'SELECT ? + 1', atom.date{ year=1999, month=12, day=31}}
219 print(result[1][1].year)
221 --]]

Impressum / About Us