<commit_before>/* jslint node:true  */
/* global IDBKeyRange: true, Promise: true */
'use strict';

/**
 * Stores all query options and compiles a query
 * @param {String} store The store to query
 */
var QueryWrapper = function (store, owningInstanceConnectionPromise) {
  this._storename = store;
  this._connectionPromise = owningInstanceConnectionPromise;

  this._findKey = null;

  this._queryType = null;
  this._tranctionMode = QueryWrapper.transactionMode.READONLY;
};

QueryWrapper.transactionMode = {
  READONLY: 'readonly',
  READWRITE: 'readwrite'
};

QueryWrapper.queryTypes = {
  FIND: 'FIND',
  FINDALL: 'FINDALL',
  INSERT: 'INSERT',
  UPSERT: 'UPSERT',
  REMOVE: 'REMOVE',
  COUNT: 'COUNT',
  CLEAR: 'CLEAR'
};

QueryWrapper.prototype._execute = function () {
  var p = this._connectionPromise;
  var method;

  switch(this._queryType) {
    case QueryWrapper.queryTypes.FIND:
      method = this._find.bind(this);
      break;
    case QueryWrapper.queryTypes.FINDALL:
      method = this._findAll.bind(this);
      break;
    case QueryWrapper.queryTypes.INSERT:
      method = this._insert.bind(this);
      break;
    case QueryWrapper.queryTypes.UPSERT:
      method = this._upsert.bind(this);
      break;
    case QueryWrapper.queryTypes.REMOVE:
      method = this._remove.bind(this);
      break;
    case QueryWrapper.queryTypes.COUNT:
      method = this._count.bind(this);
      break;    
    case QueryWrapper.queryTypes.CLEAR:
      method = this._clear.bind(this);
      break;                   
  }

  if (method) {
    return p.then(function (idb) {
      return idb._database.transaction([this._storename], this._tranctionMode);
    }.bind(this))
    .then(method);
  }

  throw new Error('No valid method has been selected, this query will not produce anything');
};

// add then and catch methods to the prototype,
// so they can be used to kick of execution of 
// the query
['then', 'catch'].forEach(function (method) {
  QueryWrapper.prototype[method] = function () {
    var promise = this._execute();
    return promise[method].apply(promise, arguments);
  };
});

QueryWrapper.prototype._setQueryTypeAndTransactionMode = function (type) {

  if (!QueryWrapper.queryTypes[type]) {
    throw new Error('trying to set invalid query type: ' + type);
  }

  if (this._queryType) {
    console.warn('query type was already set to "' + this._queryType + '". Did you really want to change it?');
  }

  // update transaction mode based on the action
  var transactionMode = QueryWrapper.transactionMode.READONLY;
  switch(type) {
    case QueryWrapper.queryTypes.INSERT:<commit_msg>Waiting for the transaction to complete before resolving the promise<commit_after>/* jslint node:true  */
/* global IDBKeyRange: true, Promise: true */
'use strict';

/**
 * Stores all query options and compiles a query
 * @param {String} store The store to query
 */
var QueryWrapper = function (store, owningInstanceConnectionPromise, insideTransaction) {
  this._storename = store;
  this._connectionPromise = owningInstanceConnectionPromise;

  this._findKey = null;

  this._queryType = null;
  this._tranctionMode = QueryWrapper.transactionMode.READONLY;
  this._isInsideTransaction = !!insideTransaction;
};

QueryWrapper.transactionMode = {
  READONLY: 'readonly',
  READWRITE: 'readwrite'
};

QueryWrapper.queryTypes = {
  FIND: 'FIND',
  FINDALL: 'FINDALL',
  INSERT: 'INSERT',
  UPSERT: 'UPSERT',
  REMOVE: 'REMOVE',
  COUNT: 'COUNT',
  CLEAR: 'CLEAR'
};

QueryWrapper.prototype._execute = function () {
  var p = this._connectionPromise;
  var method;

  switch(this._queryType) {
    case QueryWrapper.queryTypes.FIND:
      method = this._find.bind(this);
      break;
    case QueryWrapper.queryTypes.FINDALL:
      method = this._findAll.bind(this);
      break;
    case QueryWrapper.queryTypes.INSERT:
      method = this._insert.bind(this);
      break;
    case QueryWrapper.queryTypes.UPSERT:
      method = this._upsert.bind(this);
      break;
    case QueryWrapper.queryTypes.REMOVE:
      method = this._remove.bind(this);
      break;
    case QueryWrapper.queryTypes.COUNT:
      method = this._count.bind(this);
      break;    
    case QueryWrapper.queryTypes.CLEAR:
      method = this._clear.bind(this);
      break;                   
  }

  if (method) {
    var tx;
    var returnPromise = p.then(function (idb) {
      tx = idb._database.transaction([this._storename], this._tranctionMode);
      return tx;
    }.bind(this))
    .then(method);

    if (!this._isInsideTransaction) {
      returnPromise = returnPromise.then(function (result) {
        // we have to wait for the transaction to be completed
        // before the next chained statement can happen, in order
        // to guarantee that changed have been made, if we are
        // using separate transactions. actions made inside a single
        // transactions are consistent
        var p = Promise.defer();
        tx.oncomplete = function () {
          p.resolve(result);
        };
        tx.onerror = function (e) {
          p.reject(new Error(e.target.error.message));
        };
        return p.promise;
      });
    } else {
      tx = null;
    }

    return returnPromise;
  }

  throw new Error('No valid method has been selected, this query will not produce anything');
};

// add then and catch methods to the prototype,
// so they can be used to kick of execution of 
// the query
['then', 'catch'].forEach(function (method) {
  QueryWrapper.prototype[method] = function () {
    var promise = this._execute();
    return promise[method].apply(promise, arguments);
  };
});

QueryWrapper.prototype._setQueryTypeAndTransactionMode = function (type) {

  if (!QueryWrapper.queryTypes[type]) {
    throw new Error('trying to set invalid query type: ' + type);
  }

  if (this._queryType) {
    console.warn('query type was already set to "' + this._queryType + '". Did you really want to change it?');
  }

  // update transaction mode based on the action
  var transactionMode = QueryWrapper.transactionMode.READONLY;
  switch(type) {
    case QueryWrapper.queryTypes.INSERT:
{'fraction_non_alphanumeric': 0.11492561713258133, 'fraction_numerical': 0.0, 'mean_word_length': 3.167574931880109, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5141581', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1785, 'n_words': 481}