Fix. ODBC db backend should breaks loop only when callback returns number ~= 0

```Lua
dbh:query(sql, function(row)
  -- return 0     -- should not break loop
  -- return true  -- should not break loop
  return 1 -- should break loop
end)
```
This commit is contained in:
Alexey Melnichuk 2016-02-26 13:52:27 +03:00
parent 18de6fab30
commit c96a5bee48
3 changed files with 45 additions and 2 deletions

View File

@ -77,6 +77,38 @@ local function new_database(backend)
assert(db:connected())
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return 1
end)
assert(x == 1, ("Got %d expected %d"):format(x, 1))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return 0
end)
assert(x == 2, ("Got %d expected %d"):format(x, 2))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return true
end)
assert(x == 2, ("Got %d expected %d"):format(x, 2))
end
do local x = 0
db:query("select 1 as v union all select 2 as v", function(row)
x = x + 1
return false
end)
assert(x == 2, ("Got %d expected %d"):format(x, 2))
end
assert("1" == db:first_value("select 1 as v union all select 2 as v"))
local t = assert(db:first_row("select '1' as v union all select '2' as v"))
@ -99,6 +131,11 @@ local function new_database(backend)
db:release()
assert(not db:connected())
-- second close
db:release()
assert(not db:connected())
log.info('self_test Database - pass')
end

View File

@ -38,7 +38,10 @@ function OdbcDatabase:query(sql, fn)
self._rows_affected = nil
if fn then
return self._dbh:neach(sql, function(row)
return fn(remove_null(row, odbc.NULL, ""))
local n = fn(remove_null(row, odbc.NULL, ""))
if type(n) == 'number' and n ~= 0 then
return true
end
end)
end
local ok, err = self._dbh:exec(sql)

View File

@ -39,7 +39,10 @@ function OdbcPoolDatabase:query(sql, fn)
if fn then
ok, err = cli:acquire(self._timeout, function(dbh)
local ok, err = dbh:neach(sql, function(row)
return fn(remove_null(row, odbc.NULL, ""))
local n = fn(remove_null(row, odbc.NULL, ""))
if type(n) == 'number' and n ~= 0 then
return true
end
end)
if err and not ok then
log.errf("Can not execute sql: %s\n%s", tostring(err), sql)