001package io.ebean.config.dbplatform;
002
003import java.sql.Types;
004
005/**
006 * Base type for DB platform specific Encryption.
007 * <p>
008 * DB specific classes that extend this need to set their specific encryption
009 * functions for varchar, date and timestamp. If they are left null then that is
010 * treated as though that data type can not be encrypted in the DB and will
011 * instead use java client encryption.
012 * </p>
013 *
014 * @author rbygrave
015 */
016public abstract class AbstractDbEncrypt implements DbEncrypt {
017
018  /**
019   * The encryption function for all String types (VARCHAR, CLOB, LONGVARCHAR,
020   * CHAR).
021   */
022  protected DbEncryptFunction varcharEncryptFunction;
023
024  /**
025   * The encryption function for all Date types (java.sql.Date, Joda Date
026   * types).
027   */
028  protected DbEncryptFunction dateEncryptFunction;
029
030  /**
031   * The encryption function for all Timestamp types (java.sql.Timestamp,
032   * java.util.Date, java.util.Calendar, Joda DateTime types etc).
033   */
034  protected DbEncryptFunction timestampEncryptFunction;
035
036  /**
037   * Return the DB encryption function for the given JDBC type.
038   * <p>
039   * Null is returned if DB encryption of the type is not supported.
040   * </p>
041   */
042  @Override
043  public DbEncryptFunction getDbEncryptFunction(int jdbcType) {
044    switch (jdbcType) {
045      case Types.VARCHAR:
046        return varcharEncryptFunction;
047      case Types.CLOB:
048        return varcharEncryptFunction;
049      case Types.CHAR:
050        return varcharEncryptFunction;
051      case Types.LONGVARCHAR:
052        return varcharEncryptFunction;
053
054      case Types.DATE:
055        return dateEncryptFunction;
056
057      case Types.TIMESTAMP:
058        return timestampEncryptFunction;
059
060      default:
061        return null;
062    }
063  }
064
065  /**
066   * Return the DB stored type for encrypted properties.
067   */
068  @Override
069  public int getEncryptDbType() {
070    return Types.VARBINARY;
071  }
072
073  /**
074   * Generally encrypt function binding the data before the key (except h2).
075   */
076  @Override
077  public boolean isBindEncryptDataFirst() {
078    return true;
079  }
080}