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

io.ebeaninternal.server.query.DefaultRelationalQueryEngine Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebeaninternal.server.query;

import io.ebean.RowConsumer;
import io.ebean.RowMapper;
import io.ebean.SqlRow;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.ScalarType;
import io.ebean.meta.MetricVisitor;
import io.ebean.metric.MetricFactory;
import io.ebean.metric.TimedMetricMap;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.RelationalQueryEngine;
import io.ebeaninternal.server.core.RelationalQueryRequest;
import io.ebeaninternal.server.core.RowReader;
import io.ebeaninternal.server.persist.Binder;

import jakarta.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Perform native sql fetches.
 */
public final class DefaultRelationalQueryEngine implements RelationalQueryEngine {

  private final Binder binder;
  private final String dbTrueValue;
  private final boolean binaryOptimizedUUID;
  private final TimedMetricMap timedMetricMap;

  public DefaultRelationalQueryEngine(Binder binder, String dbTrueValue, boolean binaryOptimizedUUID) {
    this.binder = binder;
    this.dbTrueValue = dbTrueValue == null ? "true" : dbTrueValue;
    this.binaryOptimizedUUID = binaryOptimizedUUID;
    this.timedMetricMap = MetricFactory.get().createTimedMetricMap("sql.query.");
  }

  @Override
  public void collect(String label, long exeMicros) {
    timedMetricMap.add(label, exeMicros);
  }

  @Override
  public void visitMetrics(MetricVisitor visitor) {
    timedMetricMap.visit(visitor);
  }

  @Override
  public SqlRow createSqlRow(int estimateCapacity) {
    return new DefaultSqlRow(estimateCapacity, 0.75f, dbTrueValue, binaryOptimizedUUID);
  }

  private String errMsg(String msg, String sql) {
    return "Query threw SQLException:" + msg + " Query was:" + sql;
  }

  @Override
  public void findEach(RelationalQueryRequest request, RowConsumer consumer) {
    try {
      request.executeSql(binder, SpiQuery.Type.ITERATE);
      request.mapEach(consumer);
      request.logSummary();

    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }

  @Override
  public  void findEach(RelationalQueryRequest request, RowReader reader, Predicate consumer) {
    try {
      request.executeSql(binder, SpiQuery.Type.ITERATE);
      while (request.next()) {
        if (!consumer.test(reader.read())) {
          break;
        }
      }
      request.logSummary();

    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }

  @Override
  public  T findOne(RelationalQueryRequest request, RowMapper mapper) {
    try {
      request.executeSql(binder, SpiQuery.Type.BEAN);
      T value = request.mapOne(mapper);
      request.logSummary();
      return value;

    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }

  @Override
  public  List findList(RelationalQueryRequest request, RowReader reader) {
    try {
      request.executeSql(binder, SpiQuery.Type.LIST);
      List rows = new ArrayList<>();
      while (request.next()) {
        rows.add(reader.read());
      }
      request.logSummary();
      return rows;
    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public  T findSingleAttribute(RelationalQueryRequest request, Class cls) {
    ScalarType scalarType = (ScalarType) binder.getScalarType(cls);
    try {
      request.executeSql(binder, SpiQuery.Type.ATTRIBUTE);
      final DataReader dataReader = binder.createDataReader(request.resultSet());
      T value = null;
      if (dataReader.next()) {
        value = scalarType.read(dataReader);
      }
      request.logSummary();
      return value;

    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public  List findSingleAttributeList(RelationalQueryRequest request, Class cls) {
    ScalarType scalarType = (ScalarType) binder.getScalarType(cls);
    try {
      request.executeSql(binder, SpiQuery.Type.ATTRIBUTE);
      final DataReader dataReader = binder.createDataReader(request.resultSet());
      List rows = new ArrayList<>();
      while (dataReader.next()) {
        rows.add(scalarType.read(dataReader));
      }
      request.logSummary();
      return rows;

    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public  void findSingleAttributeEach(RelationalQueryRequest request, Class cls, Consumer consumer) {
    ScalarType scalarType = (ScalarType) binder.getScalarType(cls);
    try {
      request.executeSql(binder, SpiQuery.Type.ATTRIBUTE);
      final DataReader dataReader = binder.createDataReader(request.resultSet());
      while (dataReader.next()) {
        consumer.accept(scalarType.read(dataReader));
      }
      request.logSummary();

    } catch (Exception e) {
      throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);

    } finally {
      request.close();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy