001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.cache;
022:
023: import java.sql.SQLException;
024: import java.util.Collection;
025: import java.util.Map;
026:
027: import net.sf.hajdbc.ColumnProperties;
028: import net.sf.hajdbc.ForeignKeyConstraint;
029: import net.sf.hajdbc.QualifiedName;
030: import net.sf.hajdbc.UniqueConstraint;
031:
032: /**
033: * @author Paul Ferraro
034: *
035: */
036: public class LazyTableProperties extends AbstractTableProperties {
037: private QualifiedName table;
038: private DatabaseMetaDataSupport support;
039: private Map<String, ColumnProperties> columnMap;
040: private UniqueConstraint primaryKey;
041: private Collection<UniqueConstraint> uniqueConstraints;
042: private Collection<ForeignKeyConstraint> foreignKeyConstraints;
043: private Collection<String> identityColumns;
044: private String name;
045:
046: public LazyTableProperties(DatabaseMetaDataSupport support,
047: QualifiedName table) {
048: this .table = table;
049: this .support = support;
050: }
051:
052: /**
053: * @see net.sf.hajdbc.TableProperties#getColumns()
054: */
055: @Override
056: public synchronized Collection<String> getColumns()
057: throws SQLException {
058: return this .getColumnMap().keySet();
059: }
060:
061: /**
062: * @see net.sf.hajdbc.TableProperties#getColumnProperties(java.lang.String)
063: */
064: @Override
065: public synchronized ColumnProperties getColumnProperties(
066: String column) throws SQLException {
067: return this .getColumnMap().get(column);
068: }
069:
070: private synchronized Map<String, ColumnProperties> getColumnMap()
071: throws SQLException {
072: if (this .columnMap == null) {
073: this .columnMap = this .support.getColumns(
074: LazyDatabaseProperties.getDatabaseMetaData(),
075: this .table);
076: }
077:
078: return this .columnMap;
079: }
080:
081: /**
082: * @see net.sf.hajdbc.TableProperties#getPrimaryKey()
083: */
084: @Override
085: public synchronized UniqueConstraint getPrimaryKey()
086: throws SQLException {
087: if (this .primaryKey == null) {
088: this .primaryKey = this .support.getPrimaryKey(
089: LazyDatabaseProperties.getDatabaseMetaData(),
090: this .table);
091: }
092:
093: return this .primaryKey;
094: }
095:
096: /**
097: * @see net.sf.hajdbc.TableProperties#getForeignKeyConstraints()
098: */
099: @Override
100: public synchronized Collection<ForeignKeyConstraint> getForeignKeyConstraints()
101: throws SQLException {
102: if (this .foreignKeyConstraints == null) {
103: this .foreignKeyConstraints = this .support
104: .getForeignKeyConstraints(LazyDatabaseProperties
105: .getDatabaseMetaData(), this .table);
106: }
107:
108: return this .foreignKeyConstraints;
109: }
110:
111: /**
112: * @see net.sf.hajdbc.TableProperties#getUniqueConstraints()
113: */
114: @Override
115: public synchronized Collection<UniqueConstraint> getUniqueConstraints()
116: throws SQLException {
117: if (this .uniqueConstraints == null) {
118: this .uniqueConstraints = this .support.getUniqueConstraints(
119: LazyDatabaseProperties.getDatabaseMetaData(),
120: this .table);
121: }
122:
123: return this .uniqueConstraints;
124: }
125:
126: /**
127: * @see net.sf.hajdbc.TableProperties#getName()
128: */
129: @Override
130: public synchronized String getName() {
131: if (this .name == null) {
132: this .name = this .support.qualifyNameForDML(this .table);
133: }
134:
135: return this .name;
136: }
137:
138: /**
139: * @see net.sf.hajdbc.TableProperties#getIdentityColumns()
140: */
141: @Override
142: public synchronized Collection<String> getIdentityColumns()
143: throws SQLException {
144: if (this.identityColumns == null) {
145: this.identityColumns = this.support.getIdentityColumns(this
146: .getColumnMap().values());
147: }
148:
149: return this.identityColumns;
150: }
151: }
|