001package io.ebean.config.dbplatform.postgres;
002
003import io.ebean.config.dbplatform.AbstractDbEncrypt;
004import io.ebean.config.dbplatform.DbEncryptFunction;
005
006/**
007 * Postgres pgp_sym_encrypt pgp_sym_decrypt based encryption support.
008 */
009public class PostgresDbEncrypt extends AbstractDbEncrypt {
010
011  public PostgresDbEncrypt() {
012    this.varcharEncryptFunction = new PgVarcharFunction();
013    this.dateEncryptFunction = new PgDateFunction();
014  }
015
016  private static class PgVarcharFunction implements DbEncryptFunction {
017
018    @Override
019    public String getDecryptSql(String columnWithTableAlias) {
020      return "pgp_sym_decrypt(" + columnWithTableAlias + ",?)";
021    }
022
023    @Override
024    public String getEncryptBindSql() {
025      return "pgp_sym_encrypt(?,?)";
026    }
027  }
028
029  private static class PgDateFunction implements DbEncryptFunction {
030
031    @Override
032    public String getDecryptSql(String columnWithTableAlias) {
033      return "to_date(pgp_sym_decrypt(" + columnWithTableAlias + ",?),'YYYYMMDD')";
034    }
035
036    @Override
037    public String getEncryptBindSql() {
038      return "pgp_sym_encrypt(to_char(?::date,'YYYYMMDD'),?)";
039    }
040  }
041}