|
1
|
|
package mobisnap.mobile_trx;
|
|
2
|
|
|
|
3
|
|
import java.io.*;
|
|
4
|
|
import java.sql.*;
|
|
5
|
|
import java.util.*;
|
|
6
|
|
import mobisnap.*;
|
|
7
|
|
import nmp.dbms.*;
|
|
8
|
|
import nmp.dbms.JDBC.*;
|
|
9
|
|
import mobisnap.common.reservation.*;
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
public class MobisnapInterpreter
|
|
15
|
|
{
|
|
16
|
|
protected Jdbc_Sql database;
|
|
17
|
|
protected TransactionHandle transaction;
|
|
18
|
|
protected MobisnapSQL interpreter;
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
protected Vector refRsrvs;
|
|
24
|
|
protected Vector usedRsrvs;
|
|
25
|
|
|
|
26
|
0
|
public MobisnapInterpreter( String driverName, String url, String name, String pwd ) throws SQLException {
|
|
27
|
0
|
database = new Jdbc_Sql( driverName, url, name, pwd);
|
|
28
|
0
|
transaction = database.getTransactionHandle();
|
|
29
|
0
|
refRsrvs = new Vector();
|
|
30
|
0
|
usedRsrvs = new Vector();
|
|
31
|
|
}
|
|
32
|
|
|
|
33
|
0
|
public MobisnapInterpreter( Jdbc_Sql database) throws SQLException {
|
|
34
|
0
|
this.database = database;
|
|
35
|
0
|
transaction = database.getTransactionHandle();
|
|
36
|
0
|
refRsrvs = new Vector();
|
|
37
|
0
|
usedRsrvs = new Vector();
|
|
38
|
|
}
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
0
|
public void release() throws SQLException {
|
|
44
|
0
|
if( transaction != null) {
|
|
45
|
0
|
transaction.release();
|
|
46
|
0
|
transaction = null;
|
|
47
|
|
}
|
|
48
|
0
|
if( database != null) {
|
|
49
|
0
|
database.closeAll();
|
|
50
|
0
|
database = null;
|
|
51
|
|
}
|
|
52
|
|
}
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
0
|
protected void initTransaction( Reader reader) throws Exception {
|
|
58
|
0
|
if( transaction == null)
|
|
59
|
0
|
throw new SQLException( "Interpreter not ready");
|
|
60
|
0
|
if( interpreter == null) {
|
|
61
|
0
|
MobisnapSQL.transaction = transaction;
|
|
62
|
0
|
transaction.commit();
|
|
63
|
0
|
MobisnapSQL.names = new MSQLNames();
|
|
64
|
0
|
interpreter = new MobisnapSQL( reader);
|
|
65
|
|
} else {
|
|
66
|
0
|
transaction.commit();
|
|
67
|
0
|
MobisnapSQL.names.reset();
|
|
68
|
0
|
MobisnapSQL.ReInit( reader);
|
|
69
|
|
}
|
|
70
|
|
}
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
0
|
protected void initQuery( Reader reader) throws Exception {
|
|
76
|
0
|
if( database == null)
|
|
77
|
0
|
throw new SQLException( "Interpreter not ready");
|
|
78
|
0
|
if( interpreter == null) {
|
|
79
|
0
|
MobisnapSQL.transaction = transaction;
|
|
80
|
0
|
transaction.commit();
|
|
81
|
0
|
MobisnapSQL.names = new MSQLNames();
|
|
82
|
0
|
interpreter = new MobisnapSQL( reader);
|
|
83
|
|
} else {
|
|
84
|
0
|
transaction.commit();
|
|
85
|
0
|
MobisnapSQL.names.reset();
|
|
86
|
0
|
MobisnapSQL.ReInit( reader);
|
|
87
|
|
}
|
|
88
|
|
}
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
0
|
protected ASTDatabaseDefinition databaseDefinition() throws Exception {
|
|
95
|
0
|
return MobisnapSQL.DatabaseDefinition();
|
|
96
|
|
}
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
0
|
protected ASTCreateSnapshot createSnapshot() throws Exception {
|
|
103
|
0
|
return MobisnapSQL.CreateSnapshot();
|
|
104
|
|
}
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
0
|
protected ASTUpdateSnapshot updateSnapshot() throws Exception {
|
|
111
|
0
|
return MobisnapSQL.UpdateSnapshot();
|
|
112
|
|
}
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
0
|
protected ASTGetReservation getReservation() throws Exception {
|
|
119
|
0
|
return MobisnapSQL.GetReservation();
|
|
120
|
|
}
|
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
0
|
protected ASTCreateTable createTable( java.util.HashMap map) throws Exception {
|
|
127
|
0
|
return MobisnapSQL.CreateTable( map);
|
|
128
|
|
}
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
0
|
protected ASTDropTable dropTable( java.util.HashMap map) throws Exception {
|
|
135
|
0
|
return MobisnapSQL.DropTable(map);
|
|
136
|
|
}
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
0
|
protected ASTSQLStatement sqlStatement() throws Exception {
|
|
143
|
0
|
return MobisnapSQL.SQLStatement();
|
|
144
|
|
}
|
|
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
0
|
public synchronized SQLVectorResult processQuery( int msql_type, Reader reader) throws Exception {
|
|
150
|
0
|
initQuery( reader);
|
|
151
|
0
|
ASTQueryStatement node = MobisnapSQL.QueryStatement();
|
|
152
|
0
|
return node.getResult( msql_type);
|
|
153
|
|
}
|
|
154
|
|
|
|
155
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
0
|
private void initReservations() {
|
|
159
|
0
|
refRsrvs.removeAllElements();
|
|
160
|
0
|
usedRsrvs.removeAllElements();
|
|
161
|
|
}
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
0
|
public void refReservation( ReservationUseBase rsrv) {
|
|
167
|
0
|
refRsrvs.addElement( rsrv);
|
|
168
|
|
}
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
0
|
public void useReservation( ReservationUseBase rsrv) {
|
|
176
|
0
|
usedRsrvs.addElement( rsrv);
|
|
177
|
|
}
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
|
182
|
0
|
protected void commitReservations() {
|
|
183
|
|
|
|
184
|
|
}
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
|
189
|
|
|
|
190
|
0
|
protected void rollbackReservations() {
|
|
191
|
|
|
|
192
|
|
}
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
|
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
0
|
public ReservationUseBase getUsedReservation( String table, String column, SimpleNode where) {
|
|
203
|
0
|
where = where.simplify( MobisnapConstants.MSQL_SERVER, false);
|
|
204
|
0
|
Enumeration enum = usedRsrvs.elements();
|
|
205
|
0
|
while( enum.hasMoreElements()) {
|
|
206
|
0
|
ReservationUseBase rsrv = (ReservationUseBase)enum.nextElement();
|
|
207
|
0
|
if( rsrv.getRsrv().checkReservation( table, column, where))
|
|
208
|
0
|
return rsrv;
|
|
209
|
|
}
|
|
210
|
0
|
return null;
|
|
211
|
|
}
|
|
212
|
|
|
|
213
|
|
|
|
214
|
|
|
|
215
|
|
|
|
216
|
|
|
|
217
|
|
|
|
218
|
|
|
|
219
|
|
|
|
220
|
|
|
|
221
|
0
|
public ReservationUseBase getUsedReservation( String table, String column, Vector conditions) {
|
|
222
|
0
|
Enumeration enum = usedRsrvs.elements();
|
|
223
|
0
|
while( enum.hasMoreElements()) {
|
|
224
|
0
|
ReservationUseBase rsrv = (ReservationUseBase)enum.nextElement();
|
|
225
|
0
|
if( rsrv.getRsrv().checkReservation( table, column, conditions))
|
|
226
|
0
|
return rsrv;
|
|
227
|
|
}
|
|
228
|
0
|
return null;
|
|
229
|
|
}
|
|
230
|
|
|
|
231
|
|
|
|
232
|
|
|
|
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
|
237
|
|
|
|
238
|
0
|
public int processMSQLTrx( Reader reader) throws Exception {
|
|
239
|
0
|
return processMSQLTrx( MobisnapConstants.MSQL_SERVER, reader);
|
|
240
|
|
}
|
|
241
|
0
|
public synchronized int processMSQLTrx( int msql_type, Reader reader) throws Exception {
|
|
242
|
0
|
initTransaction( reader);
|
|
243
|
0
|
SimpleNode mainNode = MobisnapSQL.CompilationUnit() ;
|
|
244
|
0
|
if( msql_type == MobisnapConstants.MSQL_RESERVATION_CLIENT) {
|
|
245
|
0
|
try {
|
|
246
|
0
|
initReservations();
|
|
247
|
0
|
mainNode.process( msql_type);
|
|
248
|
0
|
throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
|
|
249
|
|
} catch( CommitException e) {
|
|
250
|
0
|
commitReservations();
|
|
251
|
0
|
return MobisnapConstants.MSQLCR_RESERVATION_COMMIT;
|
|
252
|
|
} catch( UnknownException e) {
|
|
253
|
0
|
transaction.rollback();
|
|
254
|
0
|
return MobisnapConstants.MSQLCR_UNKNOWN;
|
|
255
|
|
} catch( RollbackException e) {
|
|
256
|
|
|
|
257
|
|
} catch( NoReservationException e) {
|
|
258
|
|
|
|
259
|
|
} finally {
|
|
260
|
0
|
rollbackReservations();
|
|
261
|
|
}
|
|
262
|
0
|
msql_type = MobisnapConstants.MSQL_TENTATIVE_CLIENT;
|
|
263
|
|
}
|
|
264
|
0
|
if( msql_type == MobisnapConstants.MSQL_SERVER) {
|
|
265
|
0
|
try {
|
|
266
|
0
|
mainNode.process( msql_type);
|
|
267
|
0
|
throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
|
|
268
|
|
} catch( CommitException e) {
|
|
269
|
0
|
return MobisnapConstants.MSQLSR_COMMIT;
|
|
270
|
|
} catch( RollbackException e) {
|
|
271
|
0
|
return MobisnapConstants.MSQLSR_ABORT;
|
|
272
|
|
} catch( UnknownException e) {
|
|
273
|
0
|
transaction.rollback();
|
|
274
|
0
|
return MobisnapConstants.MSQLSR_ABORT;
|
|
275
|
|
}
|
|
276
|
|
} else {
|
|
277
|
0
|
try {
|
|
278
|
0
|
mainNode.process( msql_type);
|
|
279
|
0
|
throw new mobisnap.MobisnapException( "Internal error - compilation unit ended without a result");
|
|
280
|
|
} catch( CommitException e) {
|
|
281
|
0
|
return MobisnapConstants.MSQLCR_TENTATIVE_COMMIT;
|
|
282
|
|
} catch( RollbackException e) {
|
|
283
|
0
|
return MobisnapConstants.MSQLCR_TENTATIVE_ABORT;
|
|
284
|
|
} catch( UnknownException e) {
|
|
285
|
0
|
transaction.rollback();
|
|
286
|
0
|
return MobisnapConstants.MSQLCR_UNKNOWN;
|
|
287
|
|
}
|
|
288
|
|
}
|
|
289
|
|
}
|
|
290
|
|
|
|
291
|
|
|
|
292
|
|
|
|
293
|
|
|
|
294
|
0
|
public synchronized void processDatabaseDef( Reader reader, Hashtable table) throws Exception {
|
|
295
|
0
|
initQuery( reader);
|
|
296
|
0
|
ASTDatabaseDefinition node = databaseDefinition();
|
|
297
|
0
|
Iterator it = node.definitions().iterator();
|
|
298
|
0
|
while( it.hasNext()) {
|
|
299
|
0
|
SimpleNode aux = (SimpleNode)it.next();
|
|
300
|
0
|
if( aux instanceof ASTCreateTable) {
|
|
301
|
0
|
ASTCreateTable ct = (ASTCreateTable)aux;
|
|
302
|
0
|
StringBuffer buffer = new StringBuffer();
|
|
303
|
0
|
ct.sourceCode( MobisnapConstants.MSQL_SERVER, buffer);
|
|
304
|
0
|
table.put( ct.tableName( MobisnapConstants.MSQL_SERVER), buffer.toString().trim());
|
|
305
|
|
} else
|
|
306
|
0
|
System.err.println( "instruction not processed");
|
|
307
|
|
}
|
|
308
|
|
}
|
|
309
|
|
|
|
310
|
0
|
public static void main( String[] args) {
|
|
311
|
0
|
try {
|
|
312
|
0
|
MobisnapInterpreter interpreter = null;
|
|
313
|
0
|
try {
|
|
314
|
0
|
System.out.println( "Usage:\nMobisnapInterpreter filename [url user pwd [driver]]");
|
|
315
|
0
|
if( args.length == 5)
|
|
316
|
0
|
interpreter = new MobisnapInterpreter( args[4], args[1], args[2], args[3]);
|
|
317
|
0
|
else if( args.length == 4)
|
|
318
|
0
|
interpreter = new MobisnapInterpreter( "sun.jdbc.odbc.JdbcOdbcDriver", args[1], args[2], args[3]);
|
|
319
|
|
else
|
|
320
|
0
|
interpreter = new MobisnapInterpreter( "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:wine", "nmp", "");
|
|
321
|
|
|
|
322
|
0
|
interpreter.processMSQLTrx( MobisnapConstants.MSQL_SERVER, new FileReader( args[0]));
|
|
323
|
|
|
|
324
|
|
} finally {
|
|
325
|
0
|
interpreter.release();
|
|
326
|
|
}
|
|
327
|
|
} catch( Exception e) {
|
|
328
|
0
|
e.printStackTrace();
|
|
329
|
|
}
|
|
330
|
|
}
|
|
331
|
|
}
|
|
332
|
|
|