<commit_before>
// Raw
// -------
var inherits      = require('inherits')
var EventEmitter  = require('events').EventEmitter
var assign        = require('lodash/object/assign')
var reduce        = require('lodash/collection/reduce')
var isPlainObject = require('lodash/lang/isPlainObject')
var _             = require('lodash');
var isNumber      = require('lodash/lang/isNumber');

function Raw(client) {
  this.client   = client

  this.sql      = ''
  this.bindings = []
  this._cached  = undefined

  // Todo: Deprecate
  this._wrappedBefore = undefined
  this._wrappedAfter  = undefined
  this._debug         = client && client.config && client.config.debug
}
inherits(Raw, EventEmitter)

assign(Raw.prototype, {

  set: function(sql, bindings) {
    this._cached  = undefined
    this.sql      = sql
    this.bindings = (_.isObject(bindings) || _.isUndefined(bindings)) ?  bindings : [bindings]

    return this
  },

  timeout: function(ms) {
    if(isNumber(ms) && ms > 0) {
      this._timeout = ms;
    }
    return this;
  },

  // Wraps the current sql with `before` and `after`.
  wrap: function(before, after) {
    this._cached        = undefined
    this._wrappedBefore = before
    this._wrappedAfter  = after
    return this
  },

  // Calls `toString` on the Knex object.
  toString: function() {
    return this.toQuery()
  },

  // Returns the raw sql for the query.
  toSQL: function() {
    if (this._cached) return this._cached
    if (Array.isArray(this.bindings)) {
      this._cached = replaceRawArrBindings(this)
    } else if (this.bindings && isPlainObject(this.bindings)) {
      this._cached = replaceKeyBindings(this)
    } else {
      this._cached = {
        method: 'raw',
        sql: this.sql,
        bindings: this.bindings
      }
    }
    if (this._wrappedBefore) {
      this._cached.sql = this._wrappedBefore + this._cached.sql
    }
    if (this._wrappedAfter) {
      this._cached.sql = this._cached.sql + this._wrappedAfter
    }
    this._cached.options = reduce(this._options, assign, {})
    if(this._timeout) {
      this._cached.timeout = this._timeout;
    }
    return this._cached
  }

})

function replaceRawArrBindings(raw) {
  var expectedBindings = raw.bindings.length
  var values           = raw.bindings
  var client           = raw.client
  var index            = 0;
  var bindings         = []

  var sql = raw.sql.replace(/\\?\?\??/g, function(match) {
    if (match === '\\?') {
      return match
    }

    var value = values[index++]

    if (value && typeof value.toSQL === 'function') {
      var bindingSQL = value.toSQL()
      if (bindingSQL.bindings !== undefined) {
        bindings = bindings.concat(bindingSQL.bindings)
      }
      return bindingSQL.sql
    }

    if (match === '??') {
      return client.formatter().columnize(value)
    }
    bindings.push(value)
    return '?'
  })

  if (expectedBindings !== index) {
    throw new Error('Expected ' + expectedBindings + ' bindings, saw ' + index)
  }

  return {
    method: 'raw',
    sql: sql,
    bindings: bindings
  }
}

function replaceKeyBindings(raw) {
  var values   = raw.bindings
  var client   = raw.client
  var sql      = raw.sql, bindings = []

  var regex = new RegExp('(\\:\\w+\\:?)', 'g')
  var emptyBindings = [];
  sql = raw.sql.replace(regex, function(full) {
    var key = full.trim();
    var isIdentifier = key[key.length - 1] === ':'
    var value = isIdentifier ? values[key.slice(1, -1)] : values[key.slice(1)]
    if (value === undefined) {
      emptyBindings.push(full);
      return full;
    }
    if (value && typeof value.toSQL === 'function') {
      var bindingSQL = value.toSQL()
      if (bindingSQL.bindings !== undefined) {
        bindings = bindings.concat(bindingSQL.bindings)
      }
      return full.replace(key, bindingSQL.sql)
    }
    if (isIdentifier) {
      return full.replace(key, client.formatter().columnize(value))
    }
    bindings.push(value)
    return full.replace(key, '?')
  })

  emptyBindings.map(function(binding) {
    sql = sql.replace(new RegExp(` ${binding}|${binding} |${binding}`, 'g'), '');
  });

  return {
    method: 'raw',
    sql: sql,
    bindings: bindings
  }
}

// Allow the `Raw` object to be utilized with full access to the relevant
// promise API.
require('./interface')(Raw)

