<commit_before>var sys = require('sys');

// Escape of values from native to SQL string.
// http://www.sqlite.org/lang_expr.html
function sql_escape(value) {
  if (value === null) {
    return "NULL";
  }
  if (value === true) {
    return "TRUE";
  }
  if (value === false) {
    return "FALSE";
  }
  if (value.constructor.name === 'String') {
    return "'" + value.replace("'", "''") + "'";
  }
  return value.toString();
}

// Fill in the placeholders with native values
function merge(sql, parameters) {
  if (parameters.length === 0) {
    return sql;
  }
  if (parameters.length === 1 && parameters[0].constructor.name === 'Object') {
    parameters = parameters[0];
    // Named parameters
    for (var key in parameters) {
      if (parameters.hasOwnProperty(key)) {
        sql = sql.replace(":" + key, sql_escape(parameters[key]));
      }
    }
  } else {
    if (parameters.length === 1 && parameters[0].constructor.name === 'Array') {
      parameters = parameters[0];
    }
    // ordered parameters
    parameters.forEach(function (param) {
      sql = sql.replace("?", sql_escape(param));
    });
  }
  return sql;
}

// Parse a result string from sqlite into an array of json objects
function parse(string) {
  var headers, p;

  if (string.length === 0) {
    return [];
  }
  
  // Get the location of the first newline.
  p = string.indexOf("\n");

  // SQL errors return a single line ending in newline.
  if (p === string.length - 1) {
    return string.substr(0, p);
  }

  // Find and extract the header row.
  // It's \t seperated fields ending with a \n.
  headers = string.substr(0, p).split("\t");

  // Chop off the header from the string and the trailing newline.
  // Then split into rows seperated by "\n".
  // Then for each row, zip the fields seperated by "\t" with the headers as
  // keys, converting values of "\r" to null.
  return string.substr(p + 1, string.length - p - 2)
    .split("\n").map(function (row) {
      var data = {};
      row.split("\t").forEach(function (item, index) {
        data[headers[index]] = item === "\r" ? null : item;
      });
      return data;
    });
}

function js_to_sql(class) {
  if (class === String) {
    return 'text';
  }
  if (class === Number) {
    return 'integer';
  }
  if (class === Boolean) {
    return 'bool';
  }
  throw "Unknown type " + class;
}

function condition_to_sql(condition, value) {
  var operator,
      p = condition.indexOf(' ');
  if (p === -1) {
    return condition + " = " + sql_escape(value);
  }
  operator = condition.substr(p + 1);
  condition = condition.substr(0, p);
  if (['<', '>', '=', '<=', '>=', '!=', '<>'].indexOf(operator) >= 0) {
    return condition + " " + operator + " " + sql_escape(value);
  }
  if (operator === '%') {
    return condition + " LIKE " + sql_escape(value);
  }
  sys.debug(operator);
  throw "Invalid operator " + operator;
}

function Store(conn, name, columns) {
  var key,
      types = [];

  this.name = name;
  this.conn = conn;

  for (key in columns) {
    if (columns.hasOwnProperty(key)) {
      types.push(key + " " + js_to_sql(columns[key]));
    }
  }

  conn.execute("CREATE TABLE " + name + "(" + types.join(", ") +")").wait();
}
Store.prototype = {

  get: function (id) {
    var promise = new process.Promise();
    this.conn.query(<commit_msg>Fix a bug where you couldn't get a sqlite store without specifying the explicit structre.<commit_after>var sys = require('sys');

// Escape of values from native to SQL string.
// http://www.sqlite.org/lang_expr.html
function sql_escape(value) {
  if (value === null) {
    return "NULL";
  }
  if (value === true) {
    return "TRUE";
  }
  if (value === false) {
    return "FALSE";
  }
  if (value.constructor.name === 'String') {
    return "'" + value.replace("'", "''") + "'";
  }
  return value.toString();
}

// Fill in the placeholders with native values
function merge(sql, parameters) {
  if (parameters.length === 0) {
    return sql;
  }
  if (parameters.length === 1 && parameters[0].constructor.name === 'Object') {
    parameters = parameters[0];
    // Named parameters
    for (var key in parameters) {
      if (parameters.hasOwnProperty(key)) {
        sql = sql.replace(":" + key, sql_escape(parameters[key]));
      }
    }
  } else {
    if (parameters.length === 1 && parameters[0].constructor.name === 'Array') {
      parameters = parameters[0];
    }
    // ordered parameters
    parameters.forEach(function (param) {
      sql = sql.replace("?", sql_escape(param));
    });
  }
  return sql;
}

// Parse a result string from sqlite into an array of json objects
function parse(string) {
  var headers, p;

  if (string.length === 0) {
    return [];
  }
  
  // Get the location of the first newline.
  p = string.indexOf("\n");

  // SQL errors return a single line ending in newline.
  if (p === string.length - 1) {
    return string.substr(0, p);
  }

  // Find and extract the header row.
  // It's \t seperated fields ending with a \n.
  headers = string.substr(0, p).split("\t");

  // Chop off the header from the string and the trailing newline.
  // Then split into rows seperated by "\n".
  // Then for each row, zip the fields seperated by "\t" with the headers as
  // keys, converting values of "\r" to null.
  return string.substr(p + 1, string.length - p - 2)
    .split("\n").map(function (row) {
      var data = {};
      row.split("\t").forEach(function (item, index) {
        data[headers[index]] = item === "\r" ? null : item;
      });
      return data;
    });
}

function js_to_sql(class) {
  if (class === String) {
    return 'text';
  }
  if (class === Number) {
    return 'integer';
  }
  if (class === Boolean) {
    return 'bool';
  }
  throw "Unknown type " + class;
}

function condition_to_sql(condition, value) {
  var operator,
      p = condition.indexOf(' ');
  if (p === -1) {
    return condition + " = " + sql_escape(value);
  }
  operator = condition.substr(p + 1);
  condition = condition.substr(0, p);
  if (['<', '>', '=', '<=', '>=', '!=', '<>'].indexOf(operator) >= 0) {
    return condition + " " + operator + " " + sql_escape(value);
  }
  if (operator === '%') {
    return condition + " LIKE " + sql_escape(value);
  }
  sys.debug(operator);
  throw "Invalid operator " + operator;
}

function Store(conn, name, columns) {
  var key,
      types = [];

  this.name = name;
  this.conn = conn;

  if (columns) {
    for (key in columns) {
      if (columns.hasOwnProperty(key)) {
        types.push(key + " " + js_to_sql(columns[key]));
      }
    }
  
    conn.execute("CREATE TABLE " + name + "(" + types.join(", ") +")").wait();
  }
}
Store.prototype = {

  get: function (id) {
    var promise = new process.Promise();
    this.conn.query(
{'fraction_non_alphanumeric': 0.17475152054591306, 'fraction_numerical': 0.005043762053107847, 'mean_word_length': 2.71664829106946, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3711437', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2179, 'n_words': 617}