1 10 11 package com.triactive.jdo.store; 12 13 import java.sql.Connection ; 14 import java.sql.SQLException ; 15 import java.sql.Statement ; 16 import java.util.ArrayList ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import org.apache.log4j.Category; 21 22 23 abstract class AbstractTable implements Table 24 { 25 private static final Category LOG = Category.getInstance(AbstractTable.class); 26 27 protected static final int TABLE_STATE_NEW = 0; 28 protected static final int TABLE_STATE_INITIALIZED = 1; 29 protected static final int TABLE_STATE_VALIDATED = 2; 30 31 protected final StoreManager storeMgr; 32 protected final DatabaseAdapter dba; 33 protected SQLIdentifier name; 34 protected int state = TABLE_STATE_NEW; 35 36 protected ArrayList columns = new ArrayList (); 37 protected HashMap columnsByName = new HashMap (); 38 39 40 public AbstractTable(StoreManager storeMgr) 41 { 42 this(null, storeMgr); 43 } 44 45 46 public AbstractTable(SQLIdentifier name, StoreManager storeMgr) 47 { 48 this.storeMgr = storeMgr; 49 this.dba = storeMgr.getDatabaseAdapter(); 50 this.name = name; 51 } 52 53 54 public boolean isInitialized() 55 { 56 return state >= TABLE_STATE_INITIALIZED; 57 } 58 59 60 public boolean isValidated() 61 { 62 return state >= TABLE_STATE_VALIDATED; 63 } 64 65 66 protected void assertIsUninitialized() 67 { 68 if (isInitialized()) 69 throw new IllegalStateException ("Table object has already been initialized: " + this); 70 } 71 72 73 protected void assertIsInitialized() 74 { 75 if (!isInitialized()) 76 throw new IllegalStateException ("Table object has not been initialized: " + this); 77 } 78 79 80 protected void assertIsValidated() 81 { 82 if (!isValidated()) 83 throw new IllegalStateException ("Table has not been validated: " + this); 84 } 85 86 87 public SQLIdentifier getName() 88 { 89 return name; 90 } 91 92 93 public StoreManager getStoreManager() 94 { 95 return storeMgr; 96 } 97 98 99 public String getSchemaName() 100 { 101 return storeMgr.getSchemaName(); 102 } 103 104 105 public synchronized void addColumn(Column col) 106 { 107 assertIsUninitialized(); 108 109 SQLIdentifier colName = col.getName(); 110 111 if (hasColumnName(colName)) 112 throw new DuplicateColumnNameException(this, col); 113 114 columns.add(col); 115 columnsByName.put(col.getName(), col); 116 } 117 118 119 protected boolean hasColumnName(SQLIdentifier colName) 120 { 121 return columnsByName.get(colName) != null; 122 } 123 124 125 public synchronized Column newColumn(Class type, String javaName) 126 { 127 return newColumn(type, new ColumnIdentifier(dba, javaName), Role.NONE); 128 } 129 130 131 public synchronized Column newColumn(Class type, SQLIdentifier name, Role role) 132 { 133 assertIsUninitialized(); 134 135 ColumnIdentifier columnName = new ColumnIdentifier(dba, name, type, Role.NONE); 136 137 if (hasColumnName(columnName)) 138 columnName = new ColumnIdentifier(dba, name, type, role); 139 140 Column col = new Column(this, type, columnName); 141 142 addColumn(col); 143 144 return col; 145 } 146 147 148 protected void executeStatementList(List stmts, Connection conn) throws SQLException 149 { 150 Statement stmt = conn.createStatement(); 151 152 try 153 { 154 Iterator i = stmts.iterator(); 155 156 while (i.hasNext()) 157 { 158 String stmtText = (String )i.next(); 159 long startTime = System.currentTimeMillis(); 160 161 stmt.execute(stmtText); 162 163 if (LOG.isDebugEnabled()) 164 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + stmtText); 165 166 storeMgr.logSQLWarnings(stmt); 167 } 168 169 } 170 finally 171 { 172 stmt.close(); 173 } 174 } 175 176 177 protected abstract List getSQLCreateStatements(); 178 179 180 protected abstract List getSQLDropStatements(); 181 182 183 public void create(Connection conn) throws SQLException 184 { 185 assertIsInitialized(); 186 187 executeStatementList(getSQLCreateStatements(), conn); 188 } 189 190 191 public void drop(Connection conn) throws SQLException 192 { 193 assertIsInitialized(); 194 195 executeStatementList(getSQLDropStatements(), conn); 196 } 197 198 199 207 208 public boolean exists(Connection conn) throws SQLException 209 { 210 return storeMgr.tableExists(name, conn); 211 } 212 213 214 public final boolean equals(Object obj) 215 { 216 if (obj == this) 217 return true; 218 219 if (!(obj instanceof AbstractTable)) 220 return false; 221 222 AbstractTable t = (AbstractTable)obj; 223 224 return getClass().equals(t.getClass()) && name.equals(t.name) && storeMgr.equals(t.storeMgr); 225 } 226 227 228 public final int hashCode() 229 { 230 return name.hashCode() ^ storeMgr.hashCode(); 231 } 232 233 234 public final String toString() 235 { 236 return getSchemaName() + '.' + name; 237 } 238 } 239 | Popular Tags |