SQL Where Parsing

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

SQL Where Parsing

posix4e
So I am trying to build a new SQL parser in paraboiled and am having a bit of an issue with where parsing

The rules for SQL are easy. AND has Precedence of OR except in the case of parenthesis


so

A || B && C  and A|| (B && C) are the same but not the same as (A || B) && C.

I wrote the following

  def simplePredicate: Rule1[AstNode with Wherable] = rule {
    optional(START_BLOCK) ~ WhiteSpace ~
    stringVarname ~ WhiteSpace ~ comparisonEquality ~ WhiteSpace ~ stringVarname ~
    optional(END_BLOCK) ~ WhiteSpace~~> {
      ((a, b, c, d ,e ) => {
        TripleNode(a.isDefined, b, c, d ,e.isDefined)
      })
    }
  }

  def simplePredicates: Rule1[List[AstNode with Wherable]] = rule {
    simplePredicate ~ zeroOrMore(simpleSubComparison) ~~> {
      ((a, b) => (a :: b.flatten))
    }
  }

  def simpleSubComparison: Rule1[List[AstNode with Wherable]] = rule {
    ((AND | OR) ~ simplePredicate) ~~> {
      ((a,b) =>  List(LogicNode(WhereLogic.valueOf(a)), b))
    }
  }


But what I'd really like is a tree I could just walk. For now I have to handle the order of operations myself but I assume the parser should be able to do this? I looked at the sparql example and have to admit it didn't provide much for me.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: SQL Where Parsing

mathias
Administrator
For building a SQL parser I would probably try to transscribe one of the available SQL grammars (in BNF)
to parboiled. [This page][1] for example links to a few of them.
When you implement the grammar properly than things like operator precedence will automatically be taken care of.

Cheers,
Mathias

[1]: http://savage.net.au/SQL/

---
[hidden email]
http://www.parboiled.org

On 18.07.2012, at 04:26, posix4e [via parboiled users] wrote:

> So I am trying to build a new SQL parser in paraboiled and am having a bit of an issue with where parsing
>
> The rules for SQL are easy. AND has Precedence of OR except in the case of parenthesis
>
>
> so
>
> A || B && C  and A|| (B && C) are the same but not the same as (A || B) && C.
>
> I wrote the following
>
>   def simplePredicate: Rule1[AstNode with Wherable] = rule {
>     optional(START_BLOCK) ~ WhiteSpace ~
>     stringVarname ~ WhiteSpace ~ comparisonEquality ~ WhiteSpace ~ stringVarname ~
>     optional(END_BLOCK) ~ WhiteSpace~~> {
>       ((a, b, c, d ,e ) => {
>         TripleNode(a.isDefined, b, c, d ,e.isDefined)
>       })
>     }
>   }
>
>   def simplePredicates: Rule1[List[AstNode with Wherable]] = rule {
>     simplePredicate ~ zeroOrMore(simpleSubComparison) ~~> {
>       ((a, b) => (a :: b.flatten))
>     }
>   }
>
>   def simpleSubComparison: Rule1[List[AstNode with Wherable]] = rule {
>     ((AND | OR) ~ simplePredicate) ~~> {
>       ((a,b) =>  List(LogicNode(WhereLogic.valueOf(a)), b))
>     }
>   }
>
>
> But what I'd really like is a tree I could just walk. For now I have to handle the order of operations myself but I assume the parser should be able to do this? I looked at the sparql example and have to admit it didn't provide much for me.
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/SQL-Where-Parsing-tp4024044.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...