001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.synth.sql;
007:
008: import java.util.ArrayList;
009:
010: /**
011: * Represents a table.
012: */
013: class Table {
014: private TestSynth config;
015: private String name;
016: private boolean temporary;
017: private boolean globalTemporary;
018: private Column[] columns;
019: private Column[] primaryKeys;
020: private ArrayList indexes = new ArrayList();
021:
022: Table(TestSynth config) {
023: this .config = config;
024: }
025:
026: public static Table newRandomTable(TestSynth config) {
027: Table table = new Table(config);
028: table.name = "T_" + config.randomIdentifier();
029:
030: // there is a difference between local temp tables for persistent and
031: // in-memory mode
032: // table.temporary = config.random().getBoolean(10);
033: // if(table.temporary) {
034: // if(config.getMode() == TestSynth.H2_MEM) {
035: // table.globalTemporary = false;
036: // } else {
037: // table.globalTemporary = config.random().getBoolean(50);
038: // }
039: // }
040:
041: int len = config.random().getLog(10) + 1;
042: table.columns = new Column[len];
043: for (int i = 0; i < len; i++) {
044: Column col = Column.getRandomColumn(config);
045: table.columns[i] = col;
046: }
047: if (config.random().getBoolean(90)) {
048: int pkLen = config.random().getLog(len);
049: table.primaryKeys = new Column[pkLen];
050: for (int i = 0; i < pkLen; i++) {
051: Column pk = null;
052: do {
053: pk = table.columns[config.random().getInt(len)];
054: } while (pk.isPrimaryKey());
055: table.primaryKeys[i] = pk;
056: pk.setPrimaryKey(true);
057: pk.setNullable(false);
058: }
059: }
060: return table;
061: }
062:
063: public Index newRandomIndex() {
064: String indexName = "I_" + config.randomIdentifier();
065: int len = config.random().getLog(getColumnCount() - 1) + 1;
066: boolean unique = config.random().getBoolean(50);
067: Column[] cols = getRandomColumns(len);
068: Index index = new Index(this , indexName, cols, unique);
069: return index;
070: }
071:
072: public String getDropSQL() {
073: return "DROP TABLE " + name;
074: }
075:
076: public String getCreateSQL() {
077: String sql = "CREATE ";
078: if (temporary) {
079: if (globalTemporary) {
080: sql += "GLOBAL ";
081: } else {
082: sql += "LOCAL ";
083: }
084: sql += "TEMPORARY ";
085: }
086: sql += "TABLE " + name + "(";
087: for (int i = 0; i < columns.length; i++) {
088: if (i > 0) {
089: sql += ", ";
090: }
091: Column column = columns[i];
092: sql += column.getCreateSQL();
093: if (primaryKeys != null && primaryKeys.length == 1
094: && primaryKeys[0] == column) {
095: sql += " PRIMARY KEY";
096: }
097: }
098: if (primaryKeys != null && primaryKeys.length > 1) {
099: sql += ", ";
100: sql += "PRIMARY KEY(";
101: for (int i = 0; i < primaryKeys.length; i++) {
102: if (i > 0) {
103: sql += ", ";
104: }
105: Column column = primaryKeys[i];
106: sql += column.getName();
107: }
108: sql += ")";
109: }
110: sql += ")";
111: return sql;
112: }
113:
114: public String getInsertSQL(Column[] c, Value[] v) {
115: String sql = "INSERT INTO " + name;
116: if (c != null) {
117: sql += "(";
118: for (int i = 0; i < c.length; i++) {
119: if (i > 0) {
120: sql += ", ";
121: }
122: sql += c[i].getName();
123: }
124: sql += ")";
125: }
126: sql += " VALUES(";
127: for (int i = 0; i < v.length; i++) {
128: if (i > 0) {
129: sql += ", ";
130: }
131: sql += v[i].getSQL();
132: }
133: sql += ")";
134: return sql;
135: }
136:
137: public String getName() {
138: return name;
139: }
140:
141: public Column getRandomConditionColumn() {
142: ArrayList list = new ArrayList();
143: for (int i = 0; i < columns.length; i++) {
144: if (Column.isConditionType(config, columns[i].getType())) {
145: list.add(columns[i]);
146: }
147: }
148: if (list.size() == 0) {
149: return null;
150: }
151: return (Column) list.get(config.random().getInt(list.size()));
152: }
153:
154: public Column getRandomColumn() {
155: return columns[config.random().getInt(columns.length)];
156: }
157:
158: public int getColumnCount() {
159: return columns.length;
160: }
161:
162: public Column getRandomColumnOfType(int type) {
163: ArrayList list = new ArrayList();
164: for (int i = 0; i < columns.length; i++) {
165: if (columns[i].getType() == type) {
166: list.add(columns[i]);
167: }
168: }
169: if (list.size() == 0) {
170: return null;
171: }
172: return (Column) list.get(config.random().getInt(list.size()));
173: }
174:
175: public Column[] getRandomColumns(int len) {
176: int[] index = new int[columns.length];
177: for (int i = 0; i < columns.length; i++) {
178: index[i] = i;
179: }
180: for (int i = 0; i < columns.length; i++) {
181: int temp = index[i];
182: int r = index[config.random().getInt(columns.length)];
183: index[i] = index[r];
184: index[r] = temp;
185: }
186: Column[] c = new Column[len];
187: for (int i = 0; i < len; i++) {
188: c[i] = columns[index[i]];
189: }
190: return c;
191: }
192:
193: public Column[] getColumns() {
194: return columns;
195: }
196:
197: public void addIndex(Index index) {
198: indexes.add(index);
199: }
200:
201: public void removeIndex(Index index) {
202: indexes.remove(index);
203: }
204: }
|