001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.core.storage.sql.jdbc.db; 021 022import java.io.Serializable; 023import java.util.Collection; 024import java.util.HashSet; 025import java.util.Iterator; 026import java.util.LinkedHashMap; 027import java.util.Map; 028import java.util.Set; 029 030import org.nuxeo.ecm.core.storage.sql.jdbc.dialect.Dialect; 031 032/** 033 * A collection of {@link Table}s. 034 * 035 * @author Florent Guillaume 036 */ 037public class Database implements Serializable { 038 039 private static final long serialVersionUID = 1L; 040 041 protected final Dialect dialect; 042 043 protected final Map<String, Table> tables; 044 045 protected final Set<String> physicalTables; 046 047 public Database(Dialect dialect) { 048 this.dialect = dialect; 049 tables = new LinkedHashMap<String, Table>(); 050 physicalTables = new HashSet<String>(); 051 } 052 053 public Table addTable(String name) throws IllegalArgumentException { 054 String physicalName = dialect.getTableName(name); 055 if (!physicalTables.add(physicalName)) { 056 throw new IllegalArgumentException("Duplicate table name: " + physicalName); 057 } 058 Table table = new TableImpl(dialect, physicalName, name); 059 tables.put(name, table); 060 return table; 061 } 062 063 public Table getTable(String name) { 064 return tables.get(name); 065 } 066 067 public Collection<Table> getTables() { 068 return tables.values(); 069 } 070 071 @Override 072 public String toString() { 073 StringBuilder buf = new StringBuilder(); 074 buf.append(getClass().getSimpleName()); 075 buf.append('('); 076 for (Iterator<Table> iter = tables.values().iterator(); iter.hasNext();) { 077 Table table = iter.next(); 078 buf.append(table.getPhysicalName()); 079 if (iter.hasNext()) { 080 buf.append(','); 081 } 082 } 083 buf.append(')'); 084 return buf.toString(); 085 } 086 087}