001package io.ebeaninternal.dbmigration.ddlgeneration.platform;
002
003import io.ebean.config.dbplatform.DatabasePlatform;
004import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
005
006import java.io.IOException;
007
008/**
009 * Postgres specific DDL.
010 */
011public class PostgresDdl extends PlatformDdl {
012
013  private static final String dropIndexConcurrentlyIfExists = "drop index concurrently if exists ";
014
015  public PostgresDdl(DatabasePlatform platform) {
016    super(platform);
017    this.historyDdl = new PostgresHistoryDdl();
018    this.dropTableCascade = " cascade";
019    this.columnSetType = "type ";
020    this.alterTableIfExists = "if exists ";
021    this.createIndexIfNotExists = "if not exists ";
022    this.columnSetNull = "drop not null";
023    this.addForeignKeySkipCheck = " not valid";
024    this.indexConcurrent = "concurrently ";
025  }
026
027  public String setLockTimeout(int lockTimeoutSeconds) {
028    return "set lock_timeout = " + (lockTimeoutSeconds * 1000);
029  }
030
031  @Override
032  public boolean suppressPrimaryKeyOnPartition() {
033    return true;
034  }
035
036  @Override
037  protected String convertArrayType(String logicalArrayType) {
038    return NativeDbArray.logicalToNative(logicalArrayType);
039  }
040
041  @Override
042  public void addTablePartition(DdlBuffer apply, String partitionMode, String partitionColumn) throws IOException {
043    apply.append(" partition by range (").append(partitionColumn).append(")");
044  }
045
046  @Override
047  public String dropIndex(String indexName, String tableName, boolean concurrent) {
048    return (concurrent ? dropIndexConcurrentlyIfExists : dropIndexIfExists) + maxConstraintName(indexName);
049  }
050
051  /**
052   * Modify and return the column definition for autoincrement or identity definition.
053   */
054  @Override
055  public String asIdentityColumn(String columnDefn, DdlIdentity identity) {
056    return asIdentityStandardOptions(columnDefn, identity);
057  }
058
059  @Override
060  public String alterColumnType(String tableName, String columnName, String type) {
061    return super.alterColumnType(tableName, columnName, type) + " using " + columnName + "::" + convert(type);
062  }
063}