001package io.ebean.config.dbplatform;
002
003/**
004 * Database view based implementation of DbHistorySupport.
005 * <p>
006 * These implementations have explicit history tables, a view to union the
007 * base table and history table and triggers to maintain the history.
008 * </p>
009 */
010public abstract class DbViewHistorySupport implements DbHistorySupport {
011
012  @Override
013  public boolean isStandardsBased() {
014    return false;
015  }
016
017  /**
018   * Returns the configured view suffix.
019   *
020   * @param asOfViewSuffix the configured view suffix (typically "_with_history").
021   */
022  @Override
023  public String getAsOfViewSuffix(String asOfViewSuffix) {
024    // just return the configured suffix
025    return asOfViewSuffix;
026  }
027
028  /**
029   * Returns the configured view suffix (same as getAsOfViewSuffix()).
030   *
031   * @param asOfViewSuffix the configured view suffix (typically "_with_history").
032   */
033  @Override
034  public String getVersionsBetweenSuffix(String asOfViewSuffix) {
035    // just return the configured asOfViewSuffix (using the view for versions between query)
036    return asOfViewSuffix;
037  }
038
039  /**
040   * Return 2 if we have effective start and effective end as 2 columns.
041   * Note that for postgres we can use a single range type so that returns 1.
042   */
043  @Override
044  public int getBindCount() {
045    return 2;
046  }
047
048  /**
049   * Return the 'as of' predicate clause appended to the end of the normal query predicates.
050   */
051  @Override
052  public String getAsOfPredicate(String asOfTableAlias, String asOfSysPeriod) {
053
054    // (sys_period_start < ? and (sys_period_end is null or sys_period_end > ?));
055    return "(" + asOfTableAlias + "." + asOfSysPeriod + "_start" + " <= ? and (" + asOfTableAlias + "." + asOfSysPeriod + "_end" + " is null or " + asOfTableAlias + "." + asOfSysPeriod + "_end" + " > ?))";
056  }
057
058  /**
059   * Return the lower bound column prepended with the table alias.
060   *
061   * @param tableAlias the table alias
062   * @param sysPeriod  the name of the sys_period column
063   */
064  @Override
065  public String getSysPeriodLower(String tableAlias, String sysPeriod) {
066    return tableAlias + "." + sysPeriod + "_start";
067  }
068
069  /**
070   * Return the upper bound column prepended with the table alias.
071   *
072   * @param tableAlias the table alias
073   * @param sysPeriod  the name of the sys_period column
074   */
075  @Override
076  public String getSysPeriodUpper(String tableAlias, String sysPeriod) {
077    return tableAlias + "." + sysPeriod + "_end";
078  }
079}