001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: AbstractTable.java,v 1.3 2003/02/25 06:55:15 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.sql.Connection;
014: import java.sql.SQLException;
015: import java.sql.Statement;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.List;
020: import org.apache.log4j.Category;
021:
022: abstract class AbstractTable implements Table {
023: private static final Category LOG = Category
024: .getInstance(AbstractTable.class);
025:
026: protected static final int TABLE_STATE_NEW = 0;
027: protected static final int TABLE_STATE_INITIALIZED = 1;
028: protected static final int TABLE_STATE_VALIDATED = 2;
029:
030: protected final StoreManager storeMgr;
031: protected final DatabaseAdapter dba;
032: protected SQLIdentifier name;
033: protected int state = TABLE_STATE_NEW;
034:
035: protected ArrayList columns = new ArrayList();
036: protected HashMap columnsByName = new HashMap();
037:
038: public AbstractTable(StoreManager storeMgr) {
039: this (null, storeMgr);
040: }
041:
042: public AbstractTable(SQLIdentifier name, StoreManager storeMgr) {
043: this .storeMgr = storeMgr;
044: this .dba = storeMgr.getDatabaseAdapter();
045: this .name = name;
046: }
047:
048: public boolean isInitialized() {
049: return state >= TABLE_STATE_INITIALIZED;
050: }
051:
052: public boolean isValidated() {
053: return state >= TABLE_STATE_VALIDATED;
054: }
055:
056: protected void assertIsUninitialized() {
057: if (isInitialized())
058: throw new IllegalStateException(
059: "Table object has already been initialized: "
060: + this );
061: }
062:
063: protected void assertIsInitialized() {
064: if (!isInitialized())
065: throw new IllegalStateException(
066: "Table object has not been initialized: " + this );
067: }
068:
069: protected void assertIsValidated() {
070: if (!isValidated())
071: throw new IllegalStateException(
072: "Table has not been validated: " + this );
073: }
074:
075: public SQLIdentifier getName() {
076: return name;
077: }
078:
079: public StoreManager getStoreManager() {
080: return storeMgr;
081: }
082:
083: public String getSchemaName() {
084: return storeMgr.getSchemaName();
085: }
086:
087: public synchronized void addColumn(Column col) {
088: assertIsUninitialized();
089:
090: SQLIdentifier colName = col.getName();
091:
092: if (hasColumnName(colName))
093: throw new DuplicateColumnNameException(this , col);
094:
095: columns.add(col);
096: columnsByName.put(col.getName(), col);
097: }
098:
099: protected boolean hasColumnName(SQLIdentifier colName) {
100: return columnsByName.get(colName) != null;
101: }
102:
103: public synchronized Column newColumn(Class type, String javaName) {
104: return newColumn(type, new ColumnIdentifier(dba, javaName),
105: Role.NONE);
106: }
107:
108: public synchronized Column newColumn(Class type,
109: SQLIdentifier name, Role role) {
110: assertIsUninitialized();
111:
112: ColumnIdentifier columnName = new ColumnIdentifier(dba, name,
113: type, Role.NONE);
114:
115: if (hasColumnName(columnName))
116: columnName = new ColumnIdentifier(dba, name, type, role);
117:
118: Column col = new Column(this , type, columnName);
119:
120: addColumn(col);
121:
122: return col;
123: }
124:
125: protected void executeStatementList(List stmts, Connection conn)
126: throws SQLException {
127: Statement stmt = conn.createStatement();
128:
129: try {
130: Iterator i = stmts.iterator();
131:
132: while (i.hasNext()) {
133: String stmtText = (String) i.next();
134: long startTime = System.currentTimeMillis();
135:
136: stmt.execute(stmtText);
137:
138: if (LOG.isDebugEnabled())
139: LOG.debug("Time = "
140: + (System.currentTimeMillis() - startTime)
141: + " ms: " + stmtText);
142:
143: storeMgr.logSQLWarnings(stmt);
144: }
145:
146: } finally {
147: stmt.close();
148: }
149: }
150:
151: protected abstract List getSQLCreateStatements();
152:
153: protected abstract List getSQLDropStatements();
154:
155: public void create(Connection conn) throws SQLException {
156: assertIsInitialized();
157:
158: executeStatementList(getSQLCreateStatements(), conn);
159: }
160:
161: public void drop(Connection conn) throws SQLException {
162: assertIsInitialized();
163:
164: executeStatementList(getSQLDropStatements(), conn);
165: }
166:
167: /**
168: * Tests if the database table exists.
169: *
170: * @param conn a JDBC connection to the database.
171: *
172: * @return <tt>true</tt> if the table exists in the database,
173: * <tt>false</tt> otherwise.
174: */
175:
176: public boolean exists(Connection conn) throws SQLException {
177: return storeMgr.tableExists(name, conn);
178: }
179:
180: public final boolean equals(Object obj) {
181: if (obj == this )
182: return true;
183:
184: if (!(obj instanceof AbstractTable))
185: return false;
186:
187: AbstractTable t = (AbstractTable) obj;
188:
189: return getClass().equals(t.getClass()) && name.equals(t.name)
190: && storeMgr.equals(t.storeMgr);
191: }
192:
193: public final int hashCode() {
194: return name.hashCode() ^ storeMgr.hashCode();
195: }
196:
197: public final String toString() {
198: return getSchemaName() + '.' + name;
199: }
200: }
|