-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgresLuvit.lua
195 lines (163 loc) · 6.07 KB
/
postgresLuvit.lua
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
--------------------------------------------------------------------------
-- This module is a luvit binding for the postgresql api.
--
-- Copyright (C) 2012 Moritz Kühner, Germany.
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------
local postgres = require("postgresffi")
local timer = require('timer')
local object = require('core').Object
local table = require("table")
--[[Loads the library at POSTGRESQL_LIBRARY_PATH if not nil
defaults to "/usr/lib/libpq.so.5".
To use this set the global with a string pointing at the right
path befor requiring this library.
]]
postgres.init(POSTGRESQL_LIBRARY_PATH)
--Timeout for the poll timer
local POLLTIMER_INTERVALL = 5
local LuvPostgres = object:extend()
local function callIfNotNil(callback, ...)
if callback ~= nil then
callback(...)
end
end
--[[Constructor the coninfo string is passed to PQconnectStart.
]]
function LuvPostgres:initialize(coninfo, callback)
self.con = postgres.newAsync(coninfo)
--[[ This is an dirty hack to update the connection state. The correct
solution should watch the socket descriptor and update upon
network activity
]]
self.watcher = timer.setInterval(POLLTIMER_INTERVALL, function()
local state = self.con:dialUpState()
if 0 == state then
timer.clearTimer(self.watcher)
self.established = true
callIfNotNil(callback)
elseif 1 == state then
timer.clearTimer(self.watcher)
callIfNotNil(callback, self.con:getError())
end
end)
end
-- internal function to retrieve a fragment of the result
local function getFragment(connection)
local ok, ready = pcall(connection.readReady, connection)
if not ok then
--error occured
return nil, ready
elseif not ready then
--no input ready
return {}
end
local ok, result , status = pcall(connection.getAvailable, connection)
if not ok then
--error occured
return nil, ready
end
local isOver = false
if status <= 7 then
--query is over
if connection:getAvailable() ~= nil then
--internal error occured
return nil, "Internal binding error. Query is not over!"
end
if status == 5 or status == 7 then
--error occured
return nil, connection:getError()
end
isOver = true
end
return result, isOver
end
--[[Sends a query to the sql server
Callback is called with error and the entire resultset.
Read postgresffi.getAvailable for a description of the format.
]]
function LuvPostgres:sendQuery(query, callback)
if not self.established then
callIfNotNil(callback, "Can't send query. Connection is not established!")
return
end
self.con:sendQuery(query)
local resultAccu = {}
--[[ This is an dirty hack to update the connection state. The correct
solution should watch the socket descriptor and update upon
network activity
]]
self.watcher = timer.setInterval(POLLTIMER_INTERVALL, function()
local result, over = getFragment(self.con)
if not result then
timer.clearTimer(self.watcher)
callIfNotNil(callback, over)
else
local cntRow = #result
for i = 1, cntRow do
table.insert(resultAccu, result[i])
end
if over then
resultAccu[0] = result[0]
timer.clearTimer(self.watcher)
callIfNotNil(callback, nil, resultAccu)
end
end
end)
end
--[[Sends a query to the sql server and returns intermediate results
Callback is called with error and subset of the result and to mark the end a boolean true.
Read postgresffi.getAvailable for a description of the format.
]]
function LuvPostgres:sendQueryIntermediate(query, callback)
if not self.established then
callIfNotNil(callback, "Can't send query. Connection is not established!")
return
end
self.con:sendQuery(query)
--[[ This is an dirty hack to update the connection state. The correct
solution should watch the socket descriptor and update upon
network activity
]]
self.watcher = timer.setInterval(POLLTIMER_INTERVALL, function()
local result, over = getFragment(self.con)
if not result then
timer.clearTimer(self.watcher)
callIfNotNil(callback, over)
else
if over then
timer.clearTimer(self.watcher)
end
callIfNotNil(callback, nil, result, over)
end
end)
end
--[[Returns a escaped version of the string than can be savely
used in a query without danger of SQL injection or nil and
a message on error
]]
function LuvPostgres:escape(query)
local ok, value = pcall(self.con.escape, self.con, query)
if ok then
return value
end
return nil, value
end
return LuvPostgres