Example #1
0
    @Override
    final Field<T> getFunction0(Configuration configuration) {
      SQLDialect family = configuration.family();

      switch (family) {
        case POSTGRES:
          {
            String field = method + "('" + getQualifiedName(configuration) + "')";
            return field(field, getDataType());
          }

        case H2:
          {
            String field = method + "(" + getQualifiedName(configuration, true) + ")";
            return field(field, getDataType());
          }

        case FIREBIRD:
        case DERBY:
        case HSQLDB:
          {
            if ("nextval".equals(method)) {
              String field = "next value for " + getQualifiedName(configuration);
              return field(field, getDataType());
            } else if (family == FIREBIRD) {
              return field("gen_id(" + getQualifiedName(configuration) + ", 0)", getDataType());
            } else {
              throw new SQLDialectNotSupportedException(
                  "The sequence's current value functionality is not supported for the "
                      + family
                      + " dialect.");
            }
          }

        case CUBRID:
          {
            String field = getQualifiedName(configuration) + ".";

            if ("nextval".equals(method)) {
              field += "next_value";
            } else {
              field += "current_value";
            }

            return field(field, getDataType());
          }

          // Default is needed for hashCode() and toString()
        default:
          {
            String field = getQualifiedName(configuration) + "." + method;
            return field(field, getDataType());
          }
      }
    }
Example #2
0
  @Override
  final Field<Integer> getFunction0(Configuration configuration) {
    switch (configuration.family()) {
      case SQLITE:
        switch (datePart) {
          case YEAR:
            return field("{strftime}('%Y', {0})", SQLDataType.INTEGER, field);
          case MONTH:
            return field("{strftime}('%m', {0})", SQLDataType.INTEGER, field);
          case DAY:
            return field("{strftime}('%d', {0})", SQLDataType.INTEGER, field);
          case HOUR:
            return field("{strftime}('%H', {0})", SQLDataType.INTEGER, field);
          case MINUTE:
            return field("{strftime}('%M', {0})", SQLDataType.INTEGER, field);
          case SECOND:
            return field("{strftime}('%S', {0})", SQLDataType.INTEGER, field);
          default:
            throw new SQLDialectNotSupportedException("DatePart not supported: " + datePart);
        }

      case DERBY:
        switch (datePart) {
          case YEAR:
            return function("year", SQLDataType.INTEGER, field);
          case MONTH:
            return function("month", SQLDataType.INTEGER, field);
          case DAY:
            return function("day", SQLDataType.INTEGER, field);
          case HOUR:
            return function("hour", SQLDataType.INTEGER, field);
          case MINUTE:
            return function("minute", SQLDataType.INTEGER, field);
          case SECOND:
            return function("second", SQLDataType.INTEGER, field);
          default:
            throw new SQLDialectNotSupportedException("DatePart not supported: " + datePart);
        }

      case MARIADB:
      case MYSQL:
      case POSTGRES:
      case HSQLDB:
      case H2:

        // A default implementation is necessary for hashCode() and toString()
      default:
        return field("{extract}({" + datePart.toSQL() + " from} {0})", SQLDataType.INTEGER, field);
    }
  }
Example #3
0
  private final QueryPart delegate(Configuration configuration) {
    switch (configuration.family()) {
      case CUBRID:

        // There is a bug in CUBRID preventing reuse of "level" in the
        // predicate http://jira.cubrid.org/browse/ENGINE-119
        Field<Integer> level = from.add(level()).sub(one());
        return table(
            "({select} {0} {as} {1} {from} {2} {connect by} {level} <= {3})",
            level, name("generate_series"), new Dual(), to.add(one()).sub(from));

      case POSTGRES:
      default:
        return table("{generate_series}({0}, {1})", from, to);
    }
  }
Example #4
0
  @Override
  final Field<T> getFunction0(Configuration configuration) {
    switch (configuration.family()) {
      case H2:
      case HSQLDB:
        return field("{nvl}({0}, {1})", getDataType(), arg1, arg2);

      case DERBY:
      case POSTGRES:
        return field("{coalesce}({0}, {1})", getDataType(), arg1, arg2);

      case MARIADB:
      case MYSQL:
      case SQLITE:
        return field("{ifnull}({0}, {1})", getDataType(), arg1, arg2);

      default:
        return DSL.when(arg1.isNotNull(), arg1).otherwise(arg2);
    }
  }
Example #5
0
  @SuppressWarnings("unchecked")
  @Override
  final Field<T> getFunction0(Configuration configuration) {

    // In any dialect, a single argument is always the least
    if (getArguments().length == 1) {
      return (Field<T>) getArguments()[0];
    }

    switch (configuration.family()) {
        // This implementation has O(2^n) complexity. Better implementations
        // are very welcome

      case DERBY:
        {
          Field<T> first = (Field<T>) getArguments()[0];
          Field<T> other = (Field<T>) getArguments()[1];

          if (getArguments().length > 2) {
            Field<?>[] remaining = new Field<?>[getArguments().length - 2];
            System.arraycopy(getArguments(), 2, remaining, 0, remaining.length);

            return DSL.when(first.lessThan(other), DSL.least(first, remaining))
                .otherwise(DSL.least(other, remaining));
          } else {
            return DSL.when(first.lessThan(other), first).otherwise(other);
          }
        }

      case FIREBIRD:
        return function("minvalue", getDataType(), getArguments());

      case SQLITE:
        return function("min", getDataType(), getArguments());

      default:
        return function("least", getDataType(), getArguments());
    }
  }
Example #6
0
  @Override
  final Field<String> getFunction0(Configuration configuration) {
    switch (configuration.family()) {

        // This beautiful expression was contributed by "Ludo", here:
        // http://stackoverflow.com/questions/6576343/how-to-simulate-lpad-rpad-with-sqlite
      case SQLITE:
        {
          return DSL.field(
              "{0} || substr("
                  + "replace("
                  + "replace("
                  + "substr("
                  + "quote("
                  + "zeroblob((({1} - length({0}) - 1 + length({2})) / length({2}) + 1) / 2)"
                  + "), 3"
                  + "), '\''', ''"
                  + "), '0', {2}"
                  + "), 1, ({1} - length({0}))"
                  + ")",
              String.class, field, length, character);
        }

        // According to the Firebird documentation, LPAD outcomes should be
        // cast to truncate large results...
      case FIREBIRD:
        {
          return field(
              "cast(rpad({0}, {1}, {2}) as varchar(4000))",
              SQLDataType.VARCHAR, field, length, character);
        }

      default:
        {
          return function("rpad", SQLDataType.VARCHAR, field, length, character);
        }
    }
  }
Example #7
0
 @Override
 public SQLDialect family() {
   return delegate.family();
 }