module.exports = Raw<commit_msg>If a named binding is left undefined, then don't replace anything.<commit_after>
// Raw
// -------
var inherits      = require('inherits')
var EventEmitter  = require('events').EventEmitter
var assign        = require('lodash/object/assign')
var reduce        = require('lodash/collection/reduce')
var isPlainObject = require('lodash/lang/isPlainObject')
var _             = require('lodash');
var isNumber      = require('lodash/lang/isNumber');

function Raw(client) {
  this.client   = client

  this.sql      = ''
  this.bindings = []
  this._cached  = undefined

  // Todo: Deprecate
  this._wrappedBefore = undefined
  this._wrappedAfter  = undefined
  this._debug         = client && client.config && client.config.debug
}
inherits(Raw, EventEmitter)

assign(Raw.prototype, {

  set: function(sql, bindings) {
    this._cached  = undefined
    this.sql      = sql
    this.bindings = (_.isObject(bindings) || _.isUndefined(bindings)) ?  bindings : [bindings]

    return this
  },

  timeout: function(ms) {
    if(isNumber(ms) && ms > 0) {
      this._timeout = ms;
    }
    return this;
  },

  // Wraps the current sql with `before` and `after`.
  wrap: function(before, after) {
    this._cached        = undefined
    this._wrappedBefore = before
    this._wrappedAfter  = after
    return this
  },

  // Calls `toString` on the Knex object.
  toString: function() {
    return this.toQuery()
  },

  // Returns the raw sql for the query.
  toSQL: function() {
    if (this._cached) return this._cached
    if (Array.isArray(this.bindings)) {
      this._cached = replaceRawArrBindings(this)
    } else if (this.bindings && isPlainObject(this.bindings)) {
      this._cached = replaceKeyBindings(this)
    } else {
      this._cached = {
        method: 'raw',
        sql: this.sql,
        bindings: this.bindings
      }
    }
    if (this._wrappedBefore) {
      this._cached.sql = this._wrappedBefore + this._cached.sql
    }
    if (this._wrappedAfter) {
      this._cached.sql = this._cached.sql + this._wrappedAfter
    }
    this._cached.options = reduce(this._options, assign, {})
    if(this._timeout) {
      this._cached.timeout = this._timeout;
    }
    return this._cached
  }

})

function replaceRawArrBindings(raw) {
  var expectedBindings = raw.bindings.length
  var values           = raw.bindings
  var client           = raw.client
  var index            = 0;
  var bindings         = []

  var sql = raw.sql.replace(/\\?\?\??/g, function(match) {
    if (match === '\\?') {
      return match
    }

    var value = values[index++]

    if (value && typeof value.toSQL === 'function') {
      var bindingSQL = value.toSQL()
      if (bindingSQL.bindings !== undefined) {
        bindings = bindings.concat(bindingSQL.bindings)
      }
      return bindingSQL.sql
    }

    if (match === '??') {
      return client.formatter().columnize(value)
    }
    bindings.push(value)
    return '?'
  })

  if (expectedBindings !== index) {
    throw new Error('Expected ' + expectedBindings + ' bindings, saw ' + index)
  }

  return {
    method: 'raw',
    sql: sql,
    bindings: bindings
  }
}

function replaceKeyBindings(raw) {
  var values   = raw.bindings
  var client   = raw.client
  var sql      = raw.sql, bindings = []

  var regex = new RegExp('(\\:\\w+\\:?)', 'g')
  sql = raw.sql.replace(regex, function(full) {
    var key = full.trim();
    var isIdentifier = key[key.length - 1] === ':'
    var value = isIdentifier ? values[key.slice(1, -1)] : values[key.slice(1)]
    if (value === undefined) {
      return full;
    }
    if (value && typeof value.toSQL === 'function') {
      var bindingSQL = value.toSQL()
      if (bindingSQL.bindings !== undefined) {
        bindings = bindings.concat(bindingSQL.bindings)
      }
      return full.replace(key, bindingSQL.sql)
    }
    if (isIdentifier) {
      return full.replace(key, client.formatter().columnize(value))
    }
    bindings.push(value)
    return full.replace(key, '?')
  })

  return {
    method: 'raw',
    sql: sql,
    bindings: bindings
  }
}

// Allow the `Raw` object to be utilized with full access to the relevant
// promise API.
require('./interface')(Raw)

module.exports = Raw
{'fraction_non_alphanumeric': 0.14063231850117097, 'fraction_numerical': 0.001405152224824356, 'mean_word_length': 2.532258064516129, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1585135', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2696, 'n_words': 611}