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.ArrayList;
023import java.util.Collection;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Map.Entry;
028
029import net.sf.hajdbc.QualifiedName;
030import net.sf.hajdbc.SequenceProperties;
031import net.sf.hajdbc.SequenceSupport;
032import net.sf.hajdbc.TableProperties;
033import net.sf.hajdbc.cache.AbstractDatabaseProperties;
034import net.sf.hajdbc.dialect.Dialect;
035
036/**
037 * @author Paul Ferraro
038 *
039 */
040public class EagerDatabaseProperties extends AbstractDatabaseProperties
041{
042        private final Map<QualifiedName, TableProperties> tables = new HashMap<QualifiedName, TableProperties>();
043        private final Map<QualifiedName, SequenceProperties> sequences = new HashMap<QualifiedName, SequenceProperties>();
044        private final List<String> defaultSchemas;
045        private final Map<Integer, Map.Entry<String, Integer>> types;
046        
047        public EagerDatabaseProperties(DatabaseMetaData metaData, Dialect dialect) throws SQLException
048        {
049                super(metaData, dialect);
050                
051                Collection<QualifiedName> tables = dialect.getTables(metaData, this.nameFactory);
052                
053                for (QualifiedName table: tables)
054                {
055                        TableProperties properties = new EagerTableProperties(table, metaData, dialect, this.nameFactory);
056                        
057                        this.tables.put(properties.getName(), properties);
058                }
059                
060                List<String> defaultSchemaList = dialect.getDefaultSchemas(metaData);
061                
062                this.defaultSchemas = new ArrayList<String>(defaultSchemaList);
063                
064                SequenceSupport support = dialect.getSequenceSupport();
065                if (support != null)
066                {
067                        for (SequenceProperties sequence: support.getSequences(metaData, support.createSequencePropertiesFactory(this.nameFactory)))
068                        {
069                                this.sequences.put(sequence.getName(), sequence);
070                        }
071                }
072                
073                this.types = dialect.getTypes(metaData);
074        }
075        
076        @Override
077        protected List<String> defaultSchemas()
078        {
079                return this.defaultSchemas;
080        }
081
082        @Override
083        protected Map<QualifiedName, SequenceProperties> sequences()
084        {
085                return this.sequences;
086        }
087
088        @Override
089        protected Map<QualifiedName, TableProperties> tables()
090        {
091                return this.tables;
092        }
093
094        @Override
095        protected Map<Integer, Entry<String, Integer>> types()
096        {
097                return this.types;
098        }
099}