001package com.avaje.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  /**
013   * Return false for view based implementations where we append extra 'as of' predicates to the end.
014   */
015  @Override
016  public boolean isBindWithFromClause() {
017    return false;
018  }
019
020  /**
021   * Returns the configured view suffix.
022   *
023   * @param asOfViewSuffix the configured view suffix (typically "_with_history").
024   */
025  @Override
026  public String getAsOfViewSuffix(String asOfViewSuffix) {
027    // just return the configured suffix
028    return asOfViewSuffix;
029  }
030
031  /**
032   * Returns the configured view suffix (same as getAsOfViewSuffix()).
033   *
034   * @param asOfViewSuffix the configured view suffix (typically "_with_history").
035   */
036  @Override
037  public String getVersionsBetweenSuffix(String asOfViewSuffix) {
038    // just return the configured asOfViewSuffix (using the view for versions between query)
039    return asOfViewSuffix;
040  }
041
042  /**
043   * Return 2 if we have effective start and effective end as 2 columns.
044   * Note that for postgres we can use a single range type so that returns 1.
045   */
046  @Override
047  public int getBindCount() {
048    return 2;
049  }
050
051  /**
052   * Return the 'as of' predicate clause appended to the end of the normal query predicates.
053   */
054  @Override
055  public String getAsOfPredicate(String asOfTableAlias, String asOfSysPeriod) {
056
057    // (sys_period_start < ? and (sys_period_end is null or sys_period_end > ?));
058    return "(" + asOfTableAlias + "." + asOfSysPeriod + "_start" + " <= ? and (" + asOfTableAlias + "." + asOfSysPeriod + "_end" + " is null or " + asOfTableAlias + "." + asOfSysPeriod + "_end" + " > ?))";
059  }
060
061  /**
062   * Return the lower bound column prepended with the table alias.
063   *
064   * @param tableAlias the table alias
065   * @param sysPeriod  the name of the sys_period column
066   */
067  @Override
068  public String getSysPeriodLower(String tableAlias, String sysPeriod) {
069    return tableAlias + "." + sysPeriod + "_start";
070  }
071
072  /**
073   * Return the upper bound column prepended with the table alias.
074   *
075   * @param tableAlias the table alias
076   * @param sysPeriod  the name of the sys_period column
077   */
078  @Override
079  public String getSysPeriodUpper(String tableAlias, String sysPeriod) {
080    return tableAlias + "." + sysPeriod + "_end";
081  }
082}