Package org.h2.command

# Class Prepared

java.lang.Object
org.h2.command.Prepared

Direct Known Subclasses:
  `BackupCommand`, `Call`, `DataChangeStatement`, `DefineCommand`, `ExecuteImmediate`, `ExecuteProcedure`, `Explain`, `Help`, `NoOperation`, `Query`, `RunScriptCommand`, `ScriptCommand`, `Set`, `SetSessionCharacteristics`, `TransactionCommand`

public abstract class Prepared extends Object
A prepared statement.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected boolean`
`create`
Whether to create a new object (for indexes).
`protected ArrayList<Parameter>`
`parameters`
The list of parameters.
`protected boolean`
`prepareAlways`
If the query should be prepared before each execution.
`protected SessionLocal`
`session`
The session.
`protected String`
`sqlStatement`
The SQL string.
`protected ArrayList<Token>`
`sqlTokens`
The SQL tokens.

## Constructor Summary

Constructors
Constructor
Description
`Prepared(SessionLocal session)`
Create a new object.

## Method Summary

Modifier and Type
Method
Description
`void`
`checkCanceled()`
Check if this statement was canceled.
`protected void`
`checkParameters()`
Check if all parameters have been set.
`void`
`collectDependencies(HashSet<DbObject> dependencies)`
Find and collect all DbObjects, this Prepared depends on.
`long`
`getCurrentRowNumber()`
Get the current row number.
`protected final Database`
`getDatabase()`
`protected int`
`getObjectId()`
Get the current object id, or get a new id from the database.
`ArrayList<Parameter>`
`getParameters()`
Get the parameter list.
`int`
`getPersistedObjectId()`
Get the object id to use for the database object that is created in this statement.
`final String`
`getPlanSQL(int sqlFlags)`
Get the SQL statement with the execution plan.
`StringBuilder`
`getPlanSQL(StringBuilder builder, int sqlFlags)`
Appends the SQL statement with the execution plan.
`final SessionLocal`
`getSession()`
`static String`
`getSimpleSQL(Expression[] list)`
Get the SQL snippet of the expression list.
`final String`
`getSQL()`
Get the SQL statement.
`final ArrayList<Token>`
`getSQLTokens()`
Get the SQL tokens.
`abstract int`
`getType()`
Get the command type as defined in CommandInterface
`boolean`
`isCacheable()`
`boolean`
`isQuery()`
Check if this object is a query.
`boolean`
`isReadOnly()`
Check if this command is read only.
`boolean`
`isRetryable()`
Returns is this command can be repeated again on locking failure.
`abstract boolean`
`isTransactional()`
Check if this command is transactional.
`boolean`
`isWithParamValues()`
Returns whether values of parameters were specified in SQL.
`boolean`
`needRecompile()`
Check if the statement needs to be re-compiled.
`void`
`prepare()`
Prepare this statement.
`ResultInterface`
`query(long maxrows)`
Execute the query.
`abstract ResultInterface`
`queryMeta()`
Get an empty result set containing the meta data.
`void`
`setCommand(Command command)`
Set the command.
`void`
`setCurrentRowNumber(long rowNumber)`
Set the current row number.
`void`
`setParameterList(ArrayList<Parameter> parameters)`
Set the parameter list of this statement.
`void`
`setPersistedObjectId(int i)`
Set the persisted object id for this statement.
`void`
`setPrepareAlways(boolean prepareAlways)`
Set the prepare always flag.
`protected final DbException`
`setRow(DbException e, long rowId, String values)`
Set the SQL statement of the exception to the given row.
`void`
`setSession(SessionLocal currentSession)`
Set the session for this statement.
`final void`
`setSQL(String sql, ArrayList<Token> sqlTokens)`
Set the SQL statement.
`void`
`setWithParamValues(boolean withParamValues)`
Sets whether values of parameters were specified in SQL.
`String`
`toString()`
Convert the statement to a String.
`long`
`update()`
Execute the statement.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Field Details

### session

protected SessionLocal session
The session.

### sqlStatement

protected String sqlStatement
The SQL string.

### sqlTokens

protected ArrayList<Token> sqlTokens
The SQL tokens.

### create

protected boolean create
Whether to create a new object (for indexes).

### parameters

protected ArrayList<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 Details

### Prepared

public Prepared(SessionLocal session)
Create a new object.

Parameters:
  `session` - the session

## Method Details

### 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 ResultInterface queryMeta()
Get an empty result set containing the meta data.

Returns:
  the result set

### getType

public abstract int getType()
Get the command type as defined in CommandInterface

Returns:
  the statement type

### isReadOnly

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

Returns:
  true if it is

### needRecompile

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

Returns:
  true if it must

### setParameterList

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

Parameters:
  `parameters` - the parameter list

### getParameters

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

Returns:
  the parameter list

### isWithParamValues

public boolean isWithParamValues()
Returns whether values of parameters were specified in SQL.

Returns:
  are values of parameters were specified in SQL

### setWithParamValues

public void setWithParamValues(boolean withParamValues)
Sets whether values of parameters were specified in SQL.

Parameters:
  `withParamValues` - are values of parameters were specified in SQL

### checkParameters

protected void checkParameters()
Check if all parameters have been set.

Throws:
  `DbException` - 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()
Prepare this statement.

### update

public long update()
Execute the statement.

Returns:
  the update count
Throws:
  `DbException` - if it is a query

### query

public ResultInterface query(long maxrows)
Execute the query.

Parameters:
  `maxrows` - the maximum number of rows to return
Returns:
  the result set
Throws:
  `DbException` - if it is not a query

### setSQL

public final void setSQL(String sql, ArrayList<Token> sqlTokens)
Set the SQL statement.

Parameters:
  `sql` - the SQL statement
  `sqlTokens` - the SQL tokens

### getSQL

public final String getSQL()
Get the SQL statement.

Returns:
  the SQL statement

### getSQLTokens

public final ArrayList<Token> getSQLTokens()
Get the SQL tokens.

Returns:
  the SQL tokens

### getPersistedObjectId

public int getPersistedObjectId()
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 already persisted. If not set, this method returns 0.

Returns:
  the object id or 0 if not set

### getObjectId

protected int getObjectId()
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). This method may be called only once.

Returns:
  the object id

### getPlanSQL

public final String getPlanSQL(int sqlFlags)
Get the SQL statement with the execution plan.

Parameters:
  `sqlFlags` - formatting flags
Returns:
  the execution plan

### getPlanSQL

public StringBuilder getPlanSQL(StringBuilder builder, int sqlFlags)
Appends the SQL statement with the execution plan.

Parameters:
  `builder` - string builder
  `sqlFlags` - formatting flags
Returns:
  the execution plan

### checkCanceled

public void checkCanceled()
Check if this statement was canceled.

Throws:
  `DbException` - if it was canceled

### setPersistedObjectId

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

Parameters:
  `i` - the object id

### setSession

public void setSession(SessionLocal 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

public void setCurrentRowNumber(long rowNumber)
Set the current row number.

Parameters:
  `rowNumber` - the row number

### getCurrentRowNumber

public long getCurrentRowNumber()
Get the current row number.

Returns:
  the row number

### toString

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

Overrides:
  `toString` in class `Object`
Returns:
  the SQL statement

### getSimpleSQL

public static String getSimpleSQL(Expression[] list)
Get the SQL snippet of the expression list.

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

### setRow

protected final DbException setRow(DbException e, long rowId, String values)
Set the SQL statement of the exception to the given row.

Parameters:
  `e` - the exception
  `rowId` - the row number
  `values` - the values of the row
Returns:
  the exception

### isCacheable

public boolean isCacheable()

### getSession

public final SessionLocal getSession()

### collectDependencies

public void collectDependencies(HashSet<DbObject> dependencies)
Find and collect all DbObjects, this Prepared depends on.

Parameters:
  `dependencies` - collection of dependencies to populate

### getDatabase

protected final Database getDatabase()

### isRetryable

public boolean isRetryable()
Returns is this command can be repeated again on locking failure.

Returns:
  is this command can be repeated again on locking failure