org.josql.expressions
Class BindVariable

java.lang.Object
  extended by org.josql.expressions.Expression
      extended by org.josql.expressions.ValueExpression
          extended by org.josql.expressions.BindVariable

public class BindVariable
extends ValueExpression

This class represents a "bind variable" used within a SQL statement. A bind variable can be either:

Examples

   SELECT :_query,
          :_allobjs,
          :_currobj
   FROM java.lang.Object

   SELECT name
   FROM   java.io.File
   WHERE  length(:_currobj, name) > :length
   AND    length > avg(:_query,:_allobjs,length)
   AND    path LIKE '%' + ?
 

Accessors

It is also possible for bind variables (including the special variables) to have accessors. For example:

   SELECT :_query.variables
   FROM   java.lang.Object
 

Would cause all the bind variables in the query to be returned. Also, if the ? in the next query is an instance of java.lang.String.

   SELECT ?.length
   FROM   java.lang.Object
 


Field Summary
static String SPECIAL_NAME_PREFIX
           
 
Constructor Summary
BindVariable()
           
 
Method Summary
 boolean equals(Object o)
           
 Object evaluate(Object o, Query q)
          Evaluates the value of this bind variable.
 String getAccessor()
           
 Class getExpectedReturnType(Query q)
          Get the expected return type.
 String getName()
           
 Object getValue(Object o, Query q)
          Gets the value of this bind variable.
 boolean hasFixedResult(Query q)
          Will always return false since a bind variable cannot be fixed.
 void init(Query q)
          Initialises this bind variable.
 boolean isAnonymous()
           
 boolean isTrue(Object o, Query q)
          Returns whether the value of this bind variable represents a true value.
 void setAccessor(String a)
           
 void setAnonymous(boolean v)
           
 void setName(String name)
           
 String toString()
          Returns a string version of this bind variable.
 
Methods inherited from class org.josql.expressions.Expression
isBracketed, setBracketed
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SPECIAL_NAME_PREFIX

public static final String SPECIAL_NAME_PREFIX
See Also:
Constant Field Values
Constructor Detail

BindVariable

public BindVariable()
Method Detail

equals

public boolean equals(Object o)
Overrides:
equals in class Object

getAccessor

public String getAccessor()

setAccessor

public void setAccessor(String a)

getExpectedReturnType

public Class getExpectedReturnType(Query q)
                            throws QueryParseException
Get the expected return type. The exact class returned here is dependent (obviously) on what the bind variable represents. Wherever possible it attempts to get the most specific class for the variable. It is generally better to set the variables prior to executing the: Query.parse(String) method to ensure that the correct class is returned here.

Specified by:
getExpectedReturnType in class Expression
Parameters:
q - The Query object.
Returns:
The return type class or java.lang.Object.class if the class cannot be determined.
Throws:
QueryParseException - If the type cannot be determined.

init

public void init(Query q)
          throws QueryParseException
Initialises this bind variable. If the bind variable is "anonymous" then a name is gained for it from the Query object. If there is a value then it is gained from the Query object and cached. Also, if there is an accessor defined then it is inited where possible.

Specified by:
init in class Expression
Parameters:
q - The Query object.
Throws:
QueryParseException - If the bind variable cannot be inited.

getName

public String getName()

isAnonymous

public boolean isAnonymous()

setAnonymous

public void setAnonymous(boolean v)

setName

public void setName(String name)

getValue

public Object getValue(Object o,
                       Query q)
                throws QueryExecutionException
Gets the value of this bind variable.

Overrides:
getValue in class ValueExpression
Parameters:
o - The current object. Note that this variable isn't used in this method.
q - The Query object.
Returns:
The value.
Throws:
QueryExecutionException - If something goes wrong during the accessing of the value.

isTrue

public boolean isTrue(Object o,
                      Query q)
               throws QueryExecutionException
Returns whether the value of this bind variable represents a true value. See: ArithmeticExpression.isTrue(Object,Query) for details of how the return value is determined.

Specified by:
isTrue in class Expression
Parameters:
o - The current object. Not used in this method.
q - The Query object.
Returns:
true if the bind variable evaluates to true.
Throws:
QueryExecutionException - If a problem occurs during evaluation.

evaluate

public Object evaluate(Object o,
                       Query q)
                throws QueryExecutionException
Evaluates the value of this bind variable. This is just a thin-wrapper around: getValue(Object,Query).

Specified by:
evaluate in class ValueExpression
Parameters:
o - The current object, not used in this method.
q - The Query object.
Returns:
The value of this bind variable.
Throws:
QueryExecutionException - If there is a problem getting the value.

toString

public String toString()
Returns a string version of this bind variable. Returns in the form: ? | ":" [ "_" ] Name

Specified by:
toString in class Expression
Returns:
A string version of the bind variable.

hasFixedResult

public boolean hasFixedResult(Query q)
Will always return false since a bind variable cannot be fixed.

Specified by:
hasFixedResult in class Expression
Parameters:
q - The Query object.
Returns:
false always.


  Copyright © 2008 Gary Bentley. All Rights Reserved.