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 */
013public abstract class AbstractDbEncrypt implements DbEncrypt {
014
015  /**
016   * The encryption function for all String types (VARCHAR, CLOB, LONGVARCHAR,
017   * CHAR).
018   */
019  protected DbEncryptFunction varcharEncryptFunction;
020
021  /**
022   * The encryption function for all Date types (java.sql.Date, Joda Date
023   * types).
024   */
025  protected DbEncryptFunction dateEncryptFunction;
026
027  /**
028   * The encryption function for all Timestamp types (java.sql.Timestamp,
029   * java.util.Date, java.util.Calendar, Joda DateTime types etc).
030   */
031  protected DbEncryptFunction timestampEncryptFunction;
032
033  /**
034   * Return the DB encryption function for the given JDBC type.
035   * <p>
036   * Null is returned if DB encryption of the type is not supported.
037   * </p>
038   */
039  @Override
040  public DbEncryptFunction getDbEncryptFunction(int jdbcType) {
041    switch (jdbcType) {
042      case Types.VARCHAR:
043      case Types.CLOB:
044      case Types.CHAR:
045      case Types.LONGVARCHAR:
046        return varcharEncryptFunction;
047      case Types.DATE:
048        return dateEncryptFunction;
049      case Types.TIMESTAMP:
050        return timestampEncryptFunction;
051      default:
052        return null;
053    }
054  }
055
056  /**
057   * Return the DB stored type for encrypted properties.
058   */
059  @Override
060  public int getEncryptDbType() {
061    return Types.VARBINARY;
062  }
063
064  /**
065   * Generally encrypt function binding the data before the key (except h2).
066   */
067  @Override
068  public boolean isBindEncryptDataFirst() {
069    return true;
070  }
071}