001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.cache.eager;
019
020import java.sql.DatabaseMetaData;
021import java.sql.SQLException;
022import java.util.Collection;
023import java.util.Map;
024
025import net.sf.hajdbc.ColumnProperties;
026import net.sf.hajdbc.ForeignKeyConstraint;
027import net.sf.hajdbc.IdentifierNormalizer;
028import net.sf.hajdbc.QualifiedName;
029import net.sf.hajdbc.QualifiedNameFactory;
030import net.sf.hajdbc.UniqueConstraint;
031import net.sf.hajdbc.UniqueConstraintFactory;
032import net.sf.hajdbc.cache.AbstractTableProperties;
033import net.sf.hajdbc.dialect.Dialect;
034
035/**
036 * @author Paul Ferraro
037 *
038 */
039public class EagerTableProperties extends AbstractTableProperties
040{
041        private Map<String, ColumnProperties> columnMap;
042        private UniqueConstraint primaryKey;
043        private Collection<UniqueConstraint> uniqueConstraints;
044        private Collection<ForeignKeyConstraint> foreignKeyConstraints;
045        private Collection<String> identityColumns;
046        
047        public EagerTableProperties(QualifiedName table, DatabaseMetaData metaData, Dialect dialect, QualifiedNameFactory factory) throws SQLException
048        {
049                super(table);
050                
051                IdentifierNormalizer normalizer = factory.getIdentifierNormalizer();
052                this.columnMap = dialect.getColumns(metaData, table, dialect.createColumnPropertiesFactory(normalizer));
053                UniqueConstraintFactory uniqueConstraintFactory = dialect.createUniqueConstraintFactory(normalizer);
054                this.primaryKey = dialect.getPrimaryKey(metaData, table, uniqueConstraintFactory);
055                this.uniqueConstraints = dialect.getUniqueConstraints(metaData, table, this.primaryKey, uniqueConstraintFactory);
056                this.foreignKeyConstraints = dialect.getForeignKeyConstraints(metaData, table, dialect.createForeignKeyConstraintFactory(factory));
057                this.identityColumns = dialect.getIdentityColumns(this.columnMap.values());
058        }
059
060        @Override
061        protected Map<String, ColumnProperties> getColumnMap()
062        {
063                return this.columnMap;
064        }
065
066        /**
067         * @see net.sf.hajdbc.TableProperties#getPrimaryKey()
068         */
069        @Override
070        public UniqueConstraint getPrimaryKey()
071        {
072                return this.primaryKey;
073        }
074
075        /**
076         * @see net.sf.hajdbc.TableProperties#getForeignKeyConstraints()
077         */
078        @Override
079        public Collection<ForeignKeyConstraint> getForeignKeyConstraints()
080        {
081                return this.foreignKeyConstraints;
082        }
083
084        /**
085         * @see net.sf.hajdbc.TableProperties#getUniqueConstraints()
086         */
087        @Override
088        public Collection<UniqueConstraint> getUniqueConstraints()
089        {
090                return this.uniqueConstraints;
091        }
092
093        /**
094         * @see net.sf.hajdbc.TableProperties#getIdentityColumns()
095         */
096        @Override
097        public Collection<String> getIdentityColumns()
098        {
099                return this.identityColumns;
100        }
101}