1 22 23 package org.xquark.mapper.dbms; 24 25 import java.util.Arrays ; 26 27 31 public class IndexSpec extends ConstraintSpec 32 { 33 private static final String RCSRevision = "$Revision: 1.1 $"; 34 private static final String RCSName = "$Name: $"; 35 36 37 public static final byte INDEX_DEFAULT = 0; 38 public static final byte INDEX_BITMAP = 1; 39 public static final byte INDEX_HASH = 2; 40 public static final byte INDEX_BTREE = 3; 41 42 private static final byte[] indexTypes = 43 {INDEX_BITMAP, INDEX_BTREE, INDEX_DEFAULT, INDEX_HASH}; 44 45 private static final String [] indexTypeStrings = 46 {"bitmap", "btree", "default", "hash"}; 47 48 49 private TableSpec table = null; 50 private String suffix = null; 51 52 public IndexSpec(TableSpec table, String type, String suffix) 56 { 57 this.type = parseType(type); 58 this.table = table; 59 this.suffix = suffix; 60 } 61 62 public String getIndexName() 63 { 64 return table.getTableName() + '_' + suffix; 65 } 66 67 public String getIndexName(short collectionID) 68 { 69 return table.getTableName(collectionID) + '_' + suffix; 70 } 71 72 public String getIndexName(short collectionID, short tableID) 73 { 74 return table.getTableName(collectionID, tableID) + '_' + suffix; 75 } 76 77 public String generateCreationStatement(AbstractConnection dbmsInfo) 78 { 79 return generateCreationStatement(getIndexName(), 80 table.getTableName(), dbmsInfo); 81 } 82 83 public String generateCreationStatement(short collectionID, AbstractConnection dbmsInfo) 84 { 85 return generateCreationStatement(getIndexName(collectionID), 86 table.getTableName(collectionID), dbmsInfo); 87 } 88 89 public String generateCreationStatement(short collectionID, short tableID, AbstractConnection dbmsInfo) 90 { 91 return generateCreationStatement(getIndexName(collectionID, tableID), 92 table.getTableName(collectionID, tableID), dbmsInfo); 93 } 94 95 public String generateDropStatement() 96 { 97 return generateDropStatement(getIndexName()); 98 } 99 100 public String generateDropStatement(short collectionID) 101 { 102 return generateDropStatement(getIndexName(collectionID)); 103 } 104 105 public String generateDropStatement(short collectionID, short tableID) 106 { 107 return generateDropStatement(getIndexName(collectionID, tableID)); 108 } 109 110 public byte getType() 114 { 115 return type; 116 } 117 118 private byte parseType(String type) 119 { 120 int index = Arrays.binarySearch(indexTypeStrings, type); 121 if (index < 0) 122 throw new RuntimeException ("The DBMS index type " + type + " used in tableSpec.xml is unknown."); 123 return indexTypes[index]; 124 } 125 126 private String generateCreationStatement(String indexName, String tableName, 130 AbstractConnection dbmsInfo) 131 { 132 StringBuffer stmt = new StringBuffer (); 133 stmt.append("CREATE "); 134 String typeClause = null; 135 switch (type) 136 { 137 case INDEX_BITMAP: 138 typeClause = dbmsInfo.getBitmapIndexClause(); 139 break; 140 case INDEX_BTREE: 141 typeClause = dbmsInfo.getBTreeIndexClause(); 142 break; 143 case INDEX_HASH: 144 typeClause = dbmsInfo.getBitmapIndexClause(); 145 break; 146 default: 147 } 148 if (typeClause != null) 149 { 150 stmt.append(typeClause); 151 stmt.append(' '); 152 } 153 stmt.append("INDEX "); 154 stmt.append(indexName); 155 stmt.append(" ON "); 156 stmt.append(tableName); 157 stmt.append(generateColumnList()); 158 return stmt.toString(); 159 } 160 161 private String generateDropStatement(String indexName) 162 { 163 StringBuffer stmt = new StringBuffer (); 164 stmt.append("DROP INDEX "); 165 stmt.append(indexName); 166 return stmt.toString(); 167 } 168 } 169 | Popular Tags |