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