tribble.parse.sql
Class QueryExpr

java.lang.Object
  extended bytribble.parse.sql.QueryExpr
All Implemented Interfaces:
java.lang.Cloneable

public class QueryExpr
extends java.lang.Object
implements java.lang.Cloneable

Query expression tree.

An object of this type is produced by a QueryParser object, which parses an SQL-like expression (i.e., an expression similar to an SQL 'SELECT WHERE' clause) and produces an expression tree in the form of a QueryExpr object.

Expression Trees

Some example query expressions and the expression trees resulting from parsing them:

Query Expression Tree
 exists
 
 val
   "exists"
 
 not directory
 
 not
   val
     "directory"
 
 not directory or writable
 
 or
   not
     val
       "directory"
   val
     "writable"
 
 name like "Sarah%Conner" and
   ( birth_date >= '1996-06-14' )
 
 and
   like
     "name"
     "\"Sarah%Conner\""
   ge
     "birth_date"
     "'1996-06-14'"
 
 interest >= 5.000  and
   interest < 10.000
 
 and
   ge
     "interest"
     "5.000"
   lt
     "interest"
     "10.000"
 
 rec.id between 'A000' and 'B999'
 
 between
   "rec.id"
   and
     "'A000'"
     "'B999'"
 
 pay-type in ( 'WK' 'HR' 'YR' )
 
 in
   "pay-type"
   list
     "'WK'"
     list
       "'HR'"
       list
         "'YR'"
 
 addr is not null
 
 not
   is
     "addr"
     "null"
 
 rec.pay[mon]
 
 subscr
   member
     "rec"
     "pay"
   "mon"
 

Since:
2001-03-24
Version:
$Revision: 1.10 $ $Date: 2007/08/01 03:14:02 $
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:
QueryParser

Field Summary
 java.lang.Object m_arg1
          First (left) argument, typically either a QueryExpr or a String.
 java.lang.Object m_arg2
          Second (right) argument, typically either a QueryExpr or a String.
 java.lang.String m_op
          Operator, one of the OP_XXX constants.
static java.lang.String OP_ADD
           
static java.lang.String OP_AND
           
static java.lang.String OP_BETWEEN
           
static java.lang.String OP_CONCAT
           
static java.lang.String OP_CONTAINS
           
static java.lang.String OP_DIV
           
static java.lang.String OP_EQ
           
static java.lang.String OP_EXPO
           
static java.lang.String OP_GE
           
static java.lang.String OP_GT
           
static java.lang.String OP_IN
           
static java.lang.String OP_IS
           
static java.lang.String OP_LE
           
static java.lang.String OP_LIKE
           
static java.lang.String OP_LIKEFILE
           
static java.lang.String OP_LIST
           
static java.lang.String OP_LT
           
static java.lang.String OP_MEMBER
           
static java.lang.String OP_MOD
           
static java.lang.String OP_MUL
           
static java.lang.String OP_NE
           
static java.lang.String OP_NEG
           
static java.lang.String OP_NOP
           
static java.lang.String OP_NOT
           
static java.lang.String OP_NULL
           
static java.lang.String OP_OR
           
static java.lang.String OP_POS
           
static java.lang.String OP_SUB
           
static java.lang.String OP_SUBSCR
           
static java.lang.String OP_VALUE
           
static int SERIES
           
 
Constructor Summary
QueryExpr()
          Default constructor.
 
Method Summary
protected static java.lang.String asQuotedOperand(java.lang.String s)
          Add enclosing quotes to a string if necessary to make it a valid SQL token.
protected  void buildString(java.lang.Object arg, java.lang.StringBuffer text)
          Format an operand, appending it to a string buffer.
protected  void buildString(java.lang.StringBuffer text)
          Convert this expression subtree into a printable text string, appending it to a string buffer.
 java.lang.Object clone()
          Create a duplicate of this query expression tree.
 void dump(java.io.OutputStream out)
          Print the structure of this expression tree.
 void dump(java.io.Writer out)
          Print the structure of this expression tree.
protected  void dumpTree(java.io.PrintWriter out, java.lang.String indent)
          Print the structure of this expression tree.
static boolean matchPattern(java.lang.String pat, java.lang.String s)
          Determine if a string matches a pattern string.
protected static int matchSubpattern(java.lang.String pat, int patOff, java.lang.String str, int strOff)
          Determine if a substring matches a pattern substring.
 void simplify()
          Simplify this query expression tree.
 java.lang.String toString()
          Convert this expression tree into a printable text string.
protected  void writeAsXml(java.io.BufferedWriter out, java.lang.String indent)
          Print this query expression tree as XML.
 void writeAsXml(java.io.Writer out)
          Print this query expression tree as XML.
protected  void writeStringAsXml(java.io.BufferedWriter out, java.lang.String indent, java.lang.String arg)
          Print a query expression string argument tree as XML.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SERIES

