1 22 23 28 29 package org.xquark.mapper.dbms; 30 31 import java.sql.SQLException ; 32 33 import org.xquark.mapper.mapping.TableMetaData; 34 import org.xquark.mapper.mapping.TableMetaDataImpl; 35 36 41 public class TableInfo 42 { 43 private static final String RCSRevision = "$Revision: 1.1 $"; 44 private static final String RCSName = "$Name: $"; 45 46 public static short NOT_A_PARAM = 0; 47 public static short PARAM_DATA_SIZE = 1; 48 public static short PARAM_EXTRA_SIZE = 2; 49 public static short PARAM_CID_SIZE = 3; public static short PARAM_DID_SIZE = 4; 51 public static short PARAM_BID_SIZE = 5; 52 public static short PARAM_UDID_SIZE = 6; 53 54 public static int PARAM_NUMBER = 7; 55 56 private TableSpec template = null; 57 private ColumnInfo[] columns; 58 private String [] indexesCreationStatements = null; 59 private String [] indexesDropStatements = null; 60 private String selectAllStatement = null; 61 private String columnList = null; 62 private String logicalSpecification = null; 63 private String insertStatement = null; 64 private String name = null; 65 private String creationStatement = null; 66 private TableMetaData tableMetaData = null; 67 68 75 public TableInfo(TableSpec template, long[] sizeParameters, AbstractConnection dbmsInfo) 76 { 77 78 this.template = template; 79 name = template.getTableName(); 80 initializeIndexes(dbmsInfo); 81 init(template, sizeParameters, dbmsInfo); 82 } 83 84 89 public TableInfo(TableSpec template, short cid, long[] sizeParameters, AbstractConnection dbmsInfo) 90 { 91 92 this.template = template; 93 name = template.getTableName(cid); 94 initializeIndexes(cid, dbmsInfo); 95 init(template, sizeParameters, dbmsInfo); 96 } 97 98 public TableInfo(TableSpec template, short cid, AbstractConnection dbmsInfo) 99 { 100 101 this.template = template; 102 name = template.getTableName(cid); 103 initializeIndexes(cid, dbmsInfo); 104 init(template, new long[PARAM_NUMBER], dbmsInfo); 105 } 106 107 public TableInfo(TableSpec template, short cid, short tid, long[] sizeParameters, AbstractConnection dbmsInfo) 108 { 109 110 this.template = template; 111 name = template.getTableName(cid, tid); 112 initializeIndexes(cid, tid, dbmsInfo); 113 init(template, sizeParameters, dbmsInfo); 114 } 115 116 private void init(TableSpec template, long[] sizeParameters, AbstractConnection dbmsInfo) 117 { 118 119 this.template = template; 120 initializeColumns(sizeParameters, dbmsInfo); 121 generateCreationStatement(dbmsInfo); 122 initStatements(); 123 } 124 125 public String getName() 129 { 130 return name; 131 } 132 133 public String getCreationStatement() 134 { 135 return creationStatement; 136 } 137 138 public String getSelectAllStatement() 139 { 140 return selectAllStatement; 141 } 142 143 public String getColumnList() 144 { 145 return columnList; 146 } 147 148 public String getLogicalSpecification() 149 { 150 return logicalSpecification; 151 } 152 153 public String getInsertStatement() 154 { 155 156 return insertStatement; 157 } 158 159 public byte getType() 160 { 161 return template.getType(); 162 } 163 164 public TableSpec getTemplate() 165 { 166 return template; 167 } 168 169 public boolean isSequence() 170 { 171 return template.isSequence(); 172 } 173 174 public ColumnInfo[] getColumns() 175 { 176 return columns; 177 } 178 179 public ConstraintSpec[] getConstraints() 180 { 181 return template.getConstraints(); 182 } 183 184 public IndexSpec[] getIndexes() 185 { 186 return template.getIndexes(); 187 } 188 189 public String [] getIndexCreationStatements() 190 { 191 return indexesCreationStatements; 192 } 193 194 public String [] getIndexDropStatements() 195 { 196 return indexesDropStatements; 197 } 198 199 public TableMetaData getJDBCMetaData(AbstractConnection conn) 200 throws SQLException 201 { 202 if (tableMetaData == null) tableMetaData = new TableMetaDataImpl(name, conn); 204 return tableMetaData; 205 } 206 207 private void initializeColumns(long[] sizeParameters, AbstractConnection dbmsInfo) 211 { 212 ColumnSpec[] templateColumns = template.getColumns(); 213 columns = new ColumnInfo[templateColumns.length]; 214 215 for (int i = 0; i < templateColumns.length; i++) 216 columns[i] = new ColumnInfo(templateColumns[i], sizeParameters, dbmsInfo); 217 } 218 219 private void initializeIndexes(short cid, short tid, AbstractConnection dbmsInfo) 220 { 221 IndexSpec[] indexes = template.getIndexes(); 222 indexesCreationStatements = new String [indexes.length]; 223 indexesDropStatements = new String [indexes.length]; 224 225 for (int i = 0; i < indexes.length; i++) 226 { 227 indexesCreationStatements[i] = indexes[i].generateCreationStatement(cid, tid, dbmsInfo); 228 indexesDropStatements[i] = indexes[i].generateDropStatement(cid, tid); 229 } 230 } 231 232 private void initializeIndexes(short cid, AbstractConnection dbmsInfo) 233 { 234 IndexSpec[] indexes = template.getIndexes(); 235 indexesCreationStatements = new String [indexes.length]; 236 indexesDropStatements = new String [indexes.length]; 237 238 for (int i = 0; i < indexes.length; i++) 239 { 240 indexesCreationStatements[i] = indexes[i].generateCreationStatement(cid, dbmsInfo); 241 indexesDropStatements[i] = indexes[i].generateDropStatement(cid); 242 } 243 } 244 245 private void initializeIndexes(AbstractConnection dbmsInfo) 246 { 247 IndexSpec[] indexes = template.getIndexes(); 248 indexesCreationStatements = new String [indexes.length]; 249 indexesDropStatements = new String [indexes.length]; 250 251 for (int i = 0; i < indexes.length; i++) 252 { 253 indexesCreationStatements[i] = indexes[i].generateCreationStatement(dbmsInfo); 254 indexesDropStatements[i] = indexes[i].generateDropStatement(); 255 } 256 } 257 258 private void initStatements() 259 { 260 261 StringBuffer sql = new StringBuffer (); 262 for (int i = 0; i < columns.length; i++) 263 { 264 sql.append(columns[i].getName()); 265 sql.append(','); 266 } 267 sql.setLength(sql.length() - 1); 268 columnList = sql.toString(); 269 sql.setLength(0); 270 sql.append("SELECT "); 271 sql.append(columnList); 272 sql.append(" FROM "); 273 sql.append(name); 274 selectAllStatement = sql.toString(); 275 276 277 sql.setLength(0); 278 sql.append("INSERT INTO "); 279 sql.append(name); 280 sql.append(" VALUES("); 281 for (int i = 0; i < columns.length; i++) 282 sql.append("?,"); 283 sql.setCharAt(sql.length() - 1, ')'); 284 insertStatement = sql.toString(); 285 } 286 287 288 private void generateCreationStatement(AbstractConnection dbmsInfo) 289 { 290 StringBuffer statementBuffer = new StringBuffer (); 291 StringBuffer pk = new StringBuffer (); 292 293 for (int i = 0; i < columns.length; i++) 294 { 295 statementBuffer.append(columns[i].getColumnClause()); 296 statementBuffer.append(','); 297 298 if (columns[i].isPrimaryKey()) 299 { 300 pk.append(columns[i].getName()); 301 pk.append(','); 302 } 303 } 304 305 if (pk.length() > 0) 306 { 307 pk.setCharAt(pk.length() - 1, ')'); 308 pk.append(','); 309 310 statementBuffer.append("PRIMARY KEY("); 311 statementBuffer.append(pk.toString()); 312 } 313 314 315 ConstraintSpec[] constraints = template.getConstraints(); 316 for (int i = 0; i < constraints.length; i++) 317 { 318 statementBuffer.append(constraints[i].generateConstraintClause()); 319 statementBuffer.append(','); 320 } 321 322 323 statementBuffer.setLength(statementBuffer.length() - 1); 324 325 logicalSpecification = statementBuffer.toString(); 326 327 328 329 switch (template.getDBMSType()) 330 { 331 case TableSpec.TABLE_TEMPORARY: 332 creationStatement = dbmsInfo.getTemporaryTableCreationStatement(this); 333 break; 334 case TableSpec.TABLE_INDEX_ORGANIZED: 335 creationStatement = dbmsInfo.getIndexOrganizedTableCreationStatement(this); 336 break; 337 default: 338 statementBuffer.setLength(0); 339 statementBuffer.append("CREATE TABLE "); 340 statementBuffer.append(getName()); 341 statementBuffer.append('('); 342 statementBuffer.append(logicalSpecification); 343 statementBuffer.append(')'); 344 creationStatement = statementBuffer.toString(); 345 } 346 } 347 } 348 | Popular Tags |