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.sql.ResultSetMetaData;
009: import java.sql.SQLException;
010: import java.sql.Types;
011:
012: import org.h2.value.DataType;
013:
014: /**
015: * A column of a table.
016: */
017: class Column {
018: private TestSynth config;
019: private String name;
020: private int type;
021: private int precision;
022: private int scale;
023: private boolean isNullable;
024: private boolean isPrimaryKey;
025: // TODO test isAutoincrement;
026:
027: private static final int[] TYPES = { Types.INTEGER, Types.VARCHAR,
028: Types.DECIMAL, Types.DATE, Types.TIME, Types.TIMESTAMP,
029: DataType.TYPE_BOOLEAN, Types.BINARY, Types.VARBINARY,
030: Types.CLOB, Types.BLOB, Types.DOUBLE, Types.BIGINT,
031: Types.TIMESTAMP, Types.BIT, };
032:
033: Column(TestSynth config) {
034: this .config = config;
035: }
036:
037: Column(ResultSetMetaData meta, int index) throws SQLException {
038: name = meta.getColumnLabel(index);
039: type = meta.getColumnType(index);
040: switch (type) {
041: case Types.DECIMAL:
042: precision = meta.getPrecision(index);
043: scale = meta.getScale(index);
044: break;
045: case Types.BLOB:
046: case Types.BINARY:
047: case Types.VARBINARY:
048: case Types.CLOB:
049: case Types.LONGVARCHAR:
050: case Types.DATE:
051: case Types.TIME:
052: case Types.INTEGER:
053: case Types.VARCHAR:
054: case Types.CHAR:
055: case Types.BIGINT:
056: case Types.NUMERIC:
057: case Types.TIMESTAMP:
058: case Types.NULL:
059: case Types.LONGVARBINARY:
060: case Types.DOUBLE:
061: case Types.REAL:
062: case Types.OTHER:
063: case Types.BIT:
064: case DataType.TYPE_BOOLEAN:
065: break;
066: default:
067: throw new Error("type=" + type);
068: }
069: }
070:
071: public static boolean isConditionType(TestSynth config, int type) {
072: switch (config.getMode()) {
073: case TestSynth.H2:
074: case TestSynth.H2_MEM:
075: return true;
076: case TestSynth.MYSQL:
077: case TestSynth.HSQLDB:
078: case TestSynth.POSTGRESQL:
079: switch (type) {
080: case Types.INTEGER:
081: case Types.VARCHAR:
082: case Types.DECIMAL:
083: case Types.DATE:
084: case Types.TIME:
085: case Types.TIMESTAMP:
086: case Types.DOUBLE:
087: case Types.BIGINT:
088: case DataType.TYPE_BOOLEAN:
089: case Types.BIT:
090: return true;
091: case Types.BINARY:
092: case Types.VARBINARY:
093: case Types.BLOB:
094: case Types.CLOB:
095: case Types.LONGVARCHAR:
096: case Types.LONGVARBINARY:
097: return false;
098: default:
099: throw new Error("type=" + type);
100: }
101: default:
102: throw new Error("type=" + type);
103: }
104: }
105:
106: String getTypeName() {
107: switch (type) {
108: case Types.INTEGER:
109: return "INT";
110: case Types.VARCHAR:
111: return "VARCHAR(" + precision + ")";
112: case Types.DECIMAL:
113: return "NUMERIC(" + precision + ", " + scale + ")";
114: case Types.DATE:
115: return "DATE";
116: case Types.TIME:
117: return "TIME";
118: case Types.TIMESTAMP:
119: return "TIMESTAMP";
120: case Types.BINARY:
121: case Types.VARBINARY:
122: if (config.is(TestSynth.POSTGRESQL)) {
123: return "BYTEA";
124: }
125: return "BINARY(" + precision + ")";
126: case Types.CLOB: {
127: if (config.is(TestSynth.HSQLDB)) {
128: return "LONGVARCHAR";
129: } else if (config.is(TestSynth.POSTGRESQL)) {
130: return "TEXT";
131: }
132: return "CLOB";
133: }
134: case Types.BLOB: {
135: if (config.is(TestSynth.HSQLDB)) {
136: return "LONGVARBINARY";
137: }
138: return "BLOB";
139: }
140: case Types.DOUBLE:
141: if (config.is(TestSynth.POSTGRESQL)) {
142: return "DOUBLE PRECISION";
143: }
144: return "DOUBLE";
145: case Types.BIGINT:
146: return "BIGINT";
147: case DataType.TYPE_BOOLEAN:
148: case Types.BIT:
149: return "BOOLEAN";
150: default:
151: throw new Error("type=" + type);
152: }
153: }
154:
155: public String getCreateSQL() {
156: String sql = name + " " + getTypeName();
157: if (!isNullable) {
158: sql += " NOT NULL";
159: }
160: return sql;
161: }
162:
163: public String getName() {
164: return name;
165: }
166:
167: public Value getRandomValue() {
168: return Value.getRandom(config, type, precision, scale,
169: isNullable);
170: }
171:
172: public Value getRandomValueNotNull() {
173: return Value.getRandom(config, type, precision, scale, false);
174: }
175:
176: public static Column getRandomColumn(TestSynth config) {
177: Column column = new Column(config);
178: column.name = "C_" + config.randomIdentifier();
179: int randomType;
180: while (true) {
181: randomType = TYPES[config.random().getLog(TYPES.length)];
182: if (config.is(TestSynth.POSTGRESQL)
183: && (randomType == Types.BINARY
184: || randomType == Types.VARBINARY || randomType == Types.BLOB)) {
185: continue;
186: }
187: break;
188: }
189: column.type = randomType;
190: column.precision = config.random().getInt(20) + 2;
191: column.scale = config.random().getInt(column.precision);
192: column.isNullable = config.random().getBoolean(50);
193: return column;
194: }
195:
196: public boolean isPrimaryKey() {
197: return isPrimaryKey;
198: }
199:
200: public void setPrimaryKey(boolean b) {
201: isPrimaryKey = b;
202: }
203:
204: public void setNullable(boolean b) {
205: isNullable = b;
206: }
207:
208: public int getType() {
209: return type;
210: }
211:
212: }
|