Exemplo n.º 1
0
  @Override
  public void printSQLSelectStatement(
      DatabaseCall call, ExpressionSQLPrinter printer, SQLSelectStatement statement) {
    int max = 0;
    int firstRow = 0;
    ReadQuery query = statement.getQuery();
    if (query != null) {
      max = query.getMaxRows();
      firstRow = query.getFirstResult();
    }

    if (max <= 0 && firstRow <= 0) {
      // neither max nor firstRow is set
      super.printSQLSelectStatement(call, printer, statement);
      return;
    }
    if (max <= 0) {
      // if max row is not set use MAX_VALUE instead
      // this is done, because NewDB does not allow
      // OFFSET without LIMIT, and scrollable cursor is not supported
      // in order to support firstRows without MaxRows also MaxRow has to be set
      // this limits also the size of the result set in this case to Integer.MAX_VALUE rows
      query.setMaxRows(Integer.MAX_VALUE);
    }
    statement.setUseUniqueFieldAliases(true);
    call.setFields(statement.printSQL(printer));
    printer.printString(" LIMIT ");
    printer.printParameter(DatabaseCall.MAXROW_FIELD);
    printer.printString(" OFFSET ");
    printer.printParameter(DatabaseCall.FIRSTRESULT_FIELD);
    call.setIgnoreFirstRowSetting(true);
    call.setIgnoreMaxResultsSetting(true);
  }
  /**
   * INTERNAL: Get the ref for the object. This is required for use by Refs, there might be a better
   * way to do it when objID are supported. (i.e. getting it from the object or identity map).
   */
  public Ref getRef(Object object, AbstractSession session) {
    SQLSelectStatement statement = new SQLSelectStatement();
    statement.addTable(getTables().firstElement()); // Assumed only one for obj-rel descriptors.
    statement
        .getFields()
        .addElement(new org.eclipse.persistence.expressions.ExpressionBuilder().ref());
    statement.setWhereClause(
        getObjectBuilder().buildPrimaryKeyExpressionFromObject(object, session));
    statement.setRequiresAliases(true);
    statement.normalize(session, this);

    ValueReadQuery valueQuery = new ValueReadQuery();
    valueQuery.setSQLStatement(statement);
    valueQuery.prepareCall(session, new DatabaseRecord());
    // Must return unwrapped Ref on WLS.
    valueQuery.getCall().setIsNativeConnectionRequired(true);

    Ref ref = (Ref) session.executeQuery(valueQuery);

    return ref;
  }