001package com.avaje.ebean.config.dbplatform;
002
003import com.avaje.ebean.BackgroundExecutor;
004import com.avaje.ebean.config.ServerConfig;
005import com.avaje.ebean.dbmigration.ddlgeneration.DdlHandler;
006import com.avaje.ebean.dbmigration.ddlgeneration.platform.PostgresDdl;
007
008import javax.sql.DataSource;
009import java.sql.Types;
010
011/**
012 * Postgres v9 specific platform.
013 * <p>
014 * Uses serial types and getGeneratedKeys.
015 * </p>
016 */
017public class PostgresPlatform extends DatabasePlatform {
018
019  public PostgresPlatform() {
020    super();
021    this.name = "postgres";
022
023    // OnQueryOnly.CLOSE as a performance optimisation on Postgres
024    this.onQueryOnly = OnQueryOnly.CLOSE;
025    this.likeClause = "like ? escape''";
026    this.selectCountWithAlias = true;
027    this.blobDbType = Types.LONGVARBINARY;
028    this.clobDbType = Types.VARCHAR;
029    this.nativeUuidType = true;
030
031    this.dbEncrypt = new PostgresDbEncrypt();
032    this.historySupport = new PostgresHistorySupport();
033    this.platformDdl = new PostgresDdl(this.dbTypeMap, this.dbIdentity);
034
035    // Use Identity and getGeneratedKeys
036    this.dbIdentity.setIdType(IdType.IDENTITY);
037    this.dbIdentity.setSupportsGetGeneratedKeys(true);
038    this.dbIdentity.setSupportsSequence(true);
039
040    //this.columnAliasPrefix = "as c";
041
042    this.openQuote = "\"";
043    this.closeQuote = "\"";
044
045    DbType dbTypeText = new DbType("text");
046    DbType dbBytea = new DbType("bytea", false);
047
048    dbTypeMap.put(DbType.HSTORE, new DbType("hstore"));
049    dbTypeMap.put(DbType.JSON, new DbType("json"));
050    dbTypeMap.put(DbType.JSONB, new DbType("jsonb"));
051
052    dbTypeMap.put(Types.INTEGER, new DbType("integer", false));
053    dbTypeMap.put(Types.DOUBLE, new DbType("float"));
054    dbTypeMap.put(Types.TINYINT, new DbType("smallint"));
055    dbTypeMap.put(Types.DECIMAL, new DbType("decimal", 38));
056
057    dbTypeMap.put(Types.BINARY, dbBytea);
058    dbTypeMap.put(Types.VARBINARY, dbBytea);
059
060    dbTypeMap.put(Types.BLOB, dbBytea);
061    dbTypeMap.put(Types.CLOB, dbTypeText);
062    dbTypeMap.put(Types.LONGVARBINARY, dbBytea);
063    dbTypeMap.put(Types.LONGVARCHAR, dbTypeText);
064
065  }
066
067  /**
068   * Return a DdlHandler instance for generating DDL for the specific platform.
069   */
070  public DdlHandler createDdlHandler(ServerConfig serverConfig) {
071    return this.platformDdl.createDdlHandler(serverConfig);
072  }
073
074  /**
075   * Create a Postgres specific sequence IdGenerator.
076   */
077  @Override
078  public IdGenerator createSequenceIdGenerator(BackgroundExecutor be, DataSource ds, String seqName, int batchSize) {
079
080    return new PostgresSequenceIdGenerator(be, ds, seqName, batchSize);
081  }
082
083  @Override
084  protected String withForUpdate(String sql) {
085    return sql + " for update";
086  }
087}