Comparison

util/sql.lua @ 5744:253dfea0e3f6

util.sql: Do lazy fetching of affected/rowcount
author Kim Alvefur <zash@zash.se>
date Fri, 12 Jul 2013 17:44:30 +0200
parent 5743:8363dda76155
child 5776:bd0ff8ae98a8
comparison
equal deleted inserted replaced
5743:8363dda76155 5744:253dfea0e3f6
175 if not success then return success, err; end 175 if not success then return success, err; end
176 return stmt; 176 return stmt;
177 end 177 end
178 178
179 local result_mt = { __index = { 179 local result_mt = { __index = {
180 affected = function(self) return self.__affected; end; 180 affected = function(self) return self.__stmt:affected(); end;
181 rowcount = function(self) return self.__rowcount; end; 181 rowcount = function(self) return self.__stmt:rowcount(); end;
182 } }; 182 } };
183 183
184 function engine:execute_query(sql, ...) 184 function engine:execute_query(sql, ...)
185 if self.params.driver == "PostgreSQL" then 185 if self.params.driver == "PostgreSQL" then
186 sql = sql:gsub("`", "\""); 186 sql = sql:gsub("`", "\"");
198 if not stmt then 198 if not stmt then
199 stmt = assert(self.conn:prepare(sql)); 199 stmt = assert(self.conn:prepare(sql));
200 prepared[sql] = stmt; 200 prepared[sql] = stmt;
201 end 201 end
202 assert(stmt:execute(...)); 202 assert(stmt:execute(...));
203 return setmetatable({ __affected = stmt:affected(), __rowcount = stmt:rowcount() }, result_mt); 203 return setmetatable({ __stmt = stmt }, result_mt);
204 end 204 end
205 engine.insert = engine.execute_update; 205 engine.insert = engine.execute_update;
206 engine.select = engine.execute_query; 206 engine.select = engine.execute_query;
207 engine.delete = engine.execute_update; 207 engine.delete = engine.execute_update;
208 engine.update = engine.execute_update; 208 engine.update = engine.execute_update;