tribble.parse.sql
Class QueryParser

java.lang.Object
  extended bytribble.parse.sql.QueryParser

public class QueryParser
extends java.lang.Object

Contains methods for parsing query expressions.

This class contains methods for parsing an SQL-like query expression string (i.e., an expression similar to an SQL 'SELECT WHERE' clause) and constructing an expression tree from it. Such a tree can then evaluated against a given set of value objects.

Usage

A query (search) criteria string, composed of operators and operands, is used to construct a query control object:

    QueryParser parser;     // Query expression parser
    String      crit;       // Query expression
    QueryExpr   query;      // Query control object

    parser = new QueryParser();
    crit = "date >= '2001-08-01' and rec.name like 'report%.pdf'";
    query = parse(crit); 

See also the description of Expression Trees in the QueryExpr class.

Syntax

The following expression syntax is recognized:

    expr:
        or_expr

    or_expr:
        and_expr
        and_expr 'OR' or_expr                               OP_OR

    and_expr:
        not_expr
        not_expr 'AND' and_expr                             OP_AND

    not_expr:
        cmp_expr
        'NOT' not_expr                                      OP_NOT

    cmp_expr:
        add_expr
        add_expr 'IS' ['NOT'] 'NULL'                        OP_IS
        add_expr ['NOT'] '='  add_expr                      OP_EQ
        add_expr ['NOT'] '<>' add_expr                      OP_NE
        add_expr ['NOT'] '<'  add_expr                      OP_LT
        add_expr ['NOT'] '<=' add_expr                      OP_LE
        add_expr ['NOT'] '>'  add_expr                      OP_GT
        add_expr ['NOT'] '>=' add_expr                      OP_GE
        add_expr ['NOT'] 'CONTAINS' add_expr                OP_CONTAINS
        add_expr ['NOT'] 'LIKE'     add_expr                OP_LIKE
        add_expr ['NOT'] 'LIKEFILE' add_expr                OP_LIKEFILE
        add_expr ['NOT'] 'BETWEEN'  add_expr 'AND' add_expr OP_BETWEEN
        add_expr ['NOT'] 'IN' '(' expr_list ')'             OP_IN

    expr_list:
        add_expr
        add_expr [','] expr_list                            OP_LIST

    add_expr:
        mul_expr
        mul_expr '+'  add_expr                              OP_ADD
        mul_expr '-'  add_expr                              OP_SUB
        mul_expr '||' add_expr                              OP_CONCAT

    mul_expr:
        unary_expr
        unary_expr '*'   mul_expr                           OP_MUL
        unary_expr '/'   mul_expr                           OP_DIV
        unary_expr 'MOD' mul_expr                           OP_MOD

    unary_expr
        expo_expr
        '+' unary_expr                                      OP_POS
        '-' unary_expr                                      OP_NEG

    expo_expr:
        operand
        operand '**' unary_expr                             OP_EXPO

    operand:
        '(' or_expr ')'
        name_expr
        number                                              String
        'NULL'                                              String

    name_expr:
        name                                                String
        string                                              String
        name_expr '.' name                                  OP_MEMBER
        name_expr '.' string                                OP_MEMBER
        name_expr '[' add_expr ']'                          OP_SUBSCR
 

Since:
2001-03-24
Version:
$Revision: 1.11 $ $Date: 2007/08/01 03:33:10 $
Author:
David R. Tribble (david@tribble.com).

Copyright 2001 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by by the United States Department of State as a terrorist, or terrorist government or agency, to use and distribute this source code provided that the original copyright notice remains present and unaltered.

See Also:
QueryExpr

Field Summary
static int SERIES
           
 
Constructor Summary
QueryParser()
          Default constructor.
 
Method Summary
static void main(java.lang.String[] args)
          Test driver.
 QueryExpr parse(java.lang.String expr)
          Parse a query expression, converting it into an expression tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SERIES

public static final int SERIES
See Also:
Constant Field Values
Constructor Detail

QueryParser

public QueryParser()
Default constructor.

Since:
1.1, 2001-03-13
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test driver.

Throws:
java.lang.Exception
Since:
1.1, 2001-03-11

parse

public QueryExpr parse(java.lang.String expr)
                throws java.text.ParseException
Parse a query expression, converting it into an expression tree.

Parameters:
expr - A query expression. This is parsed and converted into an expression tree.
Returns:
An expression tree resulting from the parse.
Throws:
java.text.ParseException - Thrown if the query expression expr is malformed.
java.text.ParseException - Thrown if the query expression expr is malformed.
Since:
1.1, 2001-03-13