List of usage examples for org.hibernate.mapping Table Table
public Table(String name)
From source file:com.clican.pluto.orm.tool.DatabaseMetadata.java
License:LGPL
public boolean isTable(Object key) throws HibernateException { if (key instanceof String) { Table tbl = new Table((String) key); if (getTableMetadata(tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted()) != null) { return true; } else {/*ww w. j a v a2 s. c om*/ String[] strings = StringHelper.split(".", (String) key); if (strings.length == 3) { tbl = new Table(strings[2]); tbl.setCatalog(strings[0]); tbl.setSchema(strings[1]); return getTableMetadata(tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted()) != null; } else if (strings.length == 2) { tbl = new Table(strings[1]); tbl.setSchema(strings[0]); return getTableMetadata(tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted()) != null; } } } return false; }
From source file:com.opengamma.util.db.management.AbstractDbManagement.java
License:Open Source License
@Override public void clearTables(String catalog, String schema, Collection<String> ignoredTables) { LinkedList<String> script = new LinkedList<String>(); Connection conn = null;/*from w ww . j a v a2 s .co m*/ try { if (!getCatalogCreationStrategy().catalogExists(catalog)) { return; // nothing to clear } conn = connect(catalog); setActiveSchema(conn, schema); Statement statement = conn.createStatement(); // Clear tables SQL List<String> tablesToClear = new ArrayList<String>(); for (String name : getAllTables(catalog, schema, statement)) { if (!ignoredTables.contains(name.toLowerCase())) { tablesToClear.add(name); } } List<String> clearTablesCommands = getClearTablesCommand(schema, tablesToClear); script.addAll(clearTablesCommands); for (String name : tablesToClear) { Table table = new Table(name); if (matches(table.getName().toLowerCase(), Pattern.compile(".*?hibernate_sequence"))) { // if it's a sequence table, reset it script.add("INSERT INTO " + table.getQualifiedName(getHibernateDialect(), null, schema) + " values ( 1 )"); } } // Now execute it all. Constraints are taken into account by retrying the failed statement after all // dependent tables have been cleared first. int i = 0; int maxAttempts = script.size() * 3; // make sure the loop eventually terminates. Important if there's a cycle in the table dependency graph SQLException latestException = null; while (i < maxAttempts && !script.isEmpty()) { String sql = script.remove(); try { statement.executeUpdate(sql); } catch (SQLException e) { // assume it failed because of a constraint violation // try deleting other tables first - make this the new last statement latestException = e; script.add(sql); } i++; } statement.close(); if (i == maxAttempts && !script.isEmpty()) { throw new OpenGammaRuntimeException( "Failed to clear tables - is there a cycle in the table dependency graph?", latestException); } } catch (SQLException e) { throw new OpenGammaRuntimeException("Failed to clear tables", e); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } }
From source file:com.opengamma.util.db.management.AbstractDbManagement.java
License:Open Source License
protected List<String> getClearTablesCommand(String schema, List<String> tablesToClear) { List<String> clearTablesCommands = new ArrayList<String>(); for (String name : tablesToClear) { Table table = new Table(name); clearTablesCommands.add("DELETE FROM " + table.getQualifiedName(getHibernateDialect(), null, schema)); }//from ww w. ja v a 2 s. com return clearTablesCommands; }
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 . jav a 2s. 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;//from w ww . j a v 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)) { 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.opengamma.util.db.tool.DbToolTest.java
License:Open Source License
private void createTestTable() throws SQLException { Table table = new Table(TEST_TABLE); try {//w w w . j a v a 2 s . c om String dropSql = table.sqlDropString(_tool.getHibernateDialect(), null, _tool.getTestSchema()); _tool.executeSql(_tool.getTestCatalog(), _tool.getTestSchema(), dropSql); } catch (OpenGammaRuntimeException e) { // It might not exist, that's OK } String createSql = "CREATE TABLE " + table.getQualifiedName(_tool.getHibernateDialect(), null, _tool.getTestSchema()) + " (test_column char(50))"; _tool.executeSql(_tool.getTestCatalog(), _tool.getTestSchema(), createSql); }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
protected Table clone(Table table) { Table clone = new Table(table.getName()); clone.setAbstract(table.isAbstract()); clone.setCatalog(table.getCatalog()); clone.setComment(table.getComment()); clone.setName(table.getName());/* w w w . j av a 2 s.c om*/ clone.setPrimaryKey(table.getPrimaryKey()); clone.setQuoted(table.isQuoted()); clone.setRowId(table.getRowId()); clone.setSchema(table.getSchema()); clone.setSubselect(table.getSubselect()); Iterator columns = table.getColumnIterator(); while (columns.hasNext()) { Column column = (Column) columns.next(); clone.addColumn(column); } Iterator foreignKeys = table.getForeignKeyIterator(); while (foreignKeys.hasNext()) { ForeignKey key = (ForeignKey) foreignKeys.next(); clone.createForeignKey(key.getName(), key.getColumns(), key.getReferencedEntityName(), key.getReferencedColumns()); } return clone; }
From source file:com.zutubi.pulse.master.transfer.jdbc.HibernateUniqueKeyTable.java
License:Apache License
public static Table getMapping() { Table table = new Table("hibernate_unique_key"); Column column = new Column("next_hi"); SimpleValue value = new SimpleValue(null, table); value.setTypeName(int.class.getName()); column.setValue(value);//from ww w. j a va2 s . co m column.setSqlTypeCode(Types.INTEGER); table.addColumn(column); return table; }
From source file:corner.migration.services.impl.MigrationServiceImpl.java
License:Apache License
/** * /*from w w w .j a va 2 s . c om*/ * * @param tableName * ?? * @since 0.0.2 * @see corner.migration.services.MigrationService#dropTable(java.lang.String) */ @Override public void dropTable(String tableName) { Table table = new Table(tableName); this.executeSchemaScript(table.sqlDropString(this.dialect, defaultCatalog, defaultSchema)); }
From source file:corner.services.migration.impl.MigrationServiceImpl.java
License:Apache License
/** * /*from w w w . jav a 2s. c om*/ * * @param tableName * ?? * @since 0.0.2 */ public void dropTable(String tableName) { Table table = new Table(tableName); this.executeSchemaScript(table.sqlDropString(this.dialect, defaultCatalog, defaultSchema)); }