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,ScriptBase,Set,SetSessionCharacteristics,TransactionCommand
public abstract class Prepared extends java.lang.ObjectA prepared statement.
-
-
Field Summary
Fields Modifier and Type Field Description private Commandcommandprotected booleancreateWhether to create a new object (for indexes).private java.util.List<TableView>cteCleanupsCommon table expressions (CTE) in queries require us to create temporary views, which need to be cleaned up once a command is done executing.private longcurrentRowNumberprivate longmodificationMetaIdprotected java.util.ArrayList<Parameter>parametersThe list of parameters.private intpersistedObjectIdUsed to preserve object identities on database startup.protected booleanprepareAlwaysIf the query should be prepared before each execution.private introwScanCountprotected SessionLocalsessionThe session.protected java.lang.StringsqlStatementThe SQL string.protected java.util.ArrayList<Token>sqlTokensThe SQL tokens.
-
Constructor Summary
Constructors Constructor Description Prepared(SessionLocal session)Create a new object.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcheckCanceled()Check if this statement was canceled.protected voidcheckParameters()Check if all parameters have been set.voidcollectDependencies(java.util.HashSet<DbObject> dependencies)Find and collect all DbObjects, this Prepared depends on.java.util.List<TableView>getCteCleanups()longgetCurrentRowNumber()Get the current row number.(package private) longgetModificationMetaId()Get the meta data modification id of the database when this statement was compiled.protected intgetObjectId()Get the current object id, or get a new id from the database.java.util.ArrayList<Parameter>getParameters()Get the parameter list.intgetPersistedObjectId()Get the object id to use for the database object that is created in this statement.java.lang.StringgetPlanSQL(int sqlFlags)Get the SQL statement with the execution plan.SessionLocalgetSession()static java.lang.StringgetSimpleSQL(Expression[] list)Get the SQL snippet of the expression list.java.lang.StringgetSQL()Get the SQL statement.java.util.ArrayList<Token>getSQLTokens()Get the SQL tokens.abstract intgetType()Get the command type as defined in CommandInterfacebooleanisCacheable()booleanisQuery()Check if this object is a query.booleanisReadOnly()Check if this command is read only.abstract booleanisTransactional()Check if this command is transactional.booleanneedRecompile()Check if the statement needs to be re-compiled.voidprepare()Prepare this statement.ResultInterfacequery(long maxrows)Execute the query.abstract ResultInterfacequeryMeta()Get an empty result set containing the meta data.voidsetCommand(Command command)Set the command.voidsetCteCleanups(java.util.List<TableView> cteCleanups)Set the temporary views created for CTE's.voidsetCurrentRowNumber(long rowNumber)Set the current row number.(package private) voidsetModificationMetaId(long id)Set the meta data modification id of this statement.voidsetParameterList(java.util.ArrayList<Parameter> parameters)Set the parameter list of this statement.voidsetPersistedObjectId(int i)Set the persisted object id for this statement.voidsetPrepareAlways(boolean prepareAlways)Set the prepare always flag.private voidsetProgress()Notifies query progress via the DatabaseEventListenerprotected DbExceptionsetRow(DbException e, long rowId, java.lang.String values)Set the SQL statement of the exception to the given row.voidsetSession(SessionLocal currentSession)Set the session for this statement.voidsetSQL(java.lang.String sql, java.util.ArrayList<Token> sqlTokens)Set the SQL statement.java.lang.StringtoString()Convert the statement to a String.(package private) voidtrace(long startTimeNanos, long rowCount)Print information about the statement executed if info trace level is enabled.longupdate()Execute the statement.
-
-
-
Field Detail
-
session
protected SessionLocal session
The session.
-
sqlStatement
protected java.lang.String sqlStatement
The SQL string.
-
sqlTokens
protected java.util.ArrayList<Token> sqlTokens
The SQL tokens.
-
create
protected boolean create
Whether to create a new object (for indexes).
-
parameters
protected java.util.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.
-
modificationMetaId
private long modificationMetaId
-
command
private Command command
-
persistedObjectId
private int persistedObjectId
Used to preserve object identities on database startup.0if object is not stored,-1if object is stored and its ID is already read,>0if object is stored and its id is not yet read.
-
currentRowNumber
private long currentRowNumber
-
rowScanCount
private int rowScanCount
-
cteCleanups
private java.util.List<TableView> cteCleanups
Common table expressions (CTE) in queries require us to create temporary views, which need to be cleaned up once a command is done executing.
-
-
Constructor Detail
-
Prepared
public Prepared(SessionLocal 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 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
-
getModificationMetaId
long getModificationMetaId()
Get the meta data modification id of the database when this statement was compiled.- Returns:
- the meta data modification id
-
setModificationMetaId
void setModificationMetaId(long id)
Set the meta data modification id of this statement.- Parameters:
id- the new id
-
setParameterList
public void setParameterList(java.util.ArrayList<Parameter> parameters)
Set the parameter list of this statement.- Parameters:
parameters- the parameter list
-
getParameters
public java.util.ArrayList<Parameter> getParameters()
Get the parameter list.- Returns:
- the parameter list
-
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(java.lang.String sql, java.util.ArrayList<Token> sqlTokens)Set the SQL statement.- Parameters:
sql- the SQL statementsqlTokens- the SQL tokens
-
getSQL
public final java.lang.String getSQL()
Get the SQL statement.- Returns:
- the SQL statement
-
getSQLTokens
public final java.util.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 java.lang.String getPlanSQL(int sqlFlags)
Get the SQL statement with the execution plan.- Parameters:
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
-
trace
void trace(long startTimeNanos, long rowCount)Print information about the statement executed if info trace level is enabled.- Parameters:
startTimeNanos- when the statement was startedrowCount- the query or update row count
-
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
-
setProgress
private void setProgress()
Notifies query progress via the DatabaseEventListener
-
toString
public java.lang.String toString()
Convert the statement to a String.- Overrides:
toStringin classjava.lang.Object- Returns:
- the SQL statement
-
getSimpleSQL
public static java.lang.String getSimpleSQL(Expression[] list)
Get the SQL snippet of the expression list.- Parameters:
list- the expression list- Returns:
- the SQL snippet
-
setRow
protected DbException setRow(DbException e, long rowId, java.lang.String values)
Set the SQL statement of the exception to the given row.- Parameters:
e- the exceptionrowId- the row numbervalues- the values of the row- Returns:
- the exception
-
isCacheable
public boolean isCacheable()
-
getCteCleanups
public java.util.List<TableView> getCteCleanups()
- Returns:
- the temporary views created for CTE's.
-
setCteCleanups
public void setCteCleanups(java.util.List<TableView> cteCleanups)
Set the temporary views created for CTE's.- Parameters:
cteCleanups- the temporary views
-
getSession
public final SessionLocal getSession()
-
collectDependencies
public void collectDependencies(java.util.HashSet<DbObject> dependencies)
Find and collect all DbObjects, this Prepared depends on.- Parameters:
dependencies- collection of dependencies to populate
-
-