diff options
Diffstat (limited to 'plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java')
-rw-r--r-- | plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java new file mode 100644 index 0000000000..7d8dc8f5f2 --- /dev/null +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/spi/db/DBSchema.java @@ -0,0 +1,208 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + **************************************************************************/ +package org.eclipse.net4j.spi.db; + +import org.eclipse.net4j.db.DBException; +import org.eclipse.net4j.db.DBUtil; +import org.eclipse.net4j.db.IDBAdapter; +import org.eclipse.net4j.db.IDBConnectionProvider; +import org.eclipse.net4j.db.IDBRowHandler; +import org.eclipse.net4j.db.ddl.IDBSchema; +import org.eclipse.net4j.db.ddl.IDBTable; +import org.eclipse.net4j.internal.db.ddl.DBSchemaElement; +import org.eclipse.net4j.internal.db.ddl.DBTable; + +import javax.sql.DataSource; + +import java.io.PrintStream; +import java.sql.Connection; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author Eike Stepper + */ +public class DBSchema extends DBSchemaElement implements IDBSchema +{ + private String name; + + private Map<String, DBTable> tables = new HashMap<String, DBTable>(); + + private boolean locked; + + public DBSchema(String name) + { + this.name = name; + } + + public String getFullName() + { + return name; + } + + public IDBSchema getSchema() + { + return this; + } + + public String getName() + { + return name; + } + + public DBTable addTable(String name) throws DBException + { + assertUnlocked(); + if (tables.containsKey(name)) + { + throw new DBException("DBTable exists: " + name); + } + + DBTable table = new DBTable(this, name); + tables.put(name, table); + return table; + } + + public DBTable getTable(String name) + { + return tables.get(name); + } + + public DBTable[] getTables() + { + return tables.values().toArray(new DBTable[tables.size()]); + } + + public boolean isLocked() + { + return locked; + } + + public boolean lock() + { + return locked = true; + } + + public Set<IDBTable> create(IDBAdapter dbAdapter, Connection connection) throws DBException + { + return dbAdapter.createTables(tables.values(), connection); + } + + public Set<IDBTable> create(IDBAdapter dbAdapter, DataSource dataSource) throws DBException + { + return create(dbAdapter, DBUtil.createConnectionProvider(dataSource)); + } + + public Set<IDBTable> create(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException + { + Connection connection = null; + + try + { + connection = connectionProvider.getConnection(); + return create(dbAdapter, connection); + } + finally + { + DBUtil.close(connection); + } + } + + public void drop(IDBAdapter dbAdapter, Connection connection) throws DBException + { + dbAdapter.dropTables(tables.values(), connection); + } + + public void drop(IDBAdapter dbAdapter, DataSource dataSource) throws DBException + { + drop(dbAdapter, DBUtil.createConnectionProvider(dataSource)); + } + + public void drop(IDBAdapter dbAdapter, IDBConnectionProvider connectionProvider) throws DBException + { + Connection connection = null; + + try + { + connection = connectionProvider.getConnection(); + drop(dbAdapter, connection); + } + finally + { + DBUtil.close(connection); + } + } + + public void export(Connection connection, PrintStream out) throws DBException + { + for (DBTable table : getTables()) + { + export(table, connection, out); + } + } + + private void export(final DBTable table, Connection connection, final PrintStream out) + { + if (DBUtil.select(connection, new IDBRowHandler() + { + public boolean handle(int row, Object... values) + { + if (row == 0) + { + String tableName = table.getName(); + out.println(tableName); + for (int i = 0; i < tableName.length(); i++) + { + out.print("="); + } + + out.println(); + } + + out.println(Arrays.asList(values)); + return true; + } + }, table.getFields()) > 0) + { + out.println(); + } + } + + public void export(DataSource dataSource, PrintStream out) throws DBException + { + export(DBUtil.createConnectionProvider(dataSource), out); + } + + public void export(IDBConnectionProvider connectionProvider, PrintStream out) throws DBException + { + Connection connection = null; + + try + { + connection = connectionProvider.getConnection(); + export(connection, out); + } + finally + { + DBUtil.close(connection); + } + } + + public void assertUnlocked() throws DBException + { + if (locked) + { + throw new DBException("DBSchema locked: " + name); + } + } +} |