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      case Types.CLOB:
047      case Types.CHAR:
048      case Types.LONGVARCHAR:
049        return varcharEncryptFunction;
050
051      case Types.DATE:
052        return dateEncryptFunction;
053
054      case Types.TIMESTAMP:
055        return timestampEncryptFunction;
056
057      default:
058        return null;
059    }
060  }
061
062  /**
063   * Return the DB stored type for encrypted properties.
064   */
065  @Override
066  public int getEncryptDbType() {
067    return Types.VARBINARY;
068  }
069
070  /**
071   * Generally encrypt function binding the data before the key (except h2).
072   */
073  @Override
074  public boolean isBindEncryptDataFirst() {
075    return true;
076  }
077}