public static final int SERIES
See Also:
Constant Field Values

OP_NOP

public static final java.lang.String OP_NOP
See Also:
Constant Field Values

OP_VALUE

public static final java.lang.String OP_VALUE
See Also:
Constant Field Values

OP_NOT

public static final java.lang.String OP_NOT
See Also:
Constant Field Values

OP_AND

public static final java.lang.String OP_AND
See Also:
Constant Field Values

OP_OR

public static final java.lang.String OP_OR
See Also:
Constant Field Values

OP_LIST

public static final java.lang.String OP_LIST
See Also:
Constant Field Values

OP_IS

public static final java.lang.String OP_IS
See Also:
Constant Field Values

OP_EQ

public static final java.lang.String OP_EQ
See Also:
Constant Field Values

OP_NE

public static final java.lang.String OP_NE
See Also:
Constant Field Values

OP_LT

public static final java.lang.String OP_LT
See Also:
Constant Field Values

OP_LE

public static final java.lang.String OP_LE
See Also:
Constant Field Values

OP_GT

public static final java.lang.String OP_GT
See Also:
Constant Field Values

OP_GE

public static final java.lang.String OP_GE
See Also:
Constant Field Values

OP_EXPO

public static final java.lang.String OP_EXPO
See Also:
Constant Field Values

OP_MUL

public static final java.lang.String OP_MUL
See Also:
Constant Field Values

OP_DIV

public static final java.lang.String OP_DIV
See Also:
Constant Field Values

OP_MOD

public static final java.lang.String OP_MOD
See Also:
Constant Field Values

OP_ADD

public static final java.lang.String OP_ADD
See Also:
Constant Field Values

OP_SUB

public static final java.lang.String OP_SUB
See Also:
Constant Field Values

OP_CONCAT

public static final java.lang.String OP_CONCAT
See Also:
Constant Field Values

OP_POS

public static final java.lang.String OP_POS
See Also:
Constant Field Values

OP_NEG

public static final java.lang.String OP_NEG
See Also:
Constant Field Values

OP_MEMBER

public static final java.lang.String OP_MEMBER
See Also:
Constant Field Values

OP_SUBSCR

public static final java.lang.String OP_SUBSCR
See Also:
Constant Field Values

OP_CONTAINS

public static final java.lang.String OP_CONTAINS
See Also:
Constant Field Values

OP_LIKE

public static final java.lang.String OP_LIKE
See Also:
Constant Field Values

OP_LIKEFILE

public static final java.lang.String OP_LIKEFILE
See Also:
Constant Field Values

OP_IN

public static final java.lang.String OP_IN
See Also:
Constant Field Values

OP_BETWEEN

public static final java.lang.String OP_BETWEEN
See Also:
Constant Field Values

OP_NULL

public static final java.lang.String OP_NULL
See Also:
Constant Field Values

m_op

public java.lang.String m_op
Operator, one of the OP_XXX constants.


m_arg1

public java.lang.Object m_arg1
First (left) argument, typically either a QueryExpr or a String.


m_arg2

public java.lang.Object m_arg2
Second (right) argument, typically either a QueryExpr or a String.

Constructor Detail

QueryExpr

public QueryExpr()
Default constructor.

Since:
1.1, 2001-03-13
Method Detail

matchPattern

public static boolean matchPattern(java.lang.String pat,
                                   java.lang.String s)
Determine if a string matches a pattern string.

Parameters:
pat - An SQL pattern string. A pattern is composed of regular characters and special pattern matching characters, which are:
_ (underscore) - Matches any single character.
% - Matches zero or more characters.
\ - Removes (escapes) the special meaning of the next character.
s - A string to compare against pattern pat.
Returns:
True if string s matches pattern pat, otherwise false.
Since:
1.2, 2001-03-28

matchSubpattern

protected static int matchSubpattern(java.lang.String pat,
                                     int patOff,
                                     java.lang.String str,
                                     int strOff)
Determine if a substring matches a pattern substring.

Parameters:
pat - A pattern string.
patOff - The index of the first (leftmost) character within pattern string pat to match.
str - A string to compare against pattern pat.
strOff - The index of the first (leftmost) character within string str to match.
Returns:
The index of one character past the last (rightmost) character within string str that matches pattern pat, or -1 if the substring does not match the pattern substring.
Since:
1.2, 2001-03-28
See Also:
matchPattern(java.lang.String, java.lang.String)

asQuotedOperand

protected static java.lang.String asQuotedOperand(java.lang.String s)
Add enclosing quotes to a string if necessary to make it a valid SQL token.

