List of usage examples for org.hibernate.dialect Dialect supportsCommentOn
public boolean supportsCommentOn()
From source file:com.github.gekoh.yagen.ddl.CreateDDL.java
License:Apache License
public String updateCreateTable(Dialect dialect, StringBuffer buf, String tableName, Map columnMap) { LOG.info("modify DDL created by hibernate for table {}", tableName); boolean isOracle = isOracle(dialect); String nameLC = tableName.toLowerCase(); String entityClassName = getEntityClassName(nameLC); Set<String> columns = new LinkedHashSet<String>(columnMap.keySet()); if (!renderTable(nameLC)) { return "-- skipped creation statement for table '" + tableName + "' as the mapped entity was not chosen to be processed"; }// www. j a va 2 s. c o m TableConfig tableConfig = tblNameToConfig.get(nameLC); checkTableName(dialect, tableName); if (externalViews.contains(nameLC)) { return "-- skipped creation statement for table '" + tableName + "' since there will be a view in place"; } String sqlCreate = buf.toString(); buf = new StringBuffer(); Map<String, String> comments = getProfile().getComments() != null ? getProfile().getComments().get(nameLC) : null; String liveTableName = nameLC; Set<String> columnNames = columns; List<String> pkCols = getPkColumnNamesFrom(sqlCreate); if (comments != null && comments.size() < 1) { comments = null; } Auditable auditable = tableConfig.getTableAnnotationOfType(Auditable.class); if (auditable != null && auditable.createNonExistingColumns()) { sqlCreate = addAuditColumns(dialect, sqlCreate, columns); } sqlCreate = processCascadeNullable(dialect, buf, nameLC, sqlCreate, tableConfig.getColumnNamesIsCascadeNullable()); String i18nFK = tableConfig.getI18nBaseEntityFkCol(); if (i18nFK != null) { String baseEntityTableName = tableConfig.getI18nBaseEntityTblName(); String i18nTblName = getProfile().getNamingStrategy().tableName(getI18NDetailTableName(nameLC)); liveTableName = i18nTblName; columnNames = getI18NEntityColumns(columns); sqlCreate = getI18NDetailTableCreateString(dialect, sqlCreate, i18nTblName, i18nFK); addDropStatement(nameLC, getIfExistsDropStatement(dialect, "drop table " + i18nTblName, null)); pkCols = getPkColumnNamesFrom(sqlCreate); if (dialect.supportsCommentOn()) { buf.append(STATEMENT_SEPARATOR).append("comment on table ").append(i18nTblName) .append(" is 'Base table for I18N descriptions, for comments see view ").append(nameLC) .append("'\n"); } deferredDdl.append(STATEMENT_SEPARATOR).append(getI18NDetailViewCreateString(dialect, nameLC, baseEntityTableName, i18nTblName, i18nFK, columnNames)); if (comments != null && isOracle) { addComments(deferredDdl, nameLC, comments); comments = null; } writeI18NDetailViewTriggerCreateString(dialect, deferredDdl, nameLC, i18nTblName, i18nFK, columnNames); } if (comments != null && isOracle) { addComments(buf, nameLC, comments); } addAuditTrigger(dialect, buf, liveTableName, columns); IntervalPartitioning partitioning = tableConfig.getTableAnnotationOfType(IntervalPartitioning.class); TemporalEntity temporalEntity = getProfile().isNoHistory() ? null : tableConfig.getTableAnnotationOfType(TemporalEntity.class); if (temporalEntity != null) { try { String histTableName; if (entityClassName != null) { String hstEntityClassName = entityClassName + CreateEntities.HISTORY_ENTITY_SUFFIX; histTableName = getProfile().getNamingStrategy().classToTableName(hstEntityClassName); // this will throw an exception when the history entity class is not found TableConfig hstConfig = new TableConfig(this, Class.forName(hstEntityClassName), histTableName); hstConfig.setTableToBeRendered(false); tblNameToConfig.put(hstConfig.getTableName(), hstConfig); } else { // there is no entity for the live table, e.g. for ManyToMany relations histTableName = getProfile().getNamingStrategy().tableName(temporalEntity.historyTableName()); } Matcher tblMatcher = TBL_PATTERN_WO_PK.matcher(sqlCreate); if (!tblMatcher.find()) { throw new IllegalStateException("cannot find create table statement in sql: " + sqlCreate); } String histColNameLC = temporalEntity.historyTimestampColumnName().toLowerCase(); List<String> historyRelevantCols = getHistoryRelevantColumns(columnNames, temporalEntity.ignoreChangeOfColumns(), histColNameLC); if (StringUtils.isEmpty(histTableName)) { histTableName = tblMatcher.group(TBL_PATTERN_WO_PK_IDX_TBLNAME) + Constants._HST; } if (pkCols == null) { JoinTable joinTable = tableConfig.getTableAnnotationOfType(JoinTable.class); CollectionTable collectionTable = tableConfig.getTableAnnotationOfType(CollectionTable.class); javax.persistence.UniqueConstraint[] uniqueConstraints = joinTable != null ? joinTable.uniqueConstraints() : collectionTable != null ? collectionTable.uniqueConstraints() : null; if (uniqueConstraints == null || uniqueConstraints.length < 1) { throw new IllegalStateException("cannot create history for table " + liveTableName + " since this table has no unique or primary key"); } pkCols = Arrays.asList(uniqueConstraints[0].columnNames()); } buf.append(STATEMENT_SEPARATOR).append("-- adding history table due to annotation ") .append(temporalEntity.annotationType().getName()).append(" on entity of table ") .append(tableName).append("\n").append(getHistTableSqlCreateString(dialect, sqlCreate, histTableName, histColNameLC, columnNames, pkCols, partitioning)); if (isOracle) { buf.append(STATEMENT_SEPARATOR); buf.append("-- creating trigger for inserting history rows from table ").append(tableName) .append("\n").append(getOracleHistTriggerSql(dialect, liveTableName, histTableName, histColNameLC, columnNames, pkCols, historyRelevantCols)) .append("\n/"); } else if (isPostgreSql(dialect)) { buf.append(STATEMENT_SEPARATOR).append(getPostgreSQLHistTriggerFunction(dialect, liveTableName, histTableName, histColNameLC, columnNames, pkCols, historyRelevantCols)).append("\n/"); buf.append(STATEMENT_SEPARATOR).append("create trigger ").append(liveTableName).append("_htU\n") .append("after update on ").append(liveTableName).append("\n").append("for each row\n") .append("when ("); for (String historyRelevantCol : historyRelevantCols) { buf.append("new.").append(historyRelevantCol).append(" is distinct from old.") .append(historyRelevantCol).append(" or\n"); } buf.delete(buf.length() - 4, buf.length()); buf.append(")\nexecute procedure ").append(liveTableName).append("_htr_function()"); buf.append(STATEMENT_SEPARATOR).append("create trigger ").append(liveTableName).append("_htr\n") .append("after insert or delete on ").append(liveTableName).append("\n") .append("for each row\n").append("execute procedure ").append(liveTableName) .append("_htr_function()"); } else { buf.append(getHsqlDBHistTriggerSql(dialect, liveTableName, histTableName, histColNameLC, columnNames, pkCols, historyRelevantCols)); } if (!historyInitSet) { getProfile().addHeaderDdl(new DDLGenerator.AddTemplateDDLEntry( CreateDDL.class.getResource("/com/github/gekoh/yagen/ddl/InitHistory.ddl.sql"))); historyInitSet = true; } } catch (ClassNotFoundException e) { LOG.info( "not generating history table of live table {} since corresponding history entity class not found in classpath", nameLC); } } for (Sequence sequence : tableConfig.getSequences()) { String seqName = getProfile().getNamingStrategy().sequenceName(sequence.name()); buf.append(STATEMENT_SEPARATOR); if (objectNames.contains(seqName.toLowerCase())) { buf.append( "-- WARNING: duplicate definition of sequence or name already defined for another object!\n--"); } else { checkObjectName(dialect, seqName.toLowerCase()); } buf.append("create sequence ").append(seqName).append(" start with ").append(sequence.startWith()) .append(" increment by ").append(sequence.incrementBy()); if (sequence.cache() > 1) { buf.append(" cache ").append(sequence.cache()); } } if (supportsPartitioning(dialect) && partitioning != null) { sqlCreate = addPartitioning(buf, partitioning, nameLC, sqlCreate, columns, pkCols); } sqlCreate = addConstraintsAndNames(dialect, buf, sqlCreate, nameLC, tableConfig.getColumnNameToEnumCheckConstraints()); sqlCreate = addDefaultValues(sqlCreate, nameLC); addIndexes(buf, dialect, tableConfig); if (buf.length() == 0) { return sqlCreate; } getProfile().duplex(ObjectType.TABLE, tableName, sqlCreate); buf.insert(0, sqlCreate); buf.insert(0, STATEMENT_SEPARATOR); return buf.toString(); }