<commit_before>
package no.priv.garshol.duke;

import java.util.ArrayList;
import java.util.Properties;
import java.util.Collection;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class JDBCLinkDatabase implements LinkDatabase {
  private String driverklass;
  private String jdbcuri;
  private Properties props;
  private Statement stmt;
  private static final SimpleDateFormat dtformat =
    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
  public JDBCLinkDatabase(String driverklass,
                          String jdbcuri,
                          Properties props) {
    this.driverklass = driverklass;
    this.jdbcuri = jdbcuri;
    this.props = props;
    this.stmt = JDBCUtils.open(driverklass, jdbcuri, props);

    try {
      verifySchema();
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }
  
  public Collection<Link> getChangesSince(long since) {
    String where = "";
    if (since != 0)
      where = "where timestamp > TIMESTAMP '" + dtformat.format(since) + "'";
    return queryForLinks("select * from links " + where +
                         " order by timestamp desc");
  }

  public Collection<Link> getAllLinksFor(String id) {
    return queryForLinks("select * from links where " +
                         "id1 = '" + escape(id) + "' or " +
                         "id2 = '" + escape(id) + "'");
  }
  
  public Collection<Link> getAllLinks() {
    return getChangesSince(0);
  }

  public void assertLink(Link link) {
    // (1) query to see if the link is already there
    // FIXME: use prepared statement
    try {
      Link existing = null;
      ResultSet rs = stmt.executeQuery("select * from links where " +
                                  "id1 = '" + escape(link.getID1()) + "' and " +
                                  "id2 = '" + escape(link.getID2()) + "'");
      if (rs.next()) {
        existing = makeLink(rs);<commit_msg>More flexible API to support paging of links, plus better timestamp precision.<commit_after>
package no.priv.garshol.duke;

import java.util.ArrayList;
import java.util.Properties;
import java.util.Collection;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class JDBCLinkDatabase implements LinkDatabase {
  private String driverklass;
  private String jdbcuri;
  private Properties props;
  private Statement stmt;
  private static final SimpleDateFormat dtformat =
    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  
  public JDBCLinkDatabase(String driverklass,
                          String jdbcuri,
                          Properties props) {
    this.driverklass = driverklass;
    this.jdbcuri = jdbcuri;
    this.props = props;
    this.stmt = JDBCUtils.open(driverklass, jdbcuri, props);

    try {
      verifySchema();
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }
  
  public Collection<Link> getAllLinks() {
    return getChangesSince(0, 0, 0);
  }
  
  public Collection<Link> getChangesSince(long since) {
    return getChangesSince(since, 0, 0);
  }

  public Collection<Link> getChangesSince(long since, long before) {
    return getChangesSince(since, before, 0);
  }

  public Collection<Link> getChangesSince(long since, long before, int pagesize) {
    String where = "";
    if (since != 0 || before != 0)
      where = "where ";
    if (since != 0)
      where += "timestamp > TIMESTAMP '" + dtformat.format(since) + "'";
    if (before != 0) {
      if (since != 0)
        where += "and ";
      where += "timestamp <= TIMESTAMP '" + dtformat.format(before) + "'";
    }

    String limit = "";
    if (pagesize != 0)
      limit = " limit " + pagesize;
    
    return queryForLinks("select * from links " + where +
                         " order by timestamp desc" + limit);
  }
  
  public Collection<Link> getAllLinksFor(String id) {
    return queryForLinks("select * from links where " +
                         "id1 = '" + escape(id) + "' or " +
                         "id2 = '" + escape(id) + "'");
  }

  public void assertLink(Link link) {
    // (1) query to see if the link is already there
    // FIXME: use prepared statement
    try {
      Link existing = null;
      ResultSet rs = stmt.executeQuery("select * from links where " +
                                  "id1 = '" + escape(link.getID1()) + "' and " +
                                  "id2 = '" + escape(link.getID2()) + "'");
      if (rs.next()) {
        existing = makeLink(rs);
{'fraction_non_alphanumeric': 0.1156740485917007, 'fraction_numerical': 0.006020210707374758, 'mean_word_length': 2.4030724213606436, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3952535', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1354, 'n_words': 357}