1 /* Copyright (c) 2001-2015, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 package org.hsqldb;
33 
34 import org.hsqldb.HsqlNameManager.HsqlName;
35 import org.hsqldb.lib.OrderedHashSet;
36 import org.hsqldb.result.Result;
37 import org.hsqldb.result.ResultMetaData;
38 import org.hsqldb.result.ResultProperties;
39 
40 /**
41  * Base class for compiled statement objects.<p>
42  *
43  * @author Fred Toussi (fredt@users dot sourceforge.net)
44  * @version 2.0.1
45  * @since 1.9.0
46  */
47 public abstract class Statement {
48 
49     static final int META_RESET_VIEWS      = 1;
50     static final int META_RESET_STATEMENTS = 2;
51 
52     //
53     static final Statement[] emptyArray = new Statement[]{};
54 
55     //
56     final int type;
57     int       group;
58     boolean   isLogged            = true;
59     boolean   isValid             = true;
60     int       statementReturnType = StatementTypes.RETURN_COUNT;
61 
62     /** the default schema name used to resolve names in the sql */
63     HsqlName schemaName;
64 
65     /** root in PSM */
66     Routine root;
67 
68     /** parent in PSM */
69     StatementCompound parent;
70     boolean           isError;
71     boolean           isTransactionStatement;
72     boolean           isExplain;
73 
74     /** SQL string for the statement */
75     String sql;
76 
77     /** id in StatementManager */
78     long id;
79 
80     /** compileTimestamp */
81     long compileTimestamp;
82 
83     /** table names read - for concurrency control */
84     HsqlName[] readTableNames = HsqlName.emptyArray;
85 
86     /** table names written - for concurrency control */
87     HsqlName[] writeTableNames = HsqlName.emptyArray;
88 
89     //
90     OrderedHashSet references;
91 
92     //
93     int cursorPropertiesRequest;
94 
execute(Session session)95     public abstract Result execute(Session session);
96 
setParameters(ExpressionColumn[] params)97     public void setParameters(ExpressionColumn[] params) {}
98 
Statement(int type)99     Statement(int type) {
100         this.type = type;
101     }
102 
Statement(int type, int group)103     Statement(int type, int group) {
104         this.type  = type;
105         this.group = group;
106     }
107 
isError()108     public final boolean isError() {
109         return isError;
110     }
111 
isTransactionStatement()112     public boolean isTransactionStatement() {
113         return isTransactionStatement;
114     }
115 
isAutoCommitStatement()116     public boolean isAutoCommitStatement() {
117         return false;
118     }
119 
setCompileTimestamp(long ts)120     public void setCompileTimestamp(long ts) {
121         compileTimestamp = ts;
122     }
123 
getCompileTimestamp()124     public long getCompileTimestamp() {
125         return compileTimestamp;
126     }
127 
setSQL(String sql)128     public final void setSQL(String sql) {
129         this.sql = sql;
130     }
131 
getSQL()132     public String getSQL() {
133         return sql;
134     }
135 
getReferences()136     public OrderedHashSet getReferences() {
137         return references;
138     }
139 
setDescribe()140     public final void setDescribe() {
141         isExplain = true;
142     }
143 
describe(Session session)144     public abstract String describe(Session session);
145 
getSchemaName()146     public HsqlName getSchemaName() {
147         return schemaName;
148     }
149 
setSchemaHsqlName(HsqlName name)150     public final void setSchemaHsqlName(HsqlName name) {
151         schemaName = name;
152     }
153 
setID(long csid)154     public final void setID(long csid) {
155         id = csid;
156     }
157 
getID()158     public final long getID() {
159         return id;
160     }
161 
getType()162     public final int getType() {
163         return type;
164     }
165 
getGroup()166     public final int getGroup() {
167         return group;
168     }
169 
isValid()170     public final boolean isValid() {
171         return isValid;
172     }
173 
isLogged()174     public final boolean isLogged() {
175         return isLogged;
176     }
177 
clearVariables()178     public void clearVariables() {}
179 
resolve(Session session)180     public void resolve(Session session) {}
181 
getTableNamesForRead()182     public final HsqlName[] getTableNamesForRead() {
183         return readTableNames;
184     }
185 
getTableNamesForWrite()186     public final HsqlName[] getTableNamesForWrite() {
187         return writeTableNames;
188     }
189 
isCatalogLock()190     public boolean isCatalogLock() {
191 
192         switch (group) {
193 
194             case StatementTypes.X_SQL_SCHEMA_MANIPULATION :
195 
196                 // in MVCC log replay statement is not followed by COMMIT so no lock
197                 if (type == StatementTypes.ALTER_SEQUENCE) {
198                     return false;
199                 }
200 
201                 if (writeTableNames.length == 0) {
202                     return false;
203                 }
204             case StatementTypes.X_SQL_SCHEMA_DEFINITION :
205             case StatementTypes.X_HSQLDB_SCHEMA_MANIPULATION :
206             case StatementTypes.X_HSQLDB_DATABASE_OPERATION :
207                 return true;
208 
209             case StatementTypes.X_HSQLDB_NONBLOCK_OPERATION :
210             default :
211                 return false;
212         }
213     }
214 
isCatalogChange()215     public boolean isCatalogChange() {
216 
217         switch (group) {
218 
219             case StatementTypes.X_SQL_SCHEMA_DEFINITION :
220             case StatementTypes.X_SQL_SCHEMA_MANIPULATION :
221             case StatementTypes.X_HSQLDB_SCHEMA_MANIPULATION :
222                 return true;
223 
224             default :
225                 return false;
226         }
227     }
228 
setParent(StatementCompound statement)229     public void setParent(StatementCompound statement) {
230         this.parent = statement;
231     }
232 
setRoot(Routine root)233     public void setRoot(Routine root) {
234         this.root = root;
235     }
236 
hasGeneratedColumns()237     public boolean hasGeneratedColumns() {
238         return false;
239     }
240 
generatedResultMetaData()241     public ResultMetaData generatedResultMetaData() {
242         return null;
243     }
244 
setGeneratedColumnInfo(int mode, ResultMetaData meta)245     public void setGeneratedColumnInfo(int mode, ResultMetaData meta) {}
246 
getResultMetaData()247     public ResultMetaData getResultMetaData() {
248         return ResultMetaData.emptyResultMetaData;
249     }
250 
getParametersMetaData()251     public ResultMetaData getParametersMetaData() {
252         return ResultMetaData.emptyParamMetaData;
253     }
254 
getResultProperties()255     public int getResultProperties() {
256         return ResultProperties.defaultPropsValue;
257     }
258 
getStatementReturnType()259     public int getStatementReturnType() {
260         return statementReturnType;
261     }
262 
getCursorPropertiesRequest()263     public int getCursorPropertiesRequest() {
264         return cursorPropertiesRequest;
265     }
266 
setCursorPropertiesRequest(int props)267     public void setCursorPropertiesRequest(int props) {
268         cursorPropertiesRequest = props;
269     }
270 
clearStructures(Session session)271     public void clearStructures(Session session) {}
272 }
273