<commit_before>import map from 'lodash/map';

const paramsRE = /\$(\w+)/g;
const sqlSplitRE = /\s*;\s*(?=([^']*'[^']*')*[^']*$)/g;
const transactionRE = /^\s*((BEGIN)|(COMMIT)|(ROLLBACK))/i;

function findParams(sql) {
  let m;
  const params = [];
  /* eslint-disable no-cond-assign */
  while ((m = paramsRE.exec(sql))) {
    /* eslint-enable no-cond-assign */
    params.push(m[1]);
  }
  return params;
}

function replaceParams(sql) {
  return sql.replace(paramsRE, '?');
}

class ST {
  constructor(db, statement, options = {}) {
    this.db = db;
    this.statement = replaceParams(statement);
    this.params = findParams(statement);
    this.options = options;
  }

  get(params) {
    return this.db.process(this.statement, this.params, params)
    .then(result => result.rows.item(0));
  }
  all(params) {
    return this.db.process(this.statement, this.params, params)
    .then((result) => {
      let cursor = 0;
      const rows = [];
      let r;
      const rs = result.rows;
      /* eslint-disable no-cond-assign */
      while ((r = rs.item(cursor))) {
        cursor += 1;
        /* eslint-enable no-cond-assign */
        rows.push(r);
      }
      return rows;
    });
  }
  run(params) {
    return this.db.process(this.statement, this.params, params, true)
    .then(result => ({
      lastID: result.insertId,
      changes: result.rowsAffected,
    }));
  }

  each(params, onRow) {
    if (typeof params === 'function') {
      /* eslint-disable no-param-reassign */
      onRow = params;
      params = undefined;
      /* eslint-enable no-param-reassign */
    }
    return this.db.process(this.statement, this.params, params)
    .then((result) => {
      let cursor = 0;
      let r;
      const rs = result.rows;
      /* eslint-disable no-cond-assign */
      while ((r = rs.item(cursor))) {
        cursor += 1;
        /* eslint-enable no-cond-assign */
        onRow(r);
      }
    });
  }
}

export default class DB {
  constructor(db, options = {}) {
    this.db = db;
    this.options = options;
  }

  process(statement, params, paramValues, change) {
    return new Promise((resolve, reject) => {
      this.db[change ? 'transaction' : 'readTransaction'](
        tx => tx.executeSql(
          statement,
          params.map(name => paramValues[name]),
          (tx1, rs) => resolve(rs),
          (tx2, err) => {
            reject(`Error: ${JSON.stringify(err)} executing statement "${statement}", values ${paramValues}`);
          }
        ),
        (err) => {
          reject(`Error: ${JSON.stringify(err)} securing transaction for statement "${statement}", values ${paramValues}`);
        }
      );<commit_msg>Improve error reporting of values for failed statement<commit_after>import map from 'lodash/map';

const paramsRE = /\$(\w+)/g;
const sqlSplitRE = /\s*;\s*(?=([^']*'[^']*')*[^']*$)/g;
const transactionRE = /^\s*((BEGIN)|(COMMIT)|(ROLLBACK))/i;

function findParams(sql) {
  let m;
  const params = [];
  /* eslint-disable no-cond-assign */
  while ((m = paramsRE.exec(sql))) {
    /* eslint-enable no-cond-assign */
    params.push(m[1]);
  }
  return params;
}

function replaceParams(sql) {
  return sql.replace(paramsRE, '?');
}

class ST {
  constructor(db, statement, options = {}) {
    this.db = db;
    this.statement = replaceParams(statement);
    this.params = findParams(statement);
    this.options = options;
  }

  get(params) {
    return this.db.process(this.statement, this.params, params)
    .then(result => result.rows.item(0));
  }
  all(params) {
    return this.db.process(this.statement, this.params, params)
    .then((result) => {
      let cursor = 0;
      const rows = [];
      let r;
      const rs = result.rows;
      /* eslint-disable no-cond-assign */
      while ((r = rs.item(cursor))) {
        cursor += 1;
        /* eslint-enable no-cond-assign */
        rows.push(r);
      }
      return rows;
    });
  }
  run(params) {
    return this.db.process(this.statement, this.params, params, true)
    .then(result => ({
      lastID: result.insertId,
      changes: result.rowsAffected,
    }));
  }

  each(params, onRow) {
    if (typeof params === 'function') {
      /* eslint-disable no-param-reassign */
      onRow = params;
      params = undefined;
      /* eslint-enable no-param-reassign */
    }
    return this.db.process(this.statement, this.params, params)
    .then((result) => {
      let cursor = 0;
      let r;
      const rs = result.rows;
      /* eslint-disable no-cond-assign */
      while ((r = rs.item(cursor))) {
        cursor += 1;
        /* eslint-enable no-cond-assign */
        onRow(r);
      }
    });
  }
}

export default class DB {
  constructor(db, options = {}) {
    this.db = db;
    this.options = options;
  }

  process(statement, params, paramValues, change) {
    const values = params.map(name => paramValues[name]);
    return new Promise((resolve, reject) => {
      this.db[change ? 'transaction' : 'readTransaction'](
        tx => tx.executeSql(
          statement,
          values,
          (tx1, rs) => resolve(rs),
          (tx2, err) => {
            reject(`Error: ${JSON.stringify(err)} executing statement "${statement}", values ${values}`);
          }
        ),
        (err) => {
          reject(`Error: ${JSON.stringify(err)} securing transaction for statement "${statement}", values ${values}`);
        }
      );
{'fraction_non_alphanumeric': 0.1730911786508525, 'fraction_numerical': 0.0029651593773165306, 'mean_word_length': 2.3815789473684212, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '626435', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1731, 'n_words': 364}