List of usage examples for org.hibernate.mapping ForeignKey sqlDropString
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema)
From source file:com.opengamma.util.db.management.AbstractDbManagement.java
License:Open Source License
@Override public void dropSchema(String catalog, String schema) { // Does not handle triggers or stored procedures yet ArrayList<String> script = new ArrayList<String>(); Connection conn = null;/* w w w .j av a 2 s . c o m*/ try { if (!getCatalogCreationStrategy().catalogExists(catalog)) { System.out.println("Catalog " + catalog + " does not exist"); return; // nothing to drop } conn = connect(catalog); if (schema != null) { Statement statement = conn.createStatement(); Collection<String> schemas = getAllSchemas(catalog, statement); statement.close(); if (!schemas.contains(schema)) { System.out.println("Schema " + schema + " does not exist"); return; // nothing to drop } } setActiveSchema(conn, schema); Statement statement = conn.createStatement(); // Drop constraints SQL if (getHibernateDialect().dropConstraints()) { for (Pair<String, String> constraint : getAllForeignKeyConstraints(catalog, schema, statement)) { String name = constraint.getFirst(); String table = constraint.getSecond(); ForeignKey fk = new ForeignKey(); fk.setName(name); fk.setTable(new Table(table)); String dropConstraintSql = fk.sqlDropString(getHibernateDialect(), null, schema); script.add(dropConstraintSql); } } // Drop views SQL for (String name : getAllViews(catalog, schema, statement)) { Table table = new Table(name); String dropViewStr = table.sqlDropString(getHibernateDialect(), null, schema); dropViewStr = dropViewStr.replaceAll("drop table", "drop view"); script.add(dropViewStr); } // Drop tables SQL for (String name : getAllTables(catalog, schema, statement)) { Table table = new Table(name); String dropTableStr = table.sqlDropString(getHibernateDialect(), null, schema); script.add(dropTableStr); } // Now execute it all statement.close(); statement = conn.createStatement(); for (String sql : script) { //System.out.println("Executing \"" + sql + "\""); statement.executeUpdate(sql); } statement.close(); statement = conn.createStatement(); // Drop sequences SQL script.clear(); for (String name : getAllSequences(catalog, schema, statement)) { final SequenceStructure sequenceStructure = new SequenceStructure(getHibernateDialect(), name, 0, 1, Long.class); String[] dropSequenceStrings = sequenceStructure.sqlDropStrings(getHibernateDialect()); script.addAll(Arrays.asList(dropSequenceStrings)); } //now execute drop sequence statement.close(); statement = conn.createStatement(); for (String sql : script) { //System.out.println("Executing \"" + sql + "\""); statement.executeUpdate(sql); } statement.close(); } catch (SQLException e) { throw new OpenGammaRuntimeException("Failed to drop schema", e); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } }
From source file:com.opengamma.util.db.management.SqlServer2008DbManagement.java
License:Open Source License
@Override public void dropSchema(String catalog, String schema) { // Does not handle triggers or stored procedures yet ArrayList<String> script = new ArrayList<String>(); Connection conn = null;/* w ww .jav a 2 s . com*/ try { if (!getCatalogCreationStrategy().catalogExists(catalog)) { System.out.println("Catalog " + catalog + " does not exist"); return; // nothing to drop } conn = connect(catalog); if (schema != null) { Statement statement = conn.createStatement(); Collection<String> schemas = getAllSchemas(catalog, statement); statement.close(); if (!schemas.contains(schema)) { System.out.println("Schema " + schema + " does not exist"); return; // nothing to drop } } setActiveSchema(conn, schema); Statement statement = conn.createStatement(); // Drop constraints SQL if (getHibernateDialect().dropConstraints()) { for (Pair<String, String> constraint : getAllForeignKeyConstraints(catalog, schema, statement)) { String name = constraint.getFirst(); String table = constraint.getSecond(); ForeignKey fk = new ForeignKey(); fk.setName(name); fk.setTable(new Table(table)); String dropConstraintSql = fk.sqlDropString(getHibernateDialect(), null, schema); script.add(dropConstraintSql); } } // Drop views SQL for (String name : getAllViews(catalog, schema, statement)) { Table table = new Table(name); String dropViewStr = table.sqlDropString(getHibernateDialect(), null, schema); dropViewStr = dropViewStr.replaceAll("drop table", "drop view"); script.add(dropViewStr); } // Drop tables SQL for (String name : getAllTables(catalog, schema, statement)) { Table table = new Table(name); String dropTableStr = table.sqlDropString(getHibernateDialect(), null, schema); script.add(dropTableStr); } // Now execute it all statement.close(); statement = conn.createStatement(); for (String sql : script) { //System.out.println("Executing \"" + sql + "\""); statement.executeUpdate(sql); } statement.close(); statement = conn.createStatement(); // Drop sequences SQL script.clear(); for (String name : getAllSequences(catalog, schema, statement)) { Table table = new Table(name); String dropTableStr = table.sqlDropString(getHibernateDialect(), null, schema); script.add(dropTableStr); } //now execute drop sequence statement.close(); statement = conn.createStatement(); for (String sql : script) { //System.out.println("Executing \"" + sql + "\""); statement.executeUpdate(sql); } statement.close(); } catch (SQLException e) { throw new OpenGammaRuntimeException("Failed to drop schema", e); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private void transferForeignKeys(Connection connection, Table fromTable, Table toTable) throws SQLException { DatabaseMetadata meta = new DatabaseMetadata(connection, dialect); Iterator i = config.getTableMappings(); while (i.hasNext()) { Table t = (Table) i.next();//from w w w. j ava 2 s . c o m Iterator fki = t.getForeignKeyIterator(); while (fki.hasNext()) { ForeignKey fk = (ForeignKey) fki.next(); Table referencedTable = fk.getReferencedTable(); if (referencedTable != null && referencedTable == fromTable) { TableMetadata tableInfo = meta.getTableMetadata(t.getName(), defaultSchema, defaultCatalog, false); // verify that the fk is actually in the database. if (tableInfo.getForeignKeyMetadata(fk.getName()) == null) { // foreign key does not exist, so do not drop or recreate it. continue; } String sql = fk.sqlDropString(dialect, defaultCatalog, defaultSchema); LOG.info(sql); JDBCUtils.execute(connection, sql); fk.setReferencedTable(toTable); sql = fk.sqlCreateString(dialect, null, defaultCatalog, defaultSchema); LOG.info(sql); JDBCUtils.execute(connection, sql); } } } }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
private List<ForeignKey> dropColumnConstraints(Connection connection, Table table, Column column) throws SQLException { List<ForeignKey> droppedConstraints = new LinkedList<ForeignKey>(); Iterator fks = table.getForeignKeyIterator(); while (fks.hasNext()) { ForeignKey fk = (ForeignKey) fks.next(); if (fk.getColumns().contains(column)) { String sql = fk.sqlDropString(dialect, defaultCatalog, defaultSchema); LOG.info(sql);/*ww w. j a v a 2s. co m*/ JDBCUtils.execute(connection, sql); droppedConstraints.add(fk); } } return droppedConstraints; }
From source file:org.apereo.portal.tools.dbloader.HibernateDbLoader.java
License:Apache License
/** Generate the drop scripts and add them to the script list */ @SuppressWarnings("unchecked") protected List<String> dropScript(Collection<Table> tables, Dialect dialect, String defaultCatalog, String defaultSchema) {//from www . j a v a 2s.com final List<String> script = new ArrayList<String>(tables.size() * 2); if (dialect.dropConstraints()) { for (final Table table : tables) { if (table.isPhysicalTable()) { for (final Iterator<ForeignKey> subIter = table.getForeignKeyIterator(); subIter.hasNext();) { final ForeignKey fk = subIter.next(); if (fk.isPhysicalConstraint()) { script.add(fk.sqlDropString(dialect, defaultCatalog, defaultSchema)); } } } } } for (final Table table : tables) { if (table.isPhysicalTable()) { script.add(table.sqlDropString(dialect, defaultCatalog, defaultSchema)); } } return script; }
From source file:org.jbpm.db.JbpmSchema.java
License:Open Source License
public String[] getCleanSql() { if (cleanSql == null) { // loop over all foreign key constraints List dropForeignKeysSql = new ArrayList(); List createForeignKeysSql = new ArrayList(); Iterator iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { Iterator subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop foreign key constraint sql dropForeignKeysSql.add( fk.sqlDropString(dialect, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); // and collect the create foreign key constraint sql createForeignKeysSql.add(fk.sqlCreateString(dialect, mapping, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); }//from w w w . ja va 2 s . c o m } } } List deleteSql = new ArrayList(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); deleteSql.add("delete from " + table.getName()); } // glue // - drop foreign key constraints // - delete contents of all tables // - create foreign key constraints // together to form the clean script List cleanSqlList = new ArrayList(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); } return cleanSql; }
From source file:org.jbpm.identity.hibernate.IdentitySchema.java
License:Open Source License
public String[] getCleanSql() { if (cleanSql == null) { // loop over all foreign key constraints List dropForeignKeysSql = new ArrayList(); List createForeignKeysSql = new ArrayList(); Iterator iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { Iterator subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop key constraint dropForeignKeysSql.add( fk.sqlDropString(dialect, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); createForeignKeysSql.add(fk.sqlCreateString(dialect, mapping, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); }// ww w . j av a 2 s. co m } } } List deleteSql = new ArrayList(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); deleteSql.add("delete from " + table.getName()); } List cleanSqlList = new ArrayList(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); } return cleanSql; }
From source file:org.jbpm.test.Db.java
License:Open Source License
public static void clean(ProcessEngine processEngine) { SessionFactory sessionFactory = processEngine.get(SessionFactory.class); // when running this with a remote ejb invocation configuration, there is no // session factory and no cleanup needs to be done if (sessionFactory == null) { return;/* w w w. j av a 2 s. co m*/ } String[] cleanSql = cleanSqlCache.get(processEngine); if (cleanSql == null) { Configuration configuration = processEngine.get(Configuration.class); SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory; Dialect dialect = sessionFactoryImplementor.getDialect(); // loop over all foreign key constraints List<String> dropForeignKeysSql = new ArrayList<String>(); List<String> createForeignKeysSql = new ArrayList<String>(); Iterator<Table> iter = configuration.getTableMappings(); //if no session-factory is build, the configuration is not fully initialized. //Hence, the ForeignKey's won't have a referenced table. This is calculated on //second pass. configuration.buildMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { String catalog = table.getCatalog(); String schema = table.getSchema(); Iterator<ForeignKey> subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop foreign key constraint sql dropForeignKeysSql.add(fk.sqlDropString(dialect, catalog, schema)); // MySQLDialect creates an index for each foreign key. // see // http://opensource.atlassian.com/projects/hibernate/browse/HHH-2155 // This index should be dropped or an error will be thrown during // the creation phase if (dialect instanceof MySQLDialect) { dropForeignKeysSql .add("alter table " + table.getName() + " drop key " + fk.getName()); } // and collect the create foreign key constraint sql createForeignKeysSql .add(fk.sqlCreateString(dialect, sessionFactoryImplementor, catalog, schema)); } } } } List<String> deleteSql = new ArrayList<String>(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { deleteSql.add("delete from " + table.getName()); } } // glue // - drop foreign key constraints // - delete contents of all tables // - create foreign key constraints // together to form the clean script List<String> cleanSqlList = new ArrayList<String>(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); cleanSqlCache.put(processEngine, cleanSql); } Session session = sessionFactory.openSession(); try { for (String query : cleanSql) { // log.trace(query); session.createSQLQuery(query).executeUpdate(); } } finally { session.close(); } }
From source file:org.n52.sos.ds.datasource.CustomConfiguration.java
License:Open Source License
protected List<String> generateConstraintDropScript(final Dialect d, final String c, final String s, final DatabaseMetadata m) throws HibernateException { final List<String> script = new LinkedList<String>(); final Iterator<Table> itr = getTableMappings(); while (itr.hasNext()) { final Table table = itr.next(); // TODO remove because fails if table definition is quoted // final String tableName = table.getQualifiedName(d, c, s); if (checkTable(table, m)) { @SuppressWarnings("unchecked") final Iterator<ForeignKey> subItr = table.getForeignKeyIterator(); final TableMetadata tableMeta = m.getTableMetadata(table.getName(), s, c, table.isQuoted()); while (subItr.hasNext()) { final ForeignKey fk = subItr.next(); if (fk.isPhysicalConstraint() && tableMeta.getForeignKeyMetadata(fk) != null) { script.add(fk.sqlDropString(d, c, s)); }//from w w w .jav a2 s.co m } } } return script; }