All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.sql.AbstractSimpleSelectQuery Maven / Gradle / Ivy

/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.sql;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import br.com.objectos.db.Dialect;
import br.com.objectos.db.ParameterBinder;
import br.com.objectos.db.Result;
import br.com.objectos.db.Results;
import br.com.objectos.db.SqlStatement;
import br.com.objectos.db.Transaction;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class AbstractSimpleSelectQuery
    extends AbstractHasSelectNode
    implements SimpleSelectQuery {

  public AbstractSimpleSelectQuery(Dialect dialect, SelectNode node) {
    super(dialect, node);
  }

  @Override
  public final Optional findFirst(Transaction trx) {
    Result result = executeQuery(trx);
    ResultMapper mapper = node().mapper();
    return result.next()
        ? Optional.of(mapper.map(result))
        : Optional.empty();
  }

  @Override
  public final List list(Transaction trx) {
    return stream(trx).collect(Collectors.toList());
  }

  @Override
  public final Stream stream(Transaction trx) {
    Result result = executeQuery(trx);
    ResultMapper mapper = node().mapper();
    return Results.stream(result).map(rs -> mapper.map(rs));
  }

  Result executeQuery(Transaction trx) {
    SqlStatement stmt = prepare(trx);

    ParameterBinder binder = stmt.parameterBinder();
    node().bind(binder);

    return stmt.executeQuery();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy