table query language

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

table query language

Glenn edgar
Hi All

I have a tables where the size is between 1000 and 10000.  The user needs to select various table elements.  An effective manner to select table elements is through a query language.

The query language is lua, where the query string is executed with a do string.

What I would like to do is to have a query language much like SQL where the _expression_ is something like

(timeZone == "EST") or ( device =="CLX" )

where timeZone and device are fields in the lua table.

I was able to make the query language work by using the following string

{ ts.entry.timeZone == "EST") or ( ts.entry.timeZone == "PST")

ts.entry is variable from a pairs iteration over the table.

Two questions
1.  Is there a better way to do a query language over a set of tables
2.  If not is there a easy way in lua to transform the query string
(timeZone == "EST") or ( timeZone == "PST")

to
{ ts.entry.timeZone == "EST") or ( ts.entry.timeZone == "PST")


Thanks

Glenn

Reply | Threaded
Open this post in threaded view
|

Re: table query language

Luiz Henrique de Figueiredo
> 1.  Is there a better way to do a query language over a set of tables

Use setfenv to set the environment of the function returned by loadstring
to the table you want to query. Do that for each table.

> 2.  If not is there a easy way in lua to transform the query string
> (timeZone == "EST") or ( timeZone == "PST")
> 
> to
> { ts.entry.timeZone == "EST") or ( ts.entry.timeZone == "PST")

You could do it easily with token filters, but I think the soltion above is
simpler and more powerful.

If your queries involve actual Lua globals, such as say math.sqrt, then you 
need to set __index methods for your tables to _G.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: table query language

David Manura
In reply to this post by Glenn edgar
On Mon, Jan 12, 2009 at 5:06 AM, Glenn Edgar wrote:
> An effective manner to select table elements
> is through a query language.

These could give you some ideas:

[1] http://lua-users.org/wiki/ListComprehensions
[2] http://en.wikipedia.org/wiki/Language_Integrated_Query

Reply | Threaded
Open this post in threaded view
|

Re: table query language

Glenn edgar
In reply to this post by Luiz Henrique de Figueiredo
The setfenv is an excellent idea.  I will try it. 

Thanks

On Mon, Jan 12, 2009 at 2:29 AM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> 1.  Is there a better way to do a query language over a set of tables

Use setfenv to set the environment of the function returned by loadstring
to the table you want to query. Do that for each table.

> 2.  If not is there a easy way in lua to transform the query string
> (timeZone == "EST") or ( timeZone == "PST")
>
> to
> { ts.entry.timeZone == "EST") or ( ts.entry.timeZone == "PST")

You could do it easily with token filters, but I think the soltion above is
simpler and more powerful.

If your queries involve actual Lua globals, such as say math.sqrt, then you
need to set __index methods for your tables to _G.
--lhf