org.h2.command
Class Prepared

java.lang.Object
  extended by org.h2.command.Prepared
Direct Known Subclasses:
BackupCommand, Call, DefineCommand, Delete, ExecuteProcedure, ExplainPlan, Insert, Merge, NoOperation, Query, ScriptBase, Set, TransactionCommand, Update

public abstract class Prepared
extends java.lang.Object

A prepared statement.


Field Summary
protected  int headPos
          The position of the head record (used for indexes).
protected  ObjectArray<Parameter> parameters
          The list of parameters.
protected  boolean prepareAlways
          If the query should be prepared before each execution.
protected  Session session
          The session.
protected  java.lang.String sqlStatement
          The SQL string.
 
Constructor Summary
Prepared(Session session)
          Create a new object.
 
Method Summary
 void checkCanceled()
          Check if this statement was canceled.
protected  void checkParameters()
          Check if all parameters have been set.
protected  int getCurrentObjectId()
          Get the object id to use for the database object that is created in this statement.
 int getCurrentRowNumber()
          Get the current row number.
protected  int getObjectId(boolean needFresh, boolean dataFile)
          Get the current object id, or get a new id from the database.
 ObjectArray<Parameter> getParameters()
          Get the parameter list.
 java.lang.String getPlanSQL()
          Get the SQL statement with the execution plan.
 java.lang.String getSQL()
          Get the SQL statement.
protected  java.lang.String getSQL(Expression[] list)
          Get the SQL snippet of the expression list.
protected  java.lang.String getSQL(Value[] values)
          Get the SQL snippet of the value list.
 boolean isQuery()
          Check if this object is a query.
 boolean isReadOnly()
          Check if this command is read only.
abstract  boolean isTransactional()
          Check if this command is transactional.
 boolean needRecompile()
          Check if the statement needs to be re-compiled.
 void prepare()
          Prepare this statement.
 LocalResult query(int maxrows)
          Execute the query.
abstract  LocalResult queryMeta()
          Get an empty result set containing the meta data.
 void setCommand(Command command)
          Set the command.
protected  void setCurrentRowNumber(int rowNumber)
          Set the current row number.
 void setHeadPos(int headPos)
          Set the head position.
 void setObjectId(int i)
          Set the object id for this statement.
 void setParameterList(ObjectArray<Parameter> parameters)
          Set the parameter list of this statement.
 void setPrepareAlways(boolean prepareAlways)
          Set the prepare always flag.
protected  java.sql.SQLException setRow(java.sql.SQLException ex, int rowId, java.lang.String values)
          Set the SQL statement of the exception to the given row.
 void setSession(Session currentSession)
          Set the session for this statement.
 void setSQL(java.lang.String sql)
          Set the SQL statement.
 java.lang.String toString()
          Convert the statement to a String.
 int update()
          Execute the statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

session

protected Session session
The session.


sqlStatement

protected java.lang.String sqlStatement
The SQL string.


headPos

protected int headPos
The position of the head record (used for indexes).


parameters

protected ObjectArray<Parameter> parameters
The list of parameters.


prepareAlways

protected boolean prepareAlways
If the query should be prepared before each execution. This is set for queries with LIKE ?, because the query plan depends on the parameter value.

Constructor Detail

Prepared

public Prepared(Session session)
Create a new object.

Parameters:
session - the session
Method Detail

isTransactional

public abstract boolean isTransactional()
Check if this command is transactional. If it is not, then it forces the current transaction to commit.

Returns:
true if it is

queryMeta

public abstract LocalResult queryMeta()
                               throws java.sql.SQLException
Get an empty result set containing the meta data.

Returns:
an empty result set
Throws:
java.sql.SQLException

isReadOnly

public boolean isReadOnly()
Check if this command is read only.

Returns:
true if it is

needRecompile

public boolean needRecompile()
                      throws java.sql.SQLException
Check if the statement needs to be re-compiled.

Returns:
true if it must
Throws:
java.sql.SQLException

setParameterList

public void setParameterList(ObjectArray<Parameter> parameters)
Set the parameter list of this statement.

Parameters:
parameters - the parameter list

getParameters

public ObjectArray<Parameter> getParameters()
Get the parameter list.

Returns:
the parameter list

checkParameters

protected void checkParameters()
                        throws java.sql.SQLException
Check if all parameters have been set.

Throws:
java.sql.SQLException - if any parameter has not been set

setCommand

public void setCommand(Command command)
Set the command.

Parameters:
command - the new command

isQuery

public boolean isQuery()
Check if this object is a query.

Returns:
true if it is

prepare

public void prepare()
             throws java.sql.SQLException
Prepare this statement.

Throws:
java.sql.SQLException

update

public int update()
           throws java.sql.SQLException
Execute the statement.

Returns:
the update count
Throws:
java.sql.SQLException - if it is a query

query

public LocalResult query(int maxrows)
                  throws java.sql.SQLException
Execute the query.

Parameters:
maxrows - the maximum number of rows to return
Returns:
the result set
Throws:
java.sql.SQLException - if it is not a query

setSQL

public void setSQL(java.lang.String sql)
Set the SQL statement.

Parameters:
sql - the SQL statement

getSQL

public java.lang.String getSQL()
Get the SQL statement.

Returns:
the SQL statement

getCurrentObjectId

protected int getCurrentObjectId()
Get the object id to use for the database object that is created in this statement. This id is only set when the object is persistent. If not set, this method returns 0.

Returns:
the object id or 0 if not set

getObjectId

protected int getObjectId(boolean needFresh,
                          boolean dataFile)
Get the current object id, or get a new id from the database. The object id is used when creating new database object (CREATE statement).

Parameters:
needFresh - if a fresh id is required
dataFile - if the object id is used for the
Returns:
the object id

getPlanSQL

public java.lang.String getPlanSQL()
Get the SQL statement with the execution plan.

Returns:
the execution plan

checkCanceled

public void checkCanceled()
                   throws java.sql.SQLException
Check if this statement was canceled.

Throws:
java.sql.SQLException - if it was canceled

setObjectId

public void setObjectId(int i)
Set the object id for this statement.

Parameters:
i - the object id

setHeadPos

public void setHeadPos(int headPos)
Set the head position.

Parameters:
headPos - the head position

setSession

public void setSession(Session currentSession)
Set the session for this statement.

Parameters:
currentSession - the new session

setPrepareAlways

public void setPrepareAlways(boolean prepareAlways)
Set the prepare always flag. If set, the statement is re-compiled whenever it is executed.

Parameters:
prepareAlways - the new value

setCurrentRowNumber

protected void setCurrentRowNumber(int rowNumber)
                            throws java.sql.SQLException
Set the current row number.

Parameters:
rowNumber - the row number
Throws:
java.sql.SQLException

getCurrentRowNumber

public int getCurrentRowNumber()
Get the current row number.

Returns:
the row number

toString

public java.lang.String toString()
Convert the statement to a String.

Overrides:
toString in class java.lang.Object
Returns:
the SQL statement

getSQL

protected java.lang.String getSQL(Value[] values)
Get the SQL snippet of the value list.

Parameters:
values - the value list
Returns:
the SQL snippet

getSQL

protected java.lang.String getSQL(Expression[] list)
Get the SQL snippet of the expression list.

Parameters:
list - the expression list
Returns:
the SQL snippet

setRow

protected java.sql.SQLException setRow(java.sql.SQLException ex,
                                       int rowId,
                                       java.lang.String values)
Set the SQL statement of the exception to the given row.

Parameters:
ex - the exception
rowId - the row number
values - the values of the row
Returns:
the exception