<commit_before>package froop.db.jpa;

import froop.db.jpa.entity.Sample;
import froop.db.jpa.entity.Sample_;
import froop.domain.SampleData;
import org.eclipse.persistence.config.PersistenceUnitProperties;

import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public class JpaSample implements SampleData {
  private static final String DB_URL = "jdbc:derby:data/derby/sample";
  private static final EntityManagerFactory FACTORY =
      Persistence.createEntityManagerFactory("jpa-sample", createDbSetting());

  private static Map<String, String> createDbSetting() {
    HashMap<String, String> settings = new HashMap<>();
    settings.put(PersistenceUnitProperties.JDBC_URL, DB_URL);
    return settings;
  }

  @Override
  public List<String> queryAll() {
    return execute(manager -> {
//      TypedQuery<Sample> query = manager.createQuery(
//          "FROM Sample s ORDER BY s.name", Sample.class);
      CriteriaBuilder builder = manager.getCriteriaBuilder();
      CriteriaQuery<Sample> cq = builder.createQuery(Sample.class);
      Root<Sample> from = cq.from(Sample.class);
      CriteriaQuery<Sample> select = cq.select(from)
          .orderBy(builder.asc(from.get(Sample_.name)));
      TypedQuery<Sample> query = manager.createQuery(select);
      List<Sample> result = query.getResultList();
      return result.stream().map(Sample::getName).collect(Collectors.toList());
    });
  }

  @Override
  public Optional<String> queryNameById(long id) {
    return querySingle(manager -> {
      Sample entity = manager.find(Sample.class, id);
      return entity.getName();
    });
  }

  @Override
  public void update(long id, String name) {
    update(manager -> {
      Sample entity = manager.find(Sample.class, id);
      entity.setName(name);
      manager.persist(entity);
    });
  }

  private <R> Optional<R> querySingle(Function<EntityManager, R> function) {
    R res = execute(function);
    return Optional.ofNullable(res);<commit_msg>extract JpaSample#buildCriteriaQuery()<commit_after>package froop.db.jpa;

import froop.db.jpa.entity.Sample;
import froop.db.jpa.entity.Sample_;
import froop.domain.SampleData;
import org.eclipse.persistence.config.PersistenceUnitProperties;

import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public class JpaSample implements SampleData {
  private static final String DB_URL = "jdbc:derby:data/derby/sample";
  private static final EntityManagerFactory FACTORY =
      Persistence.createEntityManagerFactory("jpa-sample", createDbSetting());

  private static Map<String, String> createDbSetting() {
    HashMap<String, String> settings = new HashMap<>();
    settings.put(PersistenceUnitProperties.JDBC_URL, DB_URL);
    return settings;
  }

  @Override
  public List<String> queryAll() {
    return execute(manager -> {
//      TypedQuery<Sample> query = manager.createQuery(
//          "FROM Sample s ORDER BY s.name", Sample.class);
      TypedQuery<Sample> query = manager.createQuery(buildCriteriaQuery(manager.getCriteriaBuilder()));
      List<Sample> result = query.getResultList();
      return result.stream().map(Sample::getName).collect(Collectors.toList());
    });
  }

  private CriteriaQuery<Sample> buildCriteriaQuery(CriteriaBuilder builder) {
    CriteriaQuery<Sample> query = builder.createQuery(Sample.class);
    Root<Sample> root = query.from(Sample.class);
    return query.orderBy(builder.asc(root.get(Sample_.name)));
  }

  @Override
  public Optional<String> queryNameById(long id) {
    return querySingle(manager -> {
      Sample entity = manager.find(Sample.class, id);
      return entity.getName();
    });
  }

  @Override
  public void update(long id, String name) {
    update(manager -> {
      Sample entity = manager.find(Sample.class, id);
      entity.setName(name);
      manager.persist(entity);
    });
  }

  private <R> Optional<R> querySingle(Function<EntityManager, R> function) {
    R res = execute(function);
    return Optional.ofNullable(res);
{'fraction_non_alphanumeric': 0.11987860394537178, 'fraction_numerical': 0.0, 'mean_word_length': 4.3713620488940625, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3421404', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1330, 'n_words': 277}