001/*
002 * Copyright (c) 2008-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2010-10-13 19:22:12 +0000 (Wed, 13 Oct 2010) $' 
007 * '$Revision: 26060 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.util.sql;
031
032import java.util.HashSet;
033import java.util.LinkedHashMap;
034import java.util.Map;
035import java.util.Set;
036
037/**
038 *
039 * A generic representation of an SQL schema.
040 *
041 * @author Daniel Crawl
042 * @version $Id: Schema.java 26060 2010-10-13 19:22:12Z barseghian $
043 *
044 */
045    
046public class Schema
047{
048    /** Construct a new schema.
049     *  @param version the version of the schema 
050     */
051    public Schema(int version)
052    {
053        _tableMap = new LinkedHashMap<String,Table>(); 
054        _majorVersion = version;
055    }
056
057    /** Returns true if schema contains a specific table. */
058    public boolean containsTable(String name)
059    {
060        return _tableMap.containsKey(name);
061    }
062
063    /** Create a new table. If a table already exists with the same
064     *  name, it is overwritten.
065     */
066    public Table createTable(String name)
067    {
068        Table retval = new Table(name);
069        putTable(name, retval);
070        return retval;
071    }
072
073    /** Get an existing table. If table does not exist, returns null. */
074    public Table getTable(String name)
075    {
076        return _tableMap.get(name);
077    }
078
079    /** Get the major version. */
080    public int getMajorVersion()
081    {
082        return _majorVersion;
083    }
084    
085    /** Get the minor version. */
086    public int getMinorVersion()
087    {
088        return _minorVersion;
089    }
090    
091    /** Get the version string that includes the major and minor versions. */
092    public String getVersionString()
093    {
094        return _majorVersion + "." + _minorVersion;
095    }
096    
097    /** Add a new or change an existing table. */
098    public void putTable(String name, Table table)
099    {
100        _tableMap.put(name, table);
101    }
102
103    /** Remove a table. */
104    public void removeTable(String name)
105    {
106        _tableMap.remove(name);
107    }
108
109    /** Set the major version. */
110    public void setMajorVersion(int version)
111    {
112        _majorVersion = version;
113    }
114
115    /** Set the minor version. */
116    public void setMinorVersion(int version)
117    {
118        _minorVersion = version;
119    }
120
121    /** Get a list of tables. */
122    public Set<Table> tables()
123    {
124        return new HashSet<Table>(_tableMap.values());
125    }    
126
127    /** Get the names of all tables. */
128    public Set<String> tableNames()
129    {
130        return new HashSet<String>(_tableMap.keySet());
131    }
132
133    ////////////////////////////////////////////////////////////////////////
134    //// private variables
135
136    /** A mapping of table name to table object. */
137    private Map<String,Table> _tableMap;
138    
139    /** The major version of the schema. */
140    private int _majorVersion = -1;
141    
142    /** The minor version of the schema. */
143    private int _minorVersion = 0;
144
145}