<commit_before>package com.google.tagpost.spanner;

import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.tagpost.Comment;
import com.google.tagpost.Tag;
import com.google.tagpost.Thread;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Singleton;

/** Data access and operations of Spanner */
@Singleton
public class SpannerService implements DataService {

  @Override
  public List<Thread> getAllThreadsByTag(Tag tag) {

    SpannerOptions spannerOptions = SpannerOptions.newBuilder().build();
    Spanner spanner = spannerOptions.getService();
    List<Thread> threadList = new ArrayList<>();

    try {
      DatabaseId db = DatabaseId.of("testing-bigtest", "tagpost", "test");
      DatabaseClient dbClient = spanner.getDatabaseClient(db);
      String query =
          "SELECT ThreadID, PrimaryTag FROM Thread WHERE PrimaryTag = '" + tag.getTagName() + "'";
      ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of(query));
      convertResultToThreadList(threadList, resultSet);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return threadList;
  }

  @Override
  public List<Comment> getAllCommentsByThreadId(long threadId) {

    SpannerOptions spannerOptions = SpannerOptions.newBuilder().build();
    Spanner spanner = spannerOptions.getService();
    List<Comment> commentList = new ArrayList<>();

    try {
      DatabaseId db = DatabaseId.of("testing-bigtest", "tagpost", "test");
      DatabaseClient dbClient = spanner.getDatabaseClient(db);
      String query = "SELECT * FROM Comment WHERE ThreadID = " + threadId;
      ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of(query));
      convertResultToCommentList(threadId, commentList, resultSet);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return commentList;
  }

  private void convertResultToThreadList(List<Thread> threadList, ResultSet resultSet) {<commit_msg>use Statament.builder to build query<commit_after>package com.google.tagpost.spanner;

import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Statement;

import com.google.tagpost.Comment;
import com.google.tagpost.Tag;
import com.google.tagpost.Thread;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Singleton;

/** Data access and operations of Spanner */
@Singleton
public class SpannerService implements DataService {

  @Override
  public List<Thread> getAllThreadsByTag(Tag tag) {

    String SQLStatement = "SELECT ThreadID, PrimaryTag FROM Thread WHERE PrimaryTag = @primaryTag";

    SpannerOptions spannerOptions = SpannerOptions.newBuilder().build();
    Spanner spanner = spannerOptions.getService();
    List<Thread> threadList = new ArrayList<>();

    try {
      DatabaseId db = DatabaseId.of("testing-bigtest", "tagpost", "test");
      DatabaseClient dbClient = spanner.getDatabaseClient(db);

      Statement statement =
          Statement.newBuilder(SQLStatement).bind("primaryTag").to(tag.getTagName()).build();

      ResultSet resultSet = dbClient.singleUse().executeQuery(statement);
      convertResultToThreadList(threadList, resultSet);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return threadList;
  }

  @Override
  public List<Comment> getAllCommentsByThreadId(long threadId) {

    String SQLStatement = "SELECT * FROM Comment WHERE ThreadID = @threadId";

    SpannerOptions spannerOptions = SpannerOptions.newBuilder().build();
    Spanner spanner = spannerOptions.getService();
    List<Comment> commentList = new ArrayList<>();

    try {
      DatabaseId db = DatabaseId.of("testing-bigtest", "tagpost", "test");
      DatabaseClient dbClient = spanner.getDatabaseClient(db);

      Statement statement =
          Statement.newBuilder(SQLStatement).bind("threadId").to(threadId).build();

      ResultSet resultSet = dbClient.singleUse().executeQuery(statement);
      convertResultToCommentList(threadId, commentList, resultSet);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return commentList;
  }

  private void convertResultToThreadList(List<Thread> threadList, ResultSet resultSet) {
{'fraction_non_alphanumeric': 0.11028156996587031, 'fraction_numerical': 0.0, 'mean_word_length': 4.484210526315789, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '1367986', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1306, 'n_words': 282}