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