1 22 23 28 29 package org.xquark.mapper.metadata; 30 31 import org.xquark.mapper.dbms.*; 32 import org.xquark.mapper.mapping.MappingInfo; 33 import org.xquark.mapper.mapping.TableMapping; 34 35 38 public class CollectionMappingInfo implements MappingInfo 39 { 40 private static final String RCSRevision = "$Revision: 1.1 $"; 41 private static final String RCSName = "$Name: $"; 42 43 private final static String DATA_INDEX_SUFFIX = "_D"; 44 45 protected String deleteStatement; 46 protected String insertStatement; 47 protected String [] oidIndexCreateStatement; 48 protected String [] oidIndexDropStatement; 49 protected String oidTableCreateStatement; 50 protected String oidTableDropStatement; 51 private String oidDeleteStatement; 52 private String oidInsertStatement; 53 private String selectValuesStatement; 54 private String getRowsClause; 55 private String oidTableName; 56 private TableMapping table; 57 58 private static TableSpec OIDTableSpecs; 59 60 static 61 { 62 OIDTableSpecs = (TableSpec)TableSpecLoader.getInstance().getTableSpec( 63 TableSpec.CAT_USER_TABLE, TableSpec.TYPE_USER_DATA); 64 } 65 66 68 public CollectionMappingInfo(TableMapping table, short cid, short tid, AbstractConnection conn) 69 { 70 this.table = table; 71 initializeCreationStatements(cid, tid, conn); 72 } 73 74 76 public CollectionMappingInfo(TableMapping table) 77 { 78 this.table = table; 79 } 80 81 83 public void initializeStatements(CollectionMetadata collection, short tid, short pid) 84 { 85 initializeStatements( 86 collection.getTableInfo(TableSpec.TYPE_DOCS), 87 collection.getUOIDManager(), 88 collection.getCollectionID(), 89 tid, 90 pid 91 ); 92 } 93 94 public String [] getOIDIndexDropStatements() 95 { 96 return oidIndexDropStatement; 97 } 98 99 public String [] getOIDIndexCreateStatements() 100 { 101 return oidIndexCreateStatement; 102 } 103 public String getOIDTableCreateStatement() 104 { 105 return oidTableCreateStatement; 106 } 107 public String getOIDDeleteStatement() 108 { 109 return oidDeleteStatement; 110 } 111 public String getOIDTableDropStatement() 112 { 113 return oidTableDropStatement; 114 } 115 public String getDocumentRowsStatement() 116 { 117 return getRowsClause; 118 } 119 public String getDeleteStatement() 120 { 121 return deleteStatement; 122 } 123 public String getSelectValuesStatement() 124 { 125 return selectValuesStatement; 126 } 127 128 132 public String getInsertStatement() 133 { 134 return insertStatement; 135 } 136 137 public String getOIDInsertStatement() 138 { 139 return oidInsertStatement; 140 } 141 142 public String getOIDTableName() 143 { 144 return oidTableName; 145 } 146 147 public String getSelectStatement() 148 { 149 return table.getSelectStatement(); 150 } 151 152 public String getUpdateStatement() 153 { 154 return table.getUpdateStatement(); 155 } 156 157 public TableMapping getTableMapping() 158 { 159 return table; 160 } 161 162 167 public boolean isShareable() 168 { 169 return table.isShareable(); 170 } 171 172 178 public boolean isPathFilteringNeeded() 179 { 180 return table.isTableShared() && !table.isShared(); 181 } 182 183 187 189 private void initializeStatements(TableInfo documentTable ,UOIDManager manager, short cid, short tid, short pid) 190 { 191 String uoidColumnName = table.getUOIDColumnName(); 192 String pidColumnName = table.getPathIDColumnName(); 193 String docTableName = documentTable.getName(); 194 String userTableName = table.getTableName(); 196 insertStatement = table.getInsertStatement(); 198 199 StringBuffer sql = new StringBuffer (); 200 if (table.isClustered()) { 202 sql.setLength(0); 204 sql.append("SELECT "); 205 sql.append(table.getNamedColumnList()); 206 sql.append(" FROM "); 207 sql.append(docTableName); 208 sql.append(" d,"); 209 sql.append(userTableName); 210 sql.append(" w WHERE d.NAME=? and w."); 211 sql.append(uoidColumnName); 212 sql.append(">=(d.UDID*"); 213 sql.append(manager.getDocMultiplier()); 214 sql.append(") AND w."); 216 sql.append(uoidColumnName); 217 sql.append("<=(d.UDID*"); 218 sql.append(manager.getDocMultiplier()); 219 sql.append('+'); 220 sql.append(manager.getDocMaxConstant()); 221 sql.append(")"); 222 if (isPathFilteringNeeded()) 223 { 224 sql.append(" AND w."); 225 sql.append(pidColumnName); 226 sql.append("="); 227 sql.append(pid); 228 } 229 sql.append(" ORDER BY w."); 230 sql.append(uoidColumnName); 231 selectValuesStatement = sql.toString(); 232 233 sql.setLength(0); 234 sql.append("DELETE FROM "); 235 sql.append(userTableName); 236 sql.append(" WHERE "); 237 sql.append(uoidColumnName); 238 sql.append(">=? AND "); 239 sql.append(uoidColumnName); 240 sql.append("<=?"); 241 deleteStatement = sql.toString(); 243 getRowsClause = null; 244 245 246 oidTableCreateStatement = null; 247 oidTableDropStatement = null; 248 oidIndexCreateStatement = null; 249 oidIndexDropStatement = null; 250 oidInsertStatement = null; 251 oidDeleteStatement = null; 252 } 253 else 254 { 255 oidTableName = OIDTableSpecs.getTableName(cid, tid); 256 257 sql.setLength(0); 258 sql.append("SELECT v.UOID,v.PATH, "); 259 sql.append(table.getNamedColumnList()); 260 sql.append(" FROM "); 261 sql.append(docTableName); 262 sql.append(" d,"); 263 sql.append(oidTableName); 264 sql.append(" v, "); 265 sql.append(userTableName); 266 sql.append(" w WHERE d.NAME=? and v.UOID>=(d.UDID*"); 267 sql.append(manager.getDocMultiplier()); 268 sql.append(") AND v.UOID<=(d.UDID*"); 270 sql.append(manager.getDocMultiplier()); 271 sql.append('+'); 272 sql.append(manager.getDocMaxConstant()); 273 sql.append(")"); 274 if (isPathFilteringNeeded()) 275 { 276 sql.append(" AND v.PATH="); 277 sql.append(pid); 278 } 279 sql.append(" AND "); 280 sql.append(table.getJoinCondition()); 281 sql.append(" ORDER BY v.UOID"); 282 selectValuesStatement = sql.toString(); 283 284 deleteStatement = null; 285 286 sql.setLength(0); 287 sql.append("FROM "); 288 sql.append(oidTableName); 289 sql.append(" v, "); 290 sql.append(userTableName); 291 sql.append(" w WHERE v.UOID>=? AND v.UOID<=? AND "); 292 sql.append(table.getJoinCondition()); 293 getRowsClause = sql.toString(); 294 295 sql.setLength(0); 296 sql.append("INSERT INTO "); 297 sql.append(oidTableName); 298 sql.append("(UOID,PATH, "); 299 sql.append(table.getJoinColumnList()); 300 sql.append(") VALUES(?,?,"); 301 sql.append(table.getJoinWildcards()); 302 sql.append(")"); 303 oidInsertStatement = sql.toString(); 304 305 sql.setLength(0); 306 sql.append("DELETE FROM "); 307 sql.append(oidTableName); 308 sql.append(" WHERE UOID>=? AND UOID<=?"); 309 oidDeleteStatement = sql.toString(); 310 } 311 } 312 313 315 private void initializeCreationStatements(short cid, short tid, AbstractConnection conn) 316 { 317 String uoidColumnName = table.getUOIDColumnName(); 318 319 StringBuffer sql = new StringBuffer (); 321 322 if (!table.isClustered()) { 324 oidTableName = OIDTableSpecs.getTableName(cid, tid); 325 326 327 ColumnSpec[] columns = OIDTableSpecs.getColumns(); 328 sql.setLength(0); 329 sql.append("CREATE TABLE "); 330 sql.append(oidTableName); 331 sql.append('('); 332 sql.append(columns[0].getCreationString(conn.getTypeMap())); 333 sql.append(','); 334 sql.append(columns[1].getCreationString(conn.getTypeMap())); 335 sql.append(','); 336 sql.append(table.getJoinColumnTypes()); 337 sql.append(')'); 338 oidTableCreateStatement = sql.toString(); 339 340 sql.setLength(0); 341 sql.append("DROP TABLE "); 342 sql.append(oidTableName); 343 oidTableDropStatement = sql.toString(); 344 345 IndexSpec[] indexes = OIDTableSpecs.getIndexes(); 346 oidIndexCreateStatement = new String [indexes.length + 1]; 347 oidIndexDropStatement = new String [indexes.length + 1]; 348 349 int i = 0; 350 for (; i < indexes.length; i++) 351 { 352 oidIndexCreateStatement[i] = indexes[i].generateCreationStatement(cid, tid, conn); 353 oidIndexDropStatement[i] = "DROP INDEX " + indexes[i].getIndexName(cid, tid); 354 } 355 sql.setLength(0); 357 sql.append("CREATE INDEX "); 358 sql.append(oidTableName); 359 sql.append(DATA_INDEX_SUFFIX); 360 sql.append(" ON "); 361 sql.append(oidTableName); 362 sql.append('('); 363 sql.append(table.getJoinColumnList()); 364 sql.append(')'); 365 oidIndexCreateStatement[i] = sql.toString(); 366 367 sql.setLength(0); 368 sql.append("DROP INDEX "); 369 sql.append(oidTableName); 370 sql.append(DATA_INDEX_SUFFIX); 371 oidIndexDropStatement[i] = sql.toString(); 372 } 373 } 374 } 375 | Popular Tags |