1 5 package org.h2.test.synth; 6 7 import java.sql.*; 8 9 import org.h2.value.DataType; 10 11 class Column { 12 private TestSynth config; 13 private String name; 14 private int type; 15 private int precision; 16 private int scale; 17 private boolean isNullable; 18 private boolean isPrimaryKey; 19 21 private static int[] TYPES = { 22 Types.INTEGER, 23 Types.VARCHAR, 24 Types.DECIMAL, 25 Types.DATE, 26 Types.TIME, 27 Types.TIMESTAMP, 28 DataType.TYPE_BOOLEAN, 29 Types.BINARY, 30 Types.VARBINARY, 31 Types.CLOB, 32 Types.BLOB, 33 Types.DOUBLE, 34 Types.BIGINT, 35 Types.TIMESTAMP, 36 Types.BIT, 37 }; 38 39 Column(TestSynth config) { 40 this.config = config; 41 } 42 43 Column(ResultSetMetaData meta, int index) throws SQLException { 44 name = meta.getColumnLabel(index); 45 type = meta.getColumnType(index); 46 switch(type) { 47 case Types.DECIMAL: 48 precision = meta.getPrecision(index); 49 scale = meta.getScale(index); 50 break; 51 case Types.BLOB: 52 case Types.BINARY: 53 case Types.VARBINARY: 54 case Types.CLOB: 55 case Types.LONGVARCHAR: 56 case Types.DATE: 57 case Types.TIME: 58 case Types.INTEGER: 59 case Types.VARCHAR: 60 case Types.CHAR: 61 case Types.BIGINT: 62 case Types.NUMERIC: 63 case Types.TIMESTAMP: 64 case Types.NULL: 65 case Types.LONGVARBINARY: 66 case Types.DOUBLE: 67 case Types.REAL: 68 case Types.OTHER: 69 case Types.BIT: 70 case DataType.TYPE_BOOLEAN: 71 break; 72 default: 73 throw new Error ("type="+type); 74 } 75 } 76 77 public static boolean isConditionType(TestSynth config, int type) { 78 switch(config.getMode()) { 79 case TestSynth.H2: 80 case TestSynth.H2_MEM: 81 return true; 82 case TestSynth.MYSQL: 83 case TestSynth.HSQLDB: 84 case TestSynth.POSTGRESQL: 85 switch(type) { 86 case Types.INTEGER: 87 case Types.VARCHAR: 88 case Types.DECIMAL: 89 case Types.DATE: 90 case Types.TIME: 91 case Types.TIMESTAMP: 92 case Types.DOUBLE: 93 case Types.BIGINT: 94 case DataType.TYPE_BOOLEAN: 95 case Types.BIT: 96 return true; 97 case Types.BINARY: 98 case Types.VARBINARY: 99 case Types.BLOB: 100 case Types.CLOB: 101 case Types.LONGVARCHAR: 102 case Types.LONGVARBINARY: 103 return false; 104 default: 105 throw new Error ("type="+type); 106 } 107 default: 108 throw new Error ("type="+type); 109 } 110 } 111 112 String getTypeName() { 113 switch(type) { 114 case Types.INTEGER: 115 return "INT"; 116 case Types.VARCHAR: 117 return "VARCHAR("+precision+")"; 118 case Types.DECIMAL: 119 return "NUMERIC("+precision+", "+scale+")"; 120 case Types.DATE: 121 return "DATE"; 122 case Types.TIME: 123 return "TIME"; 124 case Types.TIMESTAMP: 125 return "TIMESTAMP"; 126 case Types.BINARY: 127 case Types.VARBINARY: 128 if(config.is(TestSynth.POSTGRESQL)) { 129 return "BYTEA"; 130 } 131 return "BINARY("+precision+")"; 132 case Types.CLOB: { 133 if(config.is(TestSynth.HSQLDB)) { 134 return "LONGVARCHAR"; 135 } else if(config.is(TestSynth.POSTGRESQL)) { 136 return "TEXT"; 137 } 138 return "CLOB"; 139 } 140 case Types.BLOB: { 141 if(config.is(TestSynth.HSQLDB)) { 142 return "LONGVARBINARY"; 143 } 144 return "BLOB"; 145 } 146 case Types.DOUBLE: 147 if(config.is(TestSynth.POSTGRESQL)) { 148 return "DOUBLE PRECISION"; 149 } 150 return "DOUBLE"; 151 case Types.BIGINT: 152 return "BIGINT"; 153 case DataType.TYPE_BOOLEAN: 154 case Types.BIT: 155 return "BOOLEAN"; 156 default: 157 throw new Error ("type="+type); 158 } 159 } 160 161 public String getCreateSQL() { 162 String sql = name + " " + getTypeName(); 163 if(!isNullable) { 164 sql += " NOT NULL"; 165 } 166 return sql; 167 } 168 169 public String getName() { 170 return name; 171 } 172 173 public Value getRandomValue() { 174 return Value.getRandom(config, type, precision, scale, isNullable); 175 } 176 177 public Value getRandomValueNotNull() { 178 return Value.getRandom(config, type, precision, scale, false); 179 } 180 181 public static Column getRandomColumn(TestSynth config) { 182 Column column = new Column(config); 183 column.name = "C_" + config.randomIdentifier(); 184 int randomType; 185 while(true) { 186 randomType = TYPES[config.random().getLog(TYPES.length)]; 187 if(config.is(TestSynth.POSTGRESQL) && (randomType == Types.BINARY || randomType == Types.VARBINARY || randomType == Types.BLOB)) { 188 continue; 189 } 190 break; 191 } 192 column.type = randomType; 193 column.precision = config.random().getInt(20)+2; 194 column.scale = config.random().getInt(column.precision); 195 column.isNullable = config.random().getBoolean(50); 196 return column; 197 } 198 199 200 public boolean isPrimaryKey() { 201 return isPrimaryKey; 202 } 203 204 205 public void setPrimaryKey(boolean b) { 206 isPrimaryKey = b; 207 } 208 209 210 public void setNullable(boolean b) { 211 isNullable = b; 212 } 213 214 215 public int getType() { 216 return type; 217 } 218 219 } 220 | Popular Tags |