KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > AbstractTable


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: AbstractTable.java,v 1.3 2003/02/25 06:55:15 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.sql.Connection JavaDoc;
14 import java.sql.SQLException JavaDoc;
15 import java.sql.Statement JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
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 JavaDoc columns = new ArrayList JavaDoc();
37     protected HashMap JavaDoc columnsByName = new HashMap JavaDoc();
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 JavaDoc("Table object has already been initialized: " + this);
70     }
71
72
73     protected void assertIsInitialized()
74     {
75         if (!isInitialized())
76             throw new IllegalStateException JavaDoc("Table object has not been initialized: " + this);
77     }
78
79
80     protected void assertIsValidated()
81     {
82         if (!isValidated())
83             throw new IllegalStateException JavaDoc("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 JavaDoc 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 JavaDoc type, String JavaDoc javaName)
126     {
127         return newColumn(type, new ColumnIdentifier(dba, javaName), Role.NONE);
128     }
129
130
131     public synchronized Column newColumn(Class JavaDoc 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 JavaDoc stmts, Connection JavaDoc conn) throws SQLException JavaDoc
149     {
150         Statement JavaDoc stmt = conn.createStatement();
151
152         try
153         {
154             Iterator JavaDoc i = stmts.iterator();
155
156             while (i.hasNext())
157             {
158                 String JavaDoc stmtText = (String JavaDoc)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 JavaDoc getSQLCreateStatements();
178
179
180     protected abstract List JavaDoc getSQLDropStatements();
181
182
183     public void create(Connection JavaDoc conn) throws SQLException JavaDoc
184     {
185         assertIsInitialized();
186
187         executeStatementList(getSQLCreateStatements(), conn);
188     }
189
190
191     public void drop(Connection JavaDoc conn) throws SQLException JavaDoc
192     {
193         assertIsInitialized();
194
195         executeStatementList(getSQLDropStatements(), conn);
196     }
197
198
199     /**
200      * Tests if the database table exists.
201      *
202      * @param conn a JDBC connection to the database.
203      *
204      * @return <tt>true</tt> if the table exists in the database,
205      * <tt>false</tt> otherwise.
206      */

207
208     public boolean exists(Connection JavaDoc conn) throws SQLException JavaDoc
209     {
210         return storeMgr.tableExists(name, conn);
211     }
212
213
214     public final boolean equals(Object JavaDoc 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 JavaDoc toString()
235     {
236         return getSchemaName() + '.' + name;
237     }
238 }
239
Popular Tags