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.columnSetNull = "drop not null";
022    this.addForeignKeySkipCheck = " not valid";
023    this.indexConcurrent = "concurrently ";
024  }
025
026  public String setLockTimeout(int lockTimeoutSeconds) {
027    return "set lock_timeout = " + (lockTimeoutSeconds * 1000);
028  }
029
030  @Override
031  public boolean suppressPrimaryKeyOnPartition() {
032    return true;
033  }
034
035  @Override
036  protected String convertArrayType(String logicalArrayType) {
037    return NativeDbArray.logicalToNative(logicalArrayType);
038  }
039
040  @Override
041  public void addTablePartition(DdlBuffer apply, String partitionMode, String partitionColumn) throws IOException {
042    apply.append(" partition by range (").append(partitionColumn).append(")");
043  }
044
045  @Override
046  public String dropIndex(String indexName, String tableName, boolean concurrent) {
047    return (concurrent ? dropIndexConcurrentlyIfExists : dropIndexIfExists) + maxConstraintName(indexName);
048  }
049
050  /**
051   * Modify and return the column definition for autoincrement or identity definition.
052   */
053  @Override
054  public String asIdentityColumn(String columnDefn, DdlIdentity identity) {
055    return asIdentityStandardOptions(columnDefn, identity);
056  }
057
058  @Override
059  public String alterColumnType(String tableName, String columnName, String type) {
060    return super.alterColumnType(tableName, columnName, type) + " using " + columnName + "::" + convert(type);
061  }
062}