List of usage examples for org.hibernate.mapping Table getCatalog
public String getCatalog()
From source file:com.blazebit.persistence.integration.hibernate.base.SimpleTableNameFormatter.java
License:Apache License
public String getQualifiedTableName(Dialect dialect, Table table) { final String catalogName = table.getCatalog(); final String schemaName = table.getSchema(); final String objectName = table.getName(); StringBuilder buff = new StringBuilder(); if (catalogName != null) { buff.append(catalogName.toString()).append('.'); }//from w w w.j a v a 2 s . co m if (schemaName != null) { buff.append(schemaName.toString()).append('.'); } buff.append(objectName.toString()); return buff.toString(); }
From source file:com.clican.pluto.orm.dynamic.impl.DataBaseOperationImpl.java
License:LGPL
@SuppressWarnings("unchecked") public void alterTable(Configuration cfg, ModelDescription oldOne, ModelDescription newOne) { Connection conn = null;/*from w w w . ja va 2s . c om*/ DatabaseMetadata meta = null; String defaultCatalog = cfg.getProperties().getProperty(Environment.DEFAULT_CATALOG); String defaultSchema = cfg.getProperties().getProperty(Environment.DEFAULT_SCHEMA); List<String> alterSqls = new ArrayList<String>(); try { conn = dataSource.getConnection(); meta = new DatabaseMetadata(conn, dialect); Mapping mapping = cfg.buildMapping(); // Alter table name; if (!oldOne.getName().equals(newOne.getName())) { String alterTableName = "alter table " + dialect.openQuote() + oldOne.getName().toUpperCase() + dialect.closeQuote() + "rename to " + dialect.openQuote() + newOne.getName().toUpperCase() + dialect.closeQuote(); executeSql(conn, alterTableName); } List<PropertyDescription> oldPropertyDescriptionList = oldOne.getPropertyDescriptionList(); List<PropertyDescription> currentPropertyDescriptionList = newOne.getPropertyDescriptionList(); List<PropertyDescription> removePropertyList = new ArrayList<PropertyDescription>(); List<PropertyDescription> addPropertyList = new ArrayList<PropertyDescription>( currentPropertyDescriptionList); Map<PropertyDescription, PropertyDescription> pdMap = new HashMap<PropertyDescription, PropertyDescription>(); for (PropertyDescription pd1 : oldPropertyDescriptionList) { boolean remove = true; for (PropertyDescription pd2 : currentPropertyDescriptionList) { if (pd1.getId().equals(pd2.getId())) { addPropertyList.remove(pd2); if (!pd1.equals(pd2)) { pdMap.put(pd2, pd1); } remove = false; break; } } if (remove) { removePropertyList.add(pd1); } } Iterator<Table> tableIter = cfg.getTableMappings(); while (tableIter.hasNext()) { Table table = tableIter.next(); TableMetadata tableInfo = meta.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted() ); if (tableInfo == null) { alterSqls.add(table.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } else { if (!table.getName().equalsIgnoreCase(newOne.getName())) { continue; } for (PropertyDescription removeProperty : removePropertyList) { if (removeProperty.getControl().isSupportMutil() && removeProperty.getControl().isDynamic()) { alterSqls.add("drop table " + dialect.openQuote() + newOne.getName().toUpperCase() + "_" + removeProperty.getName().toUpperCase() + "_RELATION" + dialect.closeQuote()); } else { if (((DialectExtention) dialect).needDropForeignKeyBeforeDropColumn()) { ForeignKeyMetadata fkm = tableInfo.getForeignKeyMetadataByColumnNames( new String[] { removeProperty.getName().toUpperCase() }); if (fkm != null) { alterSqls.add("alter table " + dialect.openQuote() + newOne.getName().toUpperCase() + dialect.closeQuote() + " " + dialect.getDropForeignKeyString() + " " + dialect.openQuote() + fkm.getName() + dialect.closeQuote()); } } alterSqls.add("alter table " + dialect.openQuote() + newOne.getName().toUpperCase() + dialect.closeQuote() + " drop column " + dialect.openQuote() + removeProperty.getName().toUpperCase() + dialect.closeQuote()); } } StringBuffer root = new StringBuffer("alter table ") .append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema)).append(' ') .append(dialect.getAddColumnString()); Iterator<Column> iter = table.getColumnIterator(); while (iter.hasNext()) { Column column = iter.next(); PropertyDescription pd = null; if ((pd = contains(column.getName(), addPropertyList)) != null) { StringBuffer alter = new StringBuffer(root.toString()).append(' ') .append(column.getQuotedName(dialect)).append(' ') .append(column.getSqlType(dialect, mapping)); String defaultValue = column.getDefaultValue(); if (defaultValue != null) { alter.append(" default ").append(defaultValue); if (column.isNullable()) { alter.append(dialect.getNullColumnString()); } else { alter.append(" not null"); } } boolean useUniqueConstraint = column.isUnique() && dialect.supportsUnique() && (!column.isNullable() || dialect.supportsNotNullUnique()); if (useUniqueConstraint) { alter.append(" unique"); } if (column.hasCheckConstraint() && dialect.supportsColumnCheck()) { alter.append(" check(").append(column.getCheckConstraint()).append(")"); } String columnComment = column.getComment(); if (columnComment != null) { alter.append(dialect.getColumnComment(columnComment)); } alterSqls.add(alter.toString()); } else if ((pd = contains(column.getName(), pdMap)) != null) { PropertyDescription newPd = pd; PropertyDescription oldPd = pdMap.get(pd); if (!oldPd.getName().equalsIgnoreCase(newPd.getName())) { StringBuffer renameColumn = new StringBuffer("alter table ") .append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema)) .append(' ').append(((DialectExtention) dialect).getRenameColumnString( oldPd.getName().toUpperCase(), newPd.getName().toUpperCase())); if (((DialectExtention) dialect).isAddColumnDefinitionWhenRename()) { renameColumn.append(" "); renameColumn.append(column.getSqlType(dialect, mapping)); } executeSql(conn, renameColumn.toString()); } StringBuffer alterColumn = new StringBuffer("alter table ") .append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema)) .append(' ').append(((DialectExtention) dialect).getModifyColumnString(column)) .append(' ').append(column.getQuotedName(dialect)); alterColumn.append(" "); alterColumn.append(column.getSqlType(dialect, mapping)); String defaultValue = column.getDefaultValue(); if (defaultValue != null) { alterColumn.append(" default ").append(defaultValue); if (column.isNullable()) { alterColumn.append(dialect.getNullColumnString()); } else { alterColumn.append(" not null"); } } boolean useUniqueConstraint = column.isUnique() && dialect.supportsUnique() && (!column.isNullable() || dialect.supportsNotNullUnique()); if (useUniqueConstraint) { alterColumn.append(" unique"); } if (column.hasCheckConstraint() && dialect.supportsColumnCheck()) { alterColumn.append(" check(").append(column.getCheckConstraint()).append(")"); } String columnComment = column.getComment(); if (columnComment != null) { alterColumn.append(dialect.getColumnComment(columnComment)); } alterSqls.add(alterColumn.toString()); } } } } tableIter = cfg.getTableMappings(); while (tableIter.hasNext()) { Table table = tableIter.next(); Iterator<ForeignKey> subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { TableMetadata tableInfo = meta.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted() ); if (tableInfo == null) { String[] cols = new String[fk.getColumnSpan()]; String[] refcols = new String[fk.getColumnSpan()]; int i = 0; Iterator<Column> refiter = null; if (fk.isReferenceToPrimaryKey()) { refiter = fk.getReferencedTable().getPrimaryKey().getColumnIterator(); } else { refiter = fk.getReferencedColumns().iterator(); } Iterator<Column> columnIter = fk.getColumnIterator(); while (columnIter.hasNext()) { cols[i] = ((Column) columnIter.next()).getQuotedName(dialect); refcols[i] = ((Column) refiter.next()).getQuotedName(dialect); i++; } String result = dialect .getAddForeignKeyConstraintString( fk.getName(), cols, fk.getReferencedTable().getQualifiedName(dialect, defaultCatalog, defaultSchema), refcols, fk.isReferenceToPrimaryKey()); StringBuffer createFK = new StringBuffer("alter table ") .append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema)) .append(dialect.supportsCascadeDelete() ? result + " on delete cascade" : result); alterSqls.add(createFK.toString()); } } } } this.executeSqls(conn, alterSqls); } catch (Exception e) { throw new PlutoException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { log.error("", e); } } } }
From source file:com.clican.pluto.orm.dynamic.impl.DataBaseOperationImpl.java
License:LGPL
@SuppressWarnings("unchecked") public void createTable(Configuration cfg) { Connection conn = null;/*ww w.j a v a 2 s . c o m*/ DatabaseMetadata meta = null; String defaultCatalog = cfg.getProperties().getProperty(Environment.DEFAULT_CATALOG); String defaultSchema = cfg.getProperties().getProperty(Environment.DEFAULT_SCHEMA); List<String> createSqls = new ArrayList<String>(); try { conn = dataSource.getConnection(); meta = new DatabaseMetadata(conn, dialect); Iterator<Table> tableIter = cfg.getTableMappings(); Mapping mapping = cfg.buildMapping(); while (tableIter.hasNext()) { Table table = (Table) tableIter.next(); TableMetadata tableInfo = meta.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted() ); if (tableInfo == null) { createSqls.add(table.sqlCreateString(dialect, mapping, defaultCatalog, defaultSchema)); } } tableIter = cfg.getTableMappings(); while (tableIter.hasNext()) { Table table = (Table) tableIter.next(); Iterator<ForeignKey> subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { TableMetadata tableInfo = meta.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted() ); if (tableInfo == null) { String[] cols = new String[fk.getColumnSpan()]; String[] refcols = new String[fk.getColumnSpan()]; int i = 0; Iterator<Column> refiter = null; if (fk.isReferenceToPrimaryKey()) { refiter = fk.getReferencedTable().getPrimaryKey().getColumnIterator(); } else { refiter = fk.getReferencedColumns().iterator(); } Iterator<Column> columnIter = fk.getColumnIterator(); while (columnIter.hasNext()) { cols[i] = ((Column) columnIter.next()).getQuotedName(dialect); refcols[i] = ((Column) refiter.next()).getQuotedName(dialect); i++; } String result = dialect .getAddForeignKeyConstraintString( fk.getName(), cols, fk.getReferencedTable().getQualifiedName(dialect, defaultCatalog, defaultSchema), refcols, fk.isReferenceToPrimaryKey()); StringBuffer createFK = new StringBuffer("alter table ") .append(table.getQualifiedName(dialect, defaultCatalog, defaultSchema)) .append(dialect.supportsCascadeDelete() ? result + " on delete cascade" : result); createSqls.add(createFK.toString()); } } } } this.executeSqls(conn, createSqls); } catch (DDLException e) { throw e; } catch (Exception e) { throw new PlutoException(e); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { log.error("", e); } } } }
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 {/*w ww . j av a 2 s . c o m*/ 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.github.shyiko.rook.target.hibernate4.cache.SynchronizationContext.java
License:Apache License
private Map<String, Integer> extractColumnMapping(Table table) throws SQLException { Map<String, Integer> result = new HashMap<String, Integer>(); Connection connection = null; ConnectionProvider connectionProvider = getSessionFactory().getServiceRegistry() .getService(ConnectionProvider.class); try {// w w w .j a va2 s. c o m connection = connectionProvider.getConnection(); DatabaseMetaData meta = connection.getMetaData(); ResultSet rs = meta.getColumns(table.getCatalog(), table.getSchema(), table.getName(), "%"); try { int index = 0; while (rs.next()) { String column = rs.getString("COLUMN_NAME"); if (column != null) { result.put(column, index++); } } } finally { rs.close(); } } finally { connectionProvider.closeConnection(connection); } return result; }
From source file:com.zutubi.pulse.master.hibernate.MutableConfiguration.java
License:Apache License
private String getTableKey(Table table) { return table.getSubselect() == null ? Table.qualify(table.getCatalog(), table.getSchema(), table.getName()) : table.getSubselect();// w w w . ja v a 2 s . co m }
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());/*from w ww. j a v 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:corner.migration.services.impl.adapter.AbstractConnectionAdapter.java
License:Apache License
@Override public TableMetadata fetchTableInfo(DatabaseMetadata meta, Table table, String defaultCatalog, String defaultSchema) {//from ww w . j a v a 2 s .c om TableMetadata tableInfo = meta.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted()); return tableInfo; }
From source file:org.beangle.commons.orm.hibernate.internal.SchemaValidator.java
License:Open Source License
public String validateSchema(Configuration config, Dialect dialect, DatabaseMetadata databaseMetadata) { String defaultCatalog = sessionFactoryBean.getHibernateProperties() .getProperty(Environment.DEFAULT_CATALOG); String defaultSchema = sessionFactoryBean.getHibernateProperties().getProperty(Environment.DEFAULT_SCHEMA); Mapping mapping = config.buildMapping(); Iterator<?> iter = config.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { TableMetadata tableInfo = databaseMetadata.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted()); if (tableInfo == null) { reporter.append("Missing table: " + table.getName() + "\n"); } else { validateColumns(table, dialect, mapping, tableInfo); }// ww w .ja v a 2s.c om } } iter = iterateGenerators(config, dialect); while (iter.hasNext()) { PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next(); Object key = generator.generatorKey(); if (!databaseMetadata.isSequence(key) && !databaseMetadata.isTable(key)) { throw new HibernateException("Missing sequence or table: " + key); } } return null; }
From source file:org.jboss.tools.hibernate.ui.diagram.editors.model.OrmShape.java
License:Open Source License
@Override public Object getPropertyValue(Object propertyId) { Object res = null;//from ww w .ja v a 2s . c o m RootClass rootClass = null; Table table = null; Object ormElement = getOrmElement(); if (ormElement instanceof RootClass) { rootClass = (RootClass) ormElement; } else if (ormElement instanceof Subclass) { //rootClass = ((Subclass)ormElement).getRootClass(); } else if (ormElement instanceof Table) { table = (Table) getOrmElement(); } if (rootClass != null) { if (ENTITY_isAbstract.equals(propertyId)) { if (rootClass.isAbstract() != null) { res = rootClass.isAbstract().toString(); } } else if (ENTITY_isCustomDeleteCallable.equals(propertyId)) { res = Boolean.valueOf(rootClass.isCustomDeleteCallable()).toString(); } else if (ENTITY_isCustomInsertCallable.equals(propertyId)) { res = Boolean.valueOf(rootClass.isCustomInsertCallable()).toString(); } else if (ENTITY_isCustomUpdateCallable.equals(propertyId)) { res = Boolean.valueOf(rootClass.isCustomUpdateCallable()).toString(); } else if (ENTITY_isDiscriminatorInsertable.equals(propertyId)) { res = Boolean.valueOf(rootClass.isDiscriminatorInsertable()).toString(); } else if (ENTITY_isDiscriminatorValueNotNull.equals(propertyId)) { res = Boolean.valueOf(rootClass.isDiscriminatorValueNotNull()).toString(); } else if (ENTITY_isDiscriminatorValueNull.equals(propertyId)) { res = Boolean.valueOf(rootClass.isDiscriminatorValueNull()).toString(); } else if (ENTITY_isExplicitPolymorphism.equals(propertyId)) { res = Boolean.valueOf(rootClass.isExplicitPolymorphism()).toString(); } else if (ENTITY_isForceDiscriminator.equals(propertyId)) { res = Boolean.valueOf(rootClass.isForceDiscriminator()).toString(); } else if (ENTITY_isInherited.equals(propertyId)) { res = Boolean.valueOf(rootClass.isInherited()).toString(); } else if (ENTITY_isJoinedSubclass.equals(propertyId)) { res = Boolean.valueOf(rootClass.isJoinedSubclass()).toString(); } else if (ENTITY_isLazy.equals(propertyId)) { res = Boolean.valueOf(rootClass.isLazy()).toString(); } else if (ENTITY_isLazyPropertiesCacheable.equals(propertyId)) { res = Boolean.valueOf(rootClass.isLazyPropertiesCacheable()).toString(); } else if (ENTITY_isMutable.equals(propertyId)) { res = Boolean.valueOf(rootClass.isMutable()).toString(); } else if (ENTITY_isPolymorphic.equals(propertyId)) { res = Boolean.valueOf(rootClass.isPolymorphic()).toString(); } else if (ENTITY_isVersioned.equals(propertyId)) { res = Boolean.valueOf(rootClass.isVersioned()).toString(); } else if (ENTITY_batchSize.equals(propertyId)) { res = Integer.valueOf(rootClass.getBatchSize()).toString(); } else if (ENTITY_cacheConcurrencyStrategy.equals(propertyId)) { res = rootClass.getCacheConcurrencyStrategy(); } else if (ENTITY_className.equals(propertyId)) { res = rootClass.getClassName(); } else if (ENTITY_customSQLDelete.equals(propertyId)) { res = rootClass.getCustomSQLDelete(); } else if (ENTITY_customSQLInsert.equals(propertyId)) { res = rootClass.getCustomSQLInsert(); } else if (ENTITY_customSQLUpdate.equals(propertyId)) { res = rootClass.getCustomSQLUpdate(); } else if (ENTITY_discriminatorValue.equals(propertyId)) { res = rootClass.getDiscriminatorValue(); } else if (ENTITY_entityName.equals(propertyId)) { res = rootClass.getEntityName(); } else if (ENTITY_loaderName.equals(propertyId)) { res = rootClass.getLoaderName(); } else if (ENTITY_nodeName.equals(propertyId)) { res = rootClass.getNodeName(); } else if (ENTITY_optimisticLockMode.equals(propertyId)) { res = Integer.valueOf(rootClass.getOptimisticLockMode()).toString(); } else if (ENTITY_table.equals(propertyId)) { if (rootClass.getTable() != null) { res = rootClass.getTable().getName(); } } else if (ENTITY_temporaryIdTableDDL.equals(propertyId)) { res = rootClass.getTemporaryIdTableDDL(); } else if (ENTITY_temporaryIdTableName.equals(propertyId)) { res = rootClass.getTemporaryIdTableName(); } else if (ENTITY_where.equals(propertyId)) { res = rootClass.getWhere(); } } if (table != null) { if (TABLE_catalog.equals(propertyId)) { res = table.getCatalog(); } else if (TABLE_comment.equals(propertyId)) { res = table.getComment(); } else if (TABLE_name.equals(propertyId)) { res = table.getName(); } else if (TABLE_primaryKey.equals(propertyId)) { if (table.getPrimaryKey() != null) { res = table.getPrimaryKey().getName(); } } else if (TABLE_rowId.equals(propertyId)) { res = table.getRowId(); } else if (TABLE_schema.equals(propertyId)) { res = table.getSchema(); } else if (TABLE_subselect.equals(propertyId)) { res = table.getSubselect(); } else if (TABLE_hasDenormalizedTables.equals(propertyId)) { res = Boolean.valueOf(table.hasDenormalizedTables()).toString(); } else if (TABLE_isAbstract.equals(propertyId)) { res = Boolean.valueOf(table.isAbstract()).toString(); } else if (TABLE_isAbstractUnionTable.equals(propertyId)) { res = Boolean.valueOf(table.isAbstractUnionTable()).toString(); } else if (TABLE_isPhysicalTable.equals(propertyId)) { res = Boolean.valueOf(table.isPhysicalTable()).toString(); } } if (res == null) { res = super.getPropertyValue(propertyId); } return toEmptyStr(res); }