Comparison

util/sqlite3.lua @ 13147:e560f7c691ce

util.sqlite3: Don't cache prepared statements for one-off queries The :execute method is mainly used for one-off queries such as creating tables and indices. There is no need to cache this prepared statement, as those queries are only done on startup. Further, prepared statements can't be reused without being reset, so this was likely broken anyway.
author Kim Alvefur <zash@zash.se>
date Sat, 10 Jun 2023 22:20:26 +0200
parent 13146:771eb453e03a
child 13239:f2578a69ccf4
comparison
equal deleted inserted replaced
13146:771eb453e03a 13147:e560f7c691ce
161 -- Override from create_engine() 161 -- Override from create_engine()
162 end 162 end
163 function engine:ondisconnect() -- luacheck: ignore 212/self 163 function engine:ondisconnect() -- luacheck: ignore 212/self
164 -- Override from create_engine() 164 -- Override from create_engine()
165 end 165 end
166
166 function engine:execute(sql, ...) 167 function engine:execute(sql, ...)
167 local success, err = self:connect(); 168 local success, err = self:connect();
168 if not success then return success, err; end 169 if not success then return success, err; end
169 local prepared = self.prepared; 170 local prepared = self.prepared;
170 171
176 return err; 177 return err;
177 end 178 end
178 return true; 179 return true;
179 end 180 end
180 181
181 local stmt = prepared[sql]; 182 local stmt, err = self.conn:prepare(sql);
182 if not stmt then 183 if not stmt then
183 local err; 184 err = sqlite_errors.new(err);
184 stmt, err = self.conn:prepare(sql); 185 err.text = self.conn:errmsg();
185 if not stmt then 186 return stmt, err;
186 err = sqlite_errors.new(err);
187 err.text = self.conn:errmsg();
188 return stmt, err;
189 end
190 prepared[sql] = stmt;
191 end 187 end
192 188
193 local ret = stmt:bind_values(...); 189 local ret = stmt:bind_values(...);
194 if ret ~= lsqlite3.OK then return nil, sqlite_errors.new(ret, { message = self.conn:errmsg() }); end 190 if ret ~= lsqlite3.OK then
191 return nil, sqlite_errors.new(ret, { message = self.conn:errmsg() });
192 end
195 return stmt; 193 return stmt;
196 end 194 end
197 195
198 local function iterator(table) 196 local function iterator(table)
199 local i = 0; 197 local i = 0;