KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > synth > Command


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.synth;
6
7 import java.sql.SQLException JavaDoc;
8 import java.util.HashMap JavaDoc;
9
10 class Command {
11     TestSynth config;
12     static final int CONNECT = 0, RESET = 1, DISCONNECT = 2,
13         CREATE_TABLE = 3, INSERT = 4, DROP_TABLE = 5,
14         SELECT = 6, DELETE = 7, UPDATE = 8, COMMIT = 9, ROLLBACK = 10,
15         AUTOCOMMIT_ON = 11, AUTOCOMMIT_OFF = 12,
16         CREATE_INDEX = 13, DROP_INDEX = 14, END = 15;
17     private int type;
18     private Table table;
19     private HashMap JavaDoc tables;
20     private Index index;
21     private Column[] columns;
22     private Value[] values;
23     private String JavaDoc condition;
24     private int nextAlias;
25     private String JavaDoc order;
26     String JavaDoc[] selectList;
27     private String JavaDoc join = "";
28     private Result result;
29     
30     static Command getDropTable(TestSynth config, Table table) {
31         return new Command(config, Command.DROP_TABLE, table);
32     }
33     
34     Command getCommit(TestSynth config) {
35         return new Command(config, Command.COMMIT);
36     }
37     
38     Command getRollback(TestSynth config) {
39         return new Command(config, Command.ROLLBACK);
40     }
41     
42     Command getSetAutoCommit(TestSynth config, boolean auto) {
43         int type = auto ? Command.AUTOCOMMIT_ON : Command.AUTOCOMMIT_OFF;
44         return new Command(config, type);
45     }
46     
47     static Command getConnect(TestSynth config) {
48         return new Command(config, CONNECT);
49     }
50     
51     static Command getReset(TestSynth config) {
52         return new Command(config, RESET);
53     }
54     
55     static Command getDisconnect(TestSynth config) {
56         return new Command(config, DISCONNECT);
57     }
58
59     static Command getEnd(TestSynth config) {
60         return new Command(config, END);
61     }
62
63     static Command getCreateTable(TestSynth config, Table table) {
64         return new Command(config, CREATE_TABLE, table);
65     }
66     
67     static Command getCreateIndex(TestSynth config, Index index) {
68         return new Command(config, CREATE_INDEX, index);
69     }
70     
71     static Command getRandomSelect(TestSynth config, Table table) {
72         Command command = new Command(config, Command.SELECT, table, "M");
73         command.selectList = Expression.getRandomSelectList(config, command);
74         // TODO group by, having, joins
75
command.condition = Expression.getRandomCondition(config, command).getSQL();
76         command.order = Expression.getRandomOrder(config, command);
77         return command;
78     }
79     
80     static Command getRandomSelectJoin(TestSynth config, Table table) {
81         Command command = new Command(config, Command.SELECT, table, "M");
82         int len = config.random().getLog(5)+1;
83         String JavaDoc globalJoinCondition = "";
84         for(int i=0; i<len; i++) {
85             Table t2 = config.randomTable();
86             String JavaDoc alias = "J" + i;
87             command.addSubqueryTable(alias, t2);
88             Expression joinOn = Expression.getRandomJoinOn(config, command, alias);
89             if(config.random().getBoolean(50)) {
90                 // regular join
91
if(globalJoinCondition.length()>0) {
92                     globalJoinCondition += " AND ";
93                         
94                 }
95                 globalJoinCondition += " (" + joinOn.getSQL() + ") ";
96                 command.addJoin(", "+t2.getName()+" "+alias);
97             } else {
98                 String JavaDoc join = " JOIN "+t2.getName()+" "+alias+" ON "+joinOn.getSQL();
99                 if(config.random().getBoolean(20)) {
100                     command.addJoin(" LEFT OUTER"+join);
101                 } else {
102                     command.addJoin(" INNER"+join);
103                 }
104             }
105         }
106         command.selectList = Expression.getRandomSelectList(config, command);
107         // TODO group by, having
108
String JavaDoc cond = Expression.getRandomCondition(config, command).getSQL();
109         if(globalJoinCondition.length()>0) {
110             if(cond!=null) {
111                 cond = "(" + globalJoinCondition + " ) AND (" + cond + ")";
112             } else {
113                 cond = globalJoinCondition;
114             }
115         }
116         command.condition = cond;
117         command.order = Expression.getRandomOrder(config, command);
118         return command;
119     }
120     
121     static Command getRandomDelete(TestSynth config, Table table) {
122         Command command = new Command(config, Command.DELETE, table);
123         command.condition = Expression.getRandomCondition(config, command).getSQL();
124         return command;
125     }
126
127     static Command getRandomUpdate(TestSynth config, Table table) {
128         Command command = new Command(config, Command.UPDATE, table);
129         command.prepareUpdate();
130         return command;
131     }
132     
133     static Command getRandomInsert(TestSynth config, Table table) {
134         Command command = new Command(config, Command.INSERT, table);
135         command.prepareInsert();
136         return command;
137     }
138     
139
140     private Command(TestSynth config, int type) {
141         this.config = config;
142         this.type = type;
143     }
144     
145     private Command(TestSynth config, int type, Table table) {
146         this.config = config;
147         this.type = type;
148         this.table = table;
149     }
150     
151     private Command(TestSynth config, int type, Table table, String JavaDoc alias) {
152         this.config = config;
153         this.type = type;
154         this.table = table;
155         this.tables = new HashMap JavaDoc();
156         this.tables.put(alias, table);
157     }
158     
159     private Command(TestSynth config, int type, Index index) {
160         this.config = config;
161         this.type = type;
162         this.index = index;
163     }
164
165     Command(int type, String JavaDoc alias, Table table) {
166         this.type = type;
167         if(alias == null) {
168             alias = table.getName();
169         }
170         addSubqueryTable(alias, table);
171         this.table = table;
172     }
173
174     void addSubqueryTable(String JavaDoc alias, Table t) {
175         tables.put(alias, t);
176     }
177     
178     void removeSubqueryTable(String JavaDoc alias) {
179         tables.remove(alias);
180     }
181     
182     void prepareInsert() {
183         Column[] c;
184         if(config.random().getBoolean(70)) {
185             c = table.getColumns();
186         } else {
187             int len = config.random().getInt(table.getColumnCount()-1)+1;
188             c = columns = table.getRandomColumns(len);
189         }
190         values = new Value[c.length];
191         for(int i=0; i<c.length; i++) {
192             values[i] = c[i].getRandomValue();
193         }
194     }
195     
196     void prepareUpdate() {
197         int len = config.random().getLog(table.getColumnCount()-1)+1;
198         Column[] c = columns = table.getRandomColumns(len);
199         values = new Value[c.length];
200         for(int i=0; i<c.length; i++) {
201             values[i] = c[i].getRandomValue();
202         }
203         condition = Expression.getRandomCondition(config, this).getSQL();
204     }
205     
206     private Result select(DbInterface db) throws SQLException JavaDoc {
207         String JavaDoc sql = "SELECT ";
208         for(int i=0; i<selectList.length; i++) {
209             if(i>0) {
210                 sql += ", ";
211             }
212             sql += selectList[i];
213         }
214         sql += " FROM " + table.getName() + " M";
215         sql += " "+join;
216         if(condition!=null) {
217             sql += " WHERE " + condition;
218         }
219         if(order.trim().length()>0) {
220             sql += " ORDER BY " + order;
221         }
222         return db.select(sql);
223     }
224
225     Result run(DbInterface db) throws Exception JavaDoc {
226         try {
227             switch (type) {
228             case CONNECT:
229                 db.connect();
230                 result = new Result("connect");
231                 break;
232             case RESET:
233                 db.reset();
234                 result = new Result("reset");
235                 break;
236             case DISCONNECT:
237                 db.disconnect();
238                 result = new Result("disconnect");
239                 break;
240             case END:
241                 db.end();
242                 result = new Result("disconnect");
243                 break;
244             case CREATE_TABLE:
245                 db.createTable(table);
246                 result = new Result("createTable");
247                 break;
248             case DROP_TABLE:
249                 db.dropTable(table);
250                 result = new Result("dropTable");
251                 break;
252             case CREATE_INDEX:
253                 db.createIndex(index);
254                 result = new Result("createIndex");
255                 break;
256             case DROP_INDEX:
257                 db.dropIndex(index);
258                 result = new Result("dropIndex");
259                 break;
260             case INSERT:
261                 result = db.insert(table, columns, values);
262                 break;
263             case SELECT:
264                 result = select(db);
265                 break;
266             case DELETE:
267                 result = db.delete(table, condition);
268                 break;
269             case UPDATE:
270                 result = db.update(table, columns, values, condition);
271                 break;
272             case AUTOCOMMIT_ON:
273                 db.setAutoCommit(true);
274                 result = new Result("setAutoCommit true");
275                 break;
276             case AUTOCOMMIT_OFF:
277                 db.setAutoCommit(false);
278                 result = new Result("setAutoCommit false");
279                 break;
280             case COMMIT:
281                 db.commit();
282                 result = new Result("commit");
283                 break;
284             case ROLLBACK:
285                 db.rollback();
286                 result = new Result("rollback");
287                 break;
288             default:
289                 throw new Error JavaDoc("internal");
290             }
291         } catch (SQLException JavaDoc e) {
292             result = new Result("", e);
293         }
294         return result;
295     }
296
297     public String JavaDoc getNextTableAlias() {
298         return "S" + nextAlias++;
299     }
300
301     public String JavaDoc getRandomTableAlias() {
302         if(tables == null) {
303             return null;
304         }
305         Object JavaDoc[] list = tables.keySet().toArray();
306         int i = config.random().getInt(list.length);
307         return (String JavaDoc)list[i];
308     }
309     
310     public Table getTable(String JavaDoc alias) {
311         if(alias == null) {
312             return table;
313         }
314         return (Table)tables.get(alias);
315     }
316
317     public void addJoin(String JavaDoc string) {
318         join += string;
319     }
320
321     static Command getSelectAll(TestSynth config, Table table) {
322         Command command = new Command(config, Command.SELECT, table, "M");
323         command.selectList = new String JavaDoc[]{"*"};
324         command.order = "";
325         return command;
326     }
327
328 }
329
Popular Tags