Parameters:
s - A string.
Returns:
The contents of string s, either unchanged or enclosed within quotes (").
Since:
1.9, 2001-04-16

toString

public java.lang.String toString()
Convert this expression tree into a printable text string.

Returns:
A text string representing this expression. (Note that the string is generated directly from the expression tree, and may not necessarily look the same as the original query expression that the tree was derived from.)
Since:
1.1, 2001-03-26

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Create a duplicate of this query expression tree.

Returns:
A deep-copy duplicate of this expression tree.
Throws:
java.lang.CloneNotSupportedException - Thrown if any of the node in this expression tree cannot be cloned.
Since:
1.5, 2001-04-01

writeAsXml

public void writeAsXml(java.io.Writer out)
                throws java.io.IOException
Print this query expression tree as XML.

For example, given the query expression:

    len > 80 and rec.type not = 'T' 
the following XML is generated for it:
    <query>
      <and>
        <gt>
          <arg>len</arg>
          <arg>80</arg>
        </gt>
        <not>
          <eq>
            <member>
              <arg>rec</arg>
              <arg>type</arg>
            </member>
            <arg>'T'</arg>
          </eq>
        </not>
      </and>
    </query> 

Parameters:
out - Output stream to write to.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
Since:
1.6, 2001-04-02

dump

public void dump(java.io.Writer out)
Print the structure of this expression tree.

Parameters:
out - An output stream to write to.
Since:
1.1, 2001-03-12

dump

public void dump(java.io.OutputStream out)
Print the structure of this expression tree.

Parameters:
out - An output stream to write to.
Since:
1.1, 2001-03-12

simplify

public void simplify()
              throws java.lang.Exception
Simplify this query expression tree.

Replaces the following query subexpressions with equivalent, but simpler, subexpressions:

    "X between A and B"   replaced with   "X >= A and X <= B"
    "X in ( A, B, C )"    replaced with   "X = A or X = B or X = C"
    "not X = A"           replaced with   "X <> A"
    "not not X = A"       replaced with   "X = A" 

In other words, the following subexpression trees are replaced with simpler equivalent subtrees:

Before After
 between
   "rec_id"
   and
     "'A000'"
     "'B999'"
 
 and
   ge
     "rec_id"
     "'A000'"
   le
     "rec_id"
     "'B999'"
 
 in
   "pay-type"
   list
     "'WK'"
     list
       "'HR'"
       list
         "'YR'"
 
 or
   eq
     "pay-type"
     "'WK'"
   or
     eq
       "pay-type"
       "'HR'"
     eq
       "pay-type"
       "'YR'"
 
 not
   gt
     "Amt"
     "25.00"
 
 le
   "Amt"
   "25.00"
 
 not
   not
     eq
       "Amt"
       "50.00"
 
 eq
   "Amt"
   "50.00"
 

Throws:
java.lang.Exception - Thrown if this expression tree is malformed.
Since:
1.7, 2001-04-03

dumpTree

protected void dumpTree(java.io.PrintWriter out,
                        java.lang.String indent)
Print the structure of this expression tree.

This method is called by methods dump(Writer) and dump(OutputStream).

Example

Parsing the expression:

    name like "sarah%conner" and ( birth.date >= '1996-06-14' ) 
results in an expression tree that is printed like this:
    and
      like
        name
        "sarah%conner"
      ge
        birth.date
        '1996-06-14' 

Parameters:
out - An output stream to write to.
indent - Leading indentation.
Since:
1.1, 2001-03-12
See Also:
dump(Writer), dump(OutputStream)

buildString

protected void buildString(java.lang.StringBuffer text)
Convert this expression subtree into a printable text string, appending it to a string buffer.

This method is called by method toString().

Parameters:
text - A string buffer to append the text string representation of this subexpression to.
Since:
1.1, 2001-03-26
See Also:
toString()

buildString

protected void buildString(java.lang.Object arg,
                           java.lang.StringBuffer text)
Format an operand, appending it to a string buffer.

This method is called by method toString().

Parameters:
arg - An operand to append, which is either a String or an QueryExpr subtree. if it is a String, it will be delimited with quotes as necessary.
text - A string buffer to append the text string representation of this subexpression to.
Since:
1.3, 2001-03-30
See Also:
toString(), buildString(StringBuffer)

writeAsXml

protected void writeAsXml(java.io.BufferedWriter out,
                          java.lang.String indent)
                   throws java.io.IOException
Print this query expression tree as XML.

This method is called by method writeAsXml().

Parameters:
out - Output stream to write to.
indent - Indentation prefix for each line of XML text output.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
Since:
1.6, 2001-04-02
See Also:
writeAsXml

writeStringAsXml

protected void writeStringAsXml(java.io.BufferedWriter out,
                                java.lang.String indent,
                                java.lang.String arg)
                         throws java.io.IOException
Print a query expression string argument tree as XML.

This method is called by method writeAsXml().

Parameters:
out - Output stream to write to.
indent - Indentation prefix for each line of XML text output.
arg - An expression operand from a query expression tree.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
Since:
1.6, 2001-04-02
See Also:
writeAsXml