001package io.ebean.config.dbplatform;
002
003import java.util.regex.Matcher;
004import java.util.regex.Pattern;
005
006/**
007 * Defines the identity/sequence behaviour for the database.
008 */
009public class DbIdentity {
010
011  private static final String TABLE_PLACEHOLDER = "{table}";
012
013  private static final Pattern TABLE_REPLACE = Pattern.compile(TABLE_PLACEHOLDER, Pattern.LITERAL);
014
015  /**
016   * Set if this DB supports sequences. Note some DB's support both Sequences
017   * and Identity.
018   */
019  private boolean supportsSequence;
020  private boolean supportsIdentity;
021
022  private boolean supportsGetGeneratedKeys;
023
024  private String selectLastInsertedIdTemplate;
025
026  private IdType idType = IdType.IDENTITY;
027
028  public DbIdentity() {
029  }
030
031  /**
032   * Return true if GetGeneratedKeys is supported.
033   * <p>
034   * GetGeneratedKeys required to support JDBC batching transparently.
035   * </p>
036   */
037  public boolean isSupportsGetGeneratedKeys() {
038    return supportsGetGeneratedKeys;
039  }
040
041  /**
042   * Set if GetGeneratedKeys is supported.
043   */
044  public void setSupportsGetGeneratedKeys(boolean supportsGetGeneratedKeys) {
045    this.supportsGetGeneratedKeys = supportsGetGeneratedKeys;
046  }
047
048  /**
049   * Return the SQL query to find the SelectLastInsertedId.
050   * <p>
051   * This should only be set on databases that don't support GetGeneratedKeys.
052   * </p>
053   */
054  public String getSelectLastInsertedId(String table) {
055    if (selectLastInsertedIdTemplate == null) {
056      return null;
057    }
058    if (!selectLastInsertedIdTemplate.contains(TABLE_PLACEHOLDER)) {
059      return selectLastInsertedIdTemplate;
060    }
061    return TABLE_REPLACE.matcher(selectLastInsertedIdTemplate).replaceAll(Matcher.quoteReplacement(table));
062  }
063
064  /**
065   * Set the template used to build the SQL query to return the LastInsertedId.
066   * <p>
067   * The template can contain "{table}" where the table name should be include
068   * in the sql query.
069   * </p>
070   * <p>
071   * This should only be set on databases that don't support GetGeneratedKeys.
072   * </p>
073   */
074  public void setSelectLastInsertedIdTemplate(String selectLastInsertedIdTemplate) {
075    this.selectLastInsertedIdTemplate = selectLastInsertedIdTemplate;
076  }
077
078  /**
079   * Return true if the database supports sequences.
080   */
081  public boolean isSupportsSequence() {
082    return supportsSequence;
083  }
084
085  /**
086   * Set to true if the database supports sequences. Generally this also means
087   * you want to set the default IdType to sequence (some DB's support both
088   * sequences and identity).
089   */
090  public void setSupportsSequence(boolean supportsSequence) {
091    this.supportsSequence = supportsSequence;
092  }
093
094  /**
095   * Return true if this DB platform supports identity (autoincrement).
096   */
097  public boolean isSupportsIdentity() {
098    return supportsIdentity;
099  }
100
101  /**
102   * Set to true if this DB platform supports identity (autoincrement).
103   */
104  public void setSupportsIdentity(boolean supportsIdentity) {
105    this.supportsIdentity = supportsIdentity;
106  }
107
108  /**
109   * Return the default ID generation type that should be used. This should be
110   * either SEQUENCE or IDENTITY (aka Autoincrement).
111   * <p>
112   * Note: Id properties of type UUID automatically get a UUID generator
113   * assigned to them.
114   * </p>
115   */
116  public IdType getIdType() {
117    return idType;
118  }
119
120  /**
121   * Set the default ID generation type that should be used.
122   */
123  public void setIdType(IdType idType) {
124    this.idType = idType;
125  }
126
127}