KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
8
9 class Table {
10     private TestSynth config;
11     private String JavaDoc name;
12     private boolean temporary;
13     private boolean globalTemporary;
14     private Column[] columns;
15     private Column[] primaryKeys;
16     private ArrayList JavaDoc indexes = new ArrayList JavaDoc();
17     
18     Table(TestSynth config) {
19         this.config = config;
20     }
21     
22     public static Table newRandomTable(TestSynth config) {
23         Table table = new Table(config);
24         table.name = "T_" + config.randomIdentifier();
25         
26         // there is a difference between local temp tables for persistent and in-memory mode
27
// table.temporary = config.random().getBoolean(10);
28
// if(table.temporary) {
29
// if(config.getMode() == TestSynth.H2_MEM) {
30
// table.globalTemporary = false;
31
// } else {
32
// table.globalTemporary = config.random().getBoolean(50);
33
// }
34
// }
35

36         int len = config.random().getLog(10) + 1;
37         table.columns = new Column[len];
38         for(int i=0; i<len; i++) {
39             Column col = Column.getRandomColumn(config);
40             table.columns[i] = col;
41         }
42         if(config.random().getBoolean(90)) {
43             int pkLen = config.random().getLog(len);
44             table.primaryKeys = new Column[pkLen];
45             for(int i=0; i<pkLen; i++) {
46                 Column pk = null;
47                 do {
48                     pk = table.columns[config.random().getInt(len)];
49                 } while(pk.isPrimaryKey());
50                 table.primaryKeys[i] = pk;
51                 pk.setPrimaryKey(true);
52                 pk.setNullable(false);
53             }
54         }
55         return table;
56     }
57     
58     public Index newRandomIndex() {
59         String JavaDoc indexName = "I_" + config.randomIdentifier();
60         int len = config.random().getLog(getColumnCount()-1)+1;
61         boolean unique = config.random().getBoolean(50);
62         Column[] cols = getRandomColumns(len);
63         Index index = new Index(this, indexName, cols, unique);
64         return index;
65     }
66     
67     public String JavaDoc getDropSQL() {
68         return "DROP TABLE " + name;
69     }
70     
71     public String JavaDoc getCreateSQL() {
72         String JavaDoc sql = "CREATE ";
73         if(temporary) {
74             if(globalTemporary) {
75                 sql += "GLOBAL ";
76             } else {
77                 sql += "LOCAL ";
78             }
79             sql += "TEMPORARY ";
80         }
81         sql += "TABLE " + name + "(";
82         for(int i=0; i<columns.length; i++) {
83             if(i>0) {
84                 sql += ", ";
85             }
86             Column column = columns[i];
87             sql += column.getCreateSQL();
88             if(primaryKeys!=null && primaryKeys.length==1 && primaryKeys[0]==column) {
89                 sql += " PRIMARY KEY";
90             }
91         }
92         if(primaryKeys != null && primaryKeys.length>1) {
93             sql += ", ";
94             sql += "PRIMARY KEY(";
95             for(int i=0; i<primaryKeys.length; i++) {
96                 if(i>0) {
97                     sql += ", ";
98                 }
99                 Column column = primaryKeys[i];
100                 sql += column.getName();
101             }
102             sql += ")";
103         }
104         sql += ")";
105         return sql;
106     }
107
108     public String JavaDoc getInsertSQL(Column[] c, Value[] v) {
109         String JavaDoc sql = "INSERT INTO " + name;
110         if(c!=null) {
111             sql += "(";
112             for(int i=0; i<c.length; i++) {
113                 if(i>0) {
114                     sql += ", ";
115                 }
116                 sql += c[i].getName();
117             }
118             sql += ")";
119         }
120         sql += " VALUES(";
121         for(int i=0; i<v.length; i++) {
122             if(i>0) {
123                 sql += ", ";
124             }
125             sql += v[i].getSQL();
126         }
127         sql += ")";
128         return sql;
129     }
130     
131     public String JavaDoc getName() {
132         return name;
133     }
134     
135     public Column getRandomConditionColumn() {
136         ArrayList JavaDoc list = new ArrayList JavaDoc();
137         for(int i=0; i<columns.length; i++) {
138             if(Column.isConditionType(config, columns[i].getType())) {
139                 list.add(columns[i]);
140             }
141         }
142         if(list.size() == 0) {
143             return null;
144         }
145         return (Column) list.get(config.random().getInt(list.size()));
146     }
147
148     public Column getRandomColumn() {
149         return columns[config.random().getInt(columns.length)];
150     }
151     
152     public int getColumnCount() {
153         return columns.length;
154     }
155     
156     public Column getRandomColumnOfType(int type) {
157         ArrayList JavaDoc list = new ArrayList JavaDoc();
158         for(int i=0; i<columns.length; i++) {
159             if(columns[i].getType()==type) {
160                 list.add(columns[i]);
161             }
162         }
163         if(list.size() == 0) {
164             return null;
165         }
166         return (Column) list.get(config.random().getInt(list.size()));
167     }
168
169     public Column[] getRandomColumns(int len) {
170         int[] index = new int[columns.length];
171         for(int i=0; i<columns.length; i++) {
172             index[i] = i;
173         }
174         for(int i=0; i<columns.length; i++) {
175             int temp = index[i];
176             int r = index[config.random().getInt(columns.length)];
177             index[i] = index[r];
178             index[r] = temp;
179         }
180         Column[] c = new Column[len];
181         for(int i=0; i<len; i++) {
182             c[i] = columns[index[i]];
183         }
184         return c;
185     }
186
187     public Column[] getColumns() {
188         return columns;
189     }
190     
191     public void addIndex(Index index) {
192         indexes.add(index);
193     }
194
195     public void removeIndex(Index index) {
196         indexes.remove(index);
197     }
198 }
199
Popular Tags