Software /
code /
prosody
Comparison
util/sql.lua @ 7179:bfa3b2ee384c
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 22 Feb 2016 15:24:20 +0100 |
parent | 7174:d350e475f94e |
child | 7180:0d7f80263813 |
comparison
equal
deleted
inserted
replaced
7171:91d36a37a9ff | 7179:bfa3b2ee384c |
---|---|
145 local result_mt = { __index = { | 145 local result_mt = { __index = { |
146 affected = function(self) return self.__stmt:affected(); end; | 146 affected = function(self) return self.__stmt:affected(); end; |
147 rowcount = function(self) return self.__stmt:rowcount(); end; | 147 rowcount = function(self) return self.__stmt:rowcount(); end; |
148 } }; | 148 } }; |
149 | 149 |
150 local function debugquery(where, sql, ...) | |
151 local i = 0; local a = {...} | |
152 log("debug", "[%s] %s", where, sql:gsub("%?", function () i = i + 1; local v = a[i]; if type(v) == "string" then v = ("%q"):format(v); end return tostring(v); end)); | |
153 end | |
154 | |
150 function engine:execute_query(sql, ...) | 155 function engine:execute_query(sql, ...) |
151 if self.params.driver == "PostgreSQL" then | 156 if self.params.driver == "PostgreSQL" then |
152 sql = sql:gsub("`", "\""); | 157 sql = sql:gsub("`", "\""); |
153 end | 158 end |
154 local stmt = assert(self.conn:prepare(sql)); | 159 local stmt = assert(self.conn:prepare(sql)); |
170 end | 175 end |
171 engine.insert = engine.execute_update; | 176 engine.insert = engine.execute_update; |
172 engine.select = engine.execute_query; | 177 engine.select = engine.execute_query; |
173 engine.delete = engine.execute_update; | 178 engine.delete = engine.execute_update; |
174 engine.update = engine.execute_update; | 179 engine.update = engine.execute_update; |
180 local function debugwrap(name, f) | |
181 return function (self, sql, ...) | |
182 debugquery(name, sql, ...) | |
183 return f(self, sql, ...) | |
184 end | |
185 end | |
186 function engine:debug(enable) | |
187 self._debug = enable; | |
188 if enable then | |
189 engine.insert = debugwrap("insert", engine.execute_update); | |
190 engine.select = debugwrap("select", engine.execute_query); | |
191 engine.delete = debugwrap("delete", engine.execute_update); | |
192 engine.update = debugwrap("update", engine.execute_update); | |
193 else | |
194 engine.insert = engine.execute_update; | |
195 engine.select = engine.execute_query; | |
196 engine.delete = engine.execute_update; | |
197 engine.update = engine.execute_update; | |
198 end | |
199 end | |
175 function engine:_transaction(func, ...) | 200 function engine:_transaction(func, ...) |
176 if not self.conn then | 201 if not self.conn then |
177 local ok, err = self:connect(); | 202 local ok, err = self:connect(); |
178 if not ok then return ok, err; end | 203 if not ok then return ok, err; end |
179 end | 204 end |
219 sql = sql:gsub("`([,)])", "`(20)%1"); | 244 sql = sql:gsub("`([,)])", "`(20)%1"); |
220 end | 245 end |
221 if index.unique then | 246 if index.unique then |
222 sql = sql:gsub("^CREATE", "CREATE UNIQUE"); | 247 sql = sql:gsub("^CREATE", "CREATE UNIQUE"); |
223 end | 248 end |
224 --print(sql); | 249 if self._debug then |
250 debugquery("create", sql); | |
251 end | |
225 return self:execute(sql); | 252 return self:execute(sql); |
226 end | 253 end |
227 function engine:_create_table(table) | 254 function engine:_create_table(table) |
228 local sql = "CREATE TABLE `"..table.name.."` ("; | 255 local sql = "CREATE TABLE `"..table.name.."` ("; |
229 for i,col in ipairs(table.c) do | 256 for i,col in ipairs(table.c) do |
249 sql = sql.. ");" | 276 sql = sql.. ");" |
250 if self.params.driver == "PostgreSQL" then | 277 if self.params.driver == "PostgreSQL" then |
251 sql = sql:gsub("`", "\""); | 278 sql = sql:gsub("`", "\""); |
252 elseif self.params.driver == "MySQL" then | 279 elseif self.params.driver == "MySQL" then |
253 sql = sql:gsub(";$", (" CHARACTER SET '%s' COLLATE '%s_bin';"):format(self.charset, self.charset)); | 280 sql = sql:gsub(";$", (" CHARACTER SET '%s' COLLATE '%s_bin';"):format(self.charset, self.charset)); |
281 end | |
282 if self._debug then | |
283 debugquery("create", sql); | |
254 end | 284 end |
255 local success,err = self:execute(sql); | 285 local success,err = self:execute(sql); |
256 if not success then return success,err; end | 286 if not success then return success,err; end |
257 for i,v in ipairs(table.__table__) do | 287 for i,v in ipairs(table.__table__) do |
258 if is_index(v) then | 288 if is_index(v) then |