1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * Command.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | import java.sql.*; |
36 | |
37 | |
38 | abstract class Command { |
39 | |
40 | int type; |
41 | String catalog; |
42 | String name; |
43 | |
44 | SSResultSet rs; |
45 | int updateCount = -1; |
46 | |
47 | /** List of Columns */ |
48 | final Expressions columnExpressions; |
49 | |
50 | /** List of ExpressionValue */ |
51 | Expressions params = new Expressions(); |
52 | |
53 | final Logger log; |
54 | |
55 | Command(Logger log){ |
56 | this.log = log; |
57 | this.columnExpressions = new Expressions(); |
58 | } |
59 | |
60 | Command(Logger log, Expressions columnExpressions){ |
61 | this.log = log; |
62 | this.columnExpressions = columnExpressions; |
63 | } |
64 | |
65 | |
66 | /** |
67 | * Add a Expression that returns the value for Column. This method is used |
68 | * from SQLParser for different Commands (CommandSelect, CommandInsert). |
69 | * @see SQLParser#select() |
70 | * @see SQLParser#insert() |
71 | */ |
72 | void addColumnExpression( Expression column ){ |
73 | columnExpressions.add( column ); |
74 | } |
75 | |
76 | void addParameter( ExpressionValue param ){ |
77 | params.add( param ); |
78 | } |
79 | |
80 | |
81 | /** |
82 | * check if all parameters are set |
83 | */ |
84 | void verifyParams() throws SQLException{ |
85 | for(int p=0; p<params.size(); p++){ |
86 | if(((ExpressionValue)params.get(p)).isEmpty()) |
87 | throw Utils.createSQLException("Parameter " + (p+1) + " is empty."); |
88 | } |
89 | } |
90 | |
91 | /** |
92 | * Clear all parameters of a PreparedStatement |
93 | */ |
94 | void clearParams(){ |
95 | for(int p=0; p<params.size(); p++){ |
96 | ((ExpressionValue)params.get(p)).clear(); |
97 | } |
98 | } |
99 | |
100 | /** |
101 | * Get a PreparedStatement parameter. |
102 | * The idx starts with 1. |
103 | */ |
104 | private ExpressionValue getParam(int idx) throws SQLException{ |
105 | if(idx < 1 || idx > params.size()) |
106 | throw Utils.createSQLException("Parameter index " +idx+ " out of range. The value must be between 1 and " + params.size()); |
107 | return ((ExpressionValue)params.get(idx-1)); |
108 | } |
109 | |
110 | /** |
111 | * Set value of a PreparedStatement parameter. |
112 | * The idx starts with 1. |
113 | */ |
114 | void setParamValue(int idx, Object value, int dataType) throws SQLException{ |
115 | getParam(idx).set( value, dataType ); |
116 | if(log.isLogging()){ |
117 | log.println("param"+idx+'='+value+"; type="+dataType); |
118 | } |
119 | } |
120 | |
121 | /** |
122 | * Set value of a PreparedStatement parameter. |
123 | * The idx starts with 1. |
124 | */ |
125 | void setParamValue(int idx, Object value, int dataType, int length) throws SQLException{ |
126 | getParam(idx).set( value, dataType, length ); |
127 | if(log.isLogging()){ |
128 | log.println("param"+idx+'='+value+"; type="+dataType+"; length="+length); |
129 | } |
130 | } |
131 | |
132 | final void execute(SSConnection con, SSStatement st) throws SQLException{ |
133 | int savepoint = con.getSavepoint(); |
134 | try{ |
135 | executeImpl( con, st ); |
136 | }catch(Throwable e){ |
137 | con.rollback(savepoint); |
138 | throw Utils.createSQLException(e); |
139 | }finally{ |
140 | if(con.getAutoCommit()) con.commit(); |
141 | } |
142 | } |
143 | |
144 | abstract void executeImpl(SSConnection con, SSStatement st) throws Exception; |
145 | |
146 | SSResultSet getQueryResult() throws SQLException{ |
147 | if(rs == null) |
148 | throw Utils.createSQLException("No ResultSet was produce."); |
149 | return rs; |
150 | } |
151 | |
152 | SSResultSet getResultSet(){ |
153 | return rs; |
154 | } |
155 | |
156 | int getUpdateCount(){ |
157 | return updateCount; |
158 | } |
159 | |
160 | |
161 | /** |
162 | * Set the max rows. Need to be override in the Commands that support it. |
163 | */ |
164 | void setMaxRows(int max){} |
165 | |
166 | |
167 | int getMaxRows(){return 0;} |
168 | } |