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