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 this.likeClause = "like ? escape''"; 023 this.selectCountWithAlias = true; 024 this.blobDbType = Types.LONGVARBINARY; 025 this.clobDbType = Types.VARCHAR; 026 this.nativeUuidType = true; 027 028 this.dbEncrypt = new PostgresDbEncrypt(); 029 this.historySupport = new PostgresHistorySupport(); 030 this.platformDdl = new PostgresDdl(this.dbTypeMap, this.dbIdentity); 031 032 // Use Identity and getGeneratedKeys 033 this.dbIdentity.setIdType(IdType.IDENTITY); 034 this.dbIdentity.setSupportsGetGeneratedKeys(true); 035 this.dbIdentity.setSupportsSequence(true); 036 037 //this.columnAliasPrefix = "as c"; 038 039 this.openQuote = "\""; 040 this.closeQuote = "\""; 041 042 DbType dbTypeText = new DbType("text"); 043 DbType dbBytea = new DbType("bytea", false); 044 045 dbTypeMap.put(DbType.HSTORE, new DbType("hstore")); 046 dbTypeMap.put(DbType.JSON, new DbType("json")); 047 dbTypeMap.put(DbType.JSONB, new DbType("jsonb")); 048 049 dbTypeMap.put(Types.INTEGER, new DbType("integer", false)); 050 dbTypeMap.put(Types.DOUBLE, new DbType("float")); 051 dbTypeMap.put(Types.TINYINT, new DbType("smallint")); 052 dbTypeMap.put(Types.DECIMAL, new DbType("decimal", 38)); 053 054 dbTypeMap.put(Types.BINARY, dbBytea); 055 dbTypeMap.put(Types.VARBINARY, dbBytea); 056 057 dbTypeMap.put(Types.BLOB, dbBytea); 058 dbTypeMap.put(Types.CLOB, dbTypeText); 059 dbTypeMap.put(Types.LONGVARBINARY, dbBytea); 060 dbTypeMap.put(Types.LONGVARCHAR, dbTypeText); 061 062 } 063 064 /** 065 * Return a DdlHandler instance for generating DDL for the specific platform. 066 */ 067 public DdlHandler createDdlHandler(ServerConfig serverConfig) { 068 return this.platformDdl.createDdlHandler(serverConfig); 069 } 070 071 /** 072 * Create a Postgres specific sequence IdGenerator. 073 */ 074 @Override 075 public IdGenerator createSequenceIdGenerator(BackgroundExecutor be, DataSource ds, String seqName, int batchSize) { 076 077 return new PostgresSequenceIdGenerator(be, ds, seqName, batchSize); 078 } 079 080 @Override 081 protected String withForUpdate(String sql) { 082 return sql + " for update"; 083 } 084}