001package com.avaje.ebean.config.dbplatform;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006/**
007 * DB Column default values mapping to database platform specific literals.
008 */
009public class DbDefaultValue {
010
011  /**
012   * The key for FALSE.
013   */
014  public static final String FALSE = "false";
015
016  /**
017   * The key for TRUE.
018   */
019  public static final String TRUE = "true";
020
021  /**
022   * The key for the NOW / current timestamp.
023   */
024  public static final String NOW = "now";
025
026  protected Map<String,String> map = new LinkedHashMap<String,String>();
027
028  /**
029   * Set the DB now function.
030   */
031  public void setNow(String dbFunction) {
032    put(NOW, dbFunction);
033  }
034
035  /**
036   * Set the DB false literal.
037   */
038  public void setFalse(String dbFalseLiteral) {
039    put(FALSE, dbFalseLiteral);
040  }
041
042  /**
043   * Set the DB true literal.
044   */
045  public void setTrue(String dbTrueLiteral) {
046    put(TRUE, dbTrueLiteral);
047  }
048
049  /**
050   * Add an translation entry.
051   */
052  public void put(String dbLiteral, String dbTranslated) {
053    map.put(dbLiteral, dbTranslated);
054  }
055
056  /**
057   * Convert the DB default literal to platform specific type or function.
058   * <p>
059   * This is intended for the DB column default clause in DDL.
060   * </p>
061   */
062  public String convert(String dbDefaultLiteral) {
063    if (dbDefaultLiteral == null) {
064      return null;
065    }
066    String val = map.get(dbDefaultLiteral);
067    return val != null ? val : dbDefaultLiteral;
068  }
069
070}