lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Jonathan

I checked the docs at:

https://freeswitch.org/confluence/display/FREESWITCH/Lua+API+Reference

There I found that:

------------------------------------------------------------------
dbh:query("query", function()) takes the query as a string and an optional Lua callback function that is called on each row returned by the db.

The callback function is passed a table representation of the current row for each iteration of the loop. Syntax of each row is: { ["column_name_1"] = "value_1", ["column_name_2"] = "value_2" }. If you (optionally) return a number other than 0 from the callback-function, you'll break the loop.
------------------------------------------------------------------

Note the last line. As Alexey already told, it seems that you can return "a number other than 0" to break the loop. In that case you should change your function:

------------------------------------------------------------------
assert (dbh:query(todnew_query,function(todresult)
...
       if (tostring(todresult.dayofmonth) == tostring(t.day)) then
freeswitch.consoleLog("notice","This entry matches on Day of month
too\n")
          --return     <<---tried return or break here, is this correct?
          return 1    <<--- try returning a number other than 0 here
...
------------------------------------------------------------------

Regards,
Tomás

On 2015-09-22 04:50, Jonathan Hunter wrote:
Hi Guys,

Thank you all so much for the response so far.

And yes I use the FreeSWITCH Database Handler (freeswitch.dbh) which
allows connection from my lua script.

I can get results fine, its just ensuring as soon as I get a match
based on month and  day of month that I break out of the function
which is returning the results, as looking at previous freeswitch
related posts I have seen;

You can pass a function as second argument to dbh:query, like so:

dbh = freeswitch.Dbh(odbc_dsn, odbc_user, odbc_pass)
dbh:query("select * from some_table", function(row) -- here it
loops through all rows
 for key, val in pairs(row) do -- here it
loops through all columns
 stream:write(string.format("%25s : %sn", key, val))
 end
 stream:write("n")
end))

This will run the anonymous function for each row, where each
iteration of the function will be passed a row in the form of a table.

Each row (table) will have the following syntax:

{ ["column_name_1"] = "row_value_1", ["column_name_2" = "row_value_2"
}

So I thought it made sense to go to you Lua guru's to give me some
pointers, as the documentation is none existent in terms of how you
can control/break out of this function if you were to get a match.

Hope this makes sense!

Thanks again

Jon

On Tue, Sep 22, 2015 at 5:15 AM, Alexey Melnichuk
<alexeymelnichuck@gmail.com> wrote:

local todnew_query = string.format("select
priority,dayofmonth,month,year,dayofmonth from timeofday_new
where
todname='sales' order by priority;")
assert
(dbh:query(todnew_query,function(todresult)

Now with using the function it loops through the rows, in this
case, priority 1 and 2 rows, but if I match on the first entry
with an if statement, such as;

It hard to say if you do not say which lib you use. I assume you
use
FS. If so just return any number > 0. Otherwise read the
doc/source.
Or try return different values

--
С уважением,
Alexey mailto:alexeymelnichuck@gmail.com

---
Это сообщение проверено на вирусы
антивирусом Avast.
https://www.avast.com/antivirus [1]

--

Jonathan HunterTechnical Director /Telephony Developer
M:(+44) 7917 190 438
Email:jhunter@voxboxcoms.co.uk



Links:
------
[1] https://www.avast.com/antivirus