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