@@ -171,16 +171,18 @@ function tbl:each(func, query)
171171 func , query = query , func
172172 end
173173
174- local rows = self .db :select (self .name , query )
175- if not rows then
176- return
177- end
174+ return run (function ()
175+ local rows = self .db :select (self .name , query )
176+ if not rows then
177+ return false
178+ end
178179
179- for _ , row in ipairs (rows ) do
180- func (row )
181- end
180+ for _ , row in ipairs (rows ) do
181+ func (row )
182+ end
182183
183- return rows ~= {} or type (rows ) ~= " boolean"
184+ return rows ~= {} or type (rows ) ~= " boolean"
185+ end , self )
184186end
185187
186188--- Create a new table from iterating over {self.name} rows with {func}.
@@ -191,21 +193,25 @@ end
191193--- @return table[]
192194function tbl :map (func , query )
193195 query = query or {}
194- local res = {}
195196 if type (func ) == " table" then
196197 func , query = query , func
197198 end
198199
199- local rows = self .db :select (self .name , query )
200- if not rows then
201- return {}
202- end
203-
204- for _ , row in ipairs (rows ) do
205- table.insert (res , func (row ))
206- end
200+ return run (function ()
201+ local res = {}
202+ local rows = self .db :select (self .name , query )
203+ if not rows then
204+ return {}
205+ end
206+ for _ , row in ipairs (rows ) do
207+ local ret = func (row )
208+ if ret then
209+ table.insert (res , func (row ))
210+ end
211+ end
207212
208- return res
213+ return res
214+ end , self )
209215end
210216
211217--- Sorts a table in-place using a transform. Values are ranked in a custom order of the results of
@@ -219,22 +225,24 @@ end
219225--- @usage `local res = t1:sort({ where = {id = {32,12,35}}}, "age")` return rows sort by age
220226--- @usage `local res = t1:sort({where = { ... }}, "age", function(a, b) return a > b end)` with custom function
221227function tbl :sort (query , transform , comp )
222- local res = self :get (query )
223- local f = transform or function (r )
224- return r [u .keys (query .where )[1 ]]
225- end
226- if type (transform ) == " string" then
227- f = function (r )
228- return r [transform ]
228+ return run (function ()
229+ local res = self :get (query )
230+ local f = transform or function (r )
231+ return r [u .keys (query .where )[1 ]]
229232 end
230- end
231- comp = comp or function (a , b )
232- return a < b
233- end
234- table.sort (res , function (a , b )
235- return comp (f (a ), f (b ))
236- end )
237- return res
233+ if type (transform ) == " string" then
234+ f = function (r )
235+ return r [transform ]
236+ end
237+ end
238+ comp = comp or function (a , b )
239+ return a < b
240+ end
241+ table.sort (res , function (a , b )
242+ return comp (f (a ), f (b ))
243+ end )
244+ return res
245+ end , self )
238246end
239247
240248--- Same functionalities as |DB:insert()|
0 commit comments