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.DatabaseMetaData;
024: import java.sql.SQLException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030:
031: import net.sf.hajdbc.DatabaseProperties;
032: import net.sf.hajdbc.Dialect;
033: import net.sf.hajdbc.QualifiedName;
034: import net.sf.hajdbc.SequenceProperties;
035: import net.sf.hajdbc.TableProperties;
036:
037: /**
038: * @author Paul Ferraro
039: *
040: */
041: public class EagerDatabaseProperties implements DatabaseProperties {
042: private DatabaseMetaDataSupport support;
043: private Map<String, TableProperties> tableMap = new HashMap<String, TableProperties>();
044: private Map<String, SequenceProperties> sequenceMap = new HashMap<String, SequenceProperties>();
045: private boolean supportsSelectForUpdate;
046: private List<String> defaultSchemaList;
047:
048: public EagerDatabaseProperties(DatabaseMetaData metaData,
049: Dialect dialect) throws SQLException {
050: this .support = new DatabaseMetaDataSupport(metaData, dialect);
051:
052: this .supportsSelectForUpdate = metaData
053: .supportsSelectForUpdate();
054:
055: Collection<QualifiedName> tables = this .support
056: .getTables(metaData);
057:
058: for (QualifiedName table : tables) {
059: TableProperties properties = new EagerTableProperties(
060: metaData, this .support, table);
061:
062: this .tableMap.put(properties.getName(), properties);
063: }
064:
065: List<String> defaultSchemaList = dialect
066: .getDefaultSchemas(metaData);
067:
068: this .defaultSchemaList = new ArrayList<String>(
069: defaultSchemaList);
070:
071: for (SequenceProperties sequence : this .support
072: .getSequences(metaData)) {
073: this .sequenceMap.put(sequence.getName(), sequence);
074: }
075: }
076:
077: /**
078: * @see net.sf.hajdbc.DatabaseProperties#getTables()
079: */
080: @Override
081: public Collection<TableProperties> getTables() {
082: return this .tableMap.values();
083: }
084:
085: /**
086: * @see net.sf.hajdbc.DatabaseProperties#findTable(java.lang.String)
087: */
088: @Override
089: public TableProperties findTable(String table) throws SQLException {
090: return this .support.find(this .tableMap, table,
091: this .defaultSchemaList);
092: }
093:
094: /**
095: * @see net.sf.hajdbc.DatabaseProperties#supportsSelectForUpdate()
096: */
097: @Override
098: public boolean supportsSelectForUpdate() {
099: return this .supportsSelectForUpdate;
100: }
101:
102: /**
103: * @see net.sf.hajdbc.DatabaseProperties#findSequence(java.lang.String)
104: */
105: @Override
106: public SequenceProperties findSequence(String sequence)
107: throws SQLException {
108: return this .support.find(this .sequenceMap, sequence,
109: this .defaultSchemaList);
110: }
111:
112: /**
113: * @see net.sf.hajdbc.DatabaseProperties#getSequences()
114: */
115: @Override
116: public Collection<SequenceProperties> getSequences()
117: throws SQLException {
118: return this.sequenceMap.values();
119: }
120: }
|