Dan Zwell
4/29/2007 8:03:00 PM
Thomas Hurst wrote:
> Demo:
> # Token stream
> irb(main):009:0> toks = analyser.simplify(tokenizer.tokenize("foo AND bar OR (wibble OR wobble NOT foo)"))
> [#<NSearch::SubStringQuery:0x18a4a20 @substr="foo">,
> :and,
> #<NSearch::SubStringQuery:0x18a49f8 @substr="bar">,
> :or,
> :open,
> #<NSearch::SubStringQuery:0x18a49a8 @substr="wibble">,
> :or,
> #<NSearch::SubStringQuery:0x18a4958 @substr="wobble">,
> :and,
> :not,
> #<NSearch::SubStringQuery:0x18a4908 @substr="foo">,
> :close]
>
> irb(main):010:0> analyser.rpnise(toks)
> [#<NSearch::SubStringQuery:0x188a670 @substr="foo">,
> #<NSearch::SubStringQuery:0x188a648 @substr="bar">,
> :and,
> #<NSearch::SubStringQuery:0x188a5f8 @substr="wibble">,
> #<NSearch::SubStringQuery:0x188a5a8 @substr="wobble">,
> #<NSearch::SubStringQuery:0x188a558 @substr="foo">,
> :not,
> :and,
> :or,
> :or]
>
> You can then follow the instructions, popping each SubStringQuery onto a
> stack and popping them off on :and, :or and :not to build a tree of
> query objects:
>
> #to_s
> OR(OR(AND(NOT("foo"),"wobble"),"wibble"),AND("bar","foo"))
>
> #to_sql
> (((NOT (Title LIKE "%foo%") AND Title LIKE "%wobble%") OR Title LIKE
> "%wibble%") OR (Title LIKE "%bar%" AND Title LIKE "%foo%"))
>
>
> I'd recommend against using Ruby's own Generator, btw; it uses callcc
> and has a tendancy towards being slow and leaky. If you just need a
> #next/#peek method, just turn it into an array and shove it in an
> appropriate object which tracks the position.
>
That's pretty neat. I hadn't thought of using this stuff to generate
SQL. I'll probably acually use that idea some day. And thanks for the
bit about Generator. I'm currently looking through a computational
script (ported from c++)--I could never figure out why it used all my
ram within a few seconds, but it used Generators several thousand times.
I'm rewriting parts of it now, and we'll see what happens.
Dan