<commit_before>var  _  = require('underscore')
, anyDB = require('any-db')
, pool  = {}
, uri   = require('uri-js')
, River = require('da_river').River
;


// ================================================================
// ================== Helpers =====================================
// ================================================================
//

var double_slashs = /\/\//g;
var mb            = function (num) { return num * 1024 * 1024; };
var now           = function () { return (new Date).getTime(); };

function replace_var(target, v_name, str) {
  return target.replace(new RegExp("\\" + v_name + '(?=[^a-zA-Z0-9\\_]|$)', 'g'), str);
}

function doc_to_update_sql(set_doc, where_doc) {
  var vals  = [];
  var ret   = "*";
  if(where_doc.returning) {
    ret = where_doc.returning.join(', ');
    delete where_doc['returning'];
  }

  var sql   = "UPDATE @table SET @set WHERE @where RETURNING " + ret + " ;";
  var set   = [];
  var where = [];

  _.each(set_doc, function (v, k) {
    if ((k.indexOf('_at') > 0) && v === '$now') {
      set.push( k +  " = (now() AT TIME ZONE 'UTC') ");
    } else {
      vals.push(v);
      set.push( k + " = $" + vals.length);
    }
  });

  return {
    sql: sql
    .replace('@set',   doc_to_set(set_doc, vals))
    .replace('@where', doc_to_and(where_doc, vals)),
    vals: vals
  };
}


function doc_to_set(doc, val_arr) {
  var arr = doc_to_equals(doc, val_arr);
  return arr.join(', ');
}

function doc_to_and(doc, val_arr) {
  var arr = doc_to_equals(doc, val_arr);
  return arr.join(' AND ');
}

function doc_to_equals(doc, val_arr) {
  if (!_.isObject(doc))
    doc = {id: doc};
  var i = val_arr.length;
  var sql_vals = [];
  _.each(doc, function (v, k) {
    if (_.isArray(v)) {
      var partial = k + " IN ( ";

      _.each(v, function (arr_v) {
        ++i;
        partial += ' $' + i;
        val_arr.push(arr_v);
      });

      partial += ' ) ';
      sql_vals.push(partial);
      return;
    }

    ++i;
    sql_vals.push( k + " = $" + i )
    val_arr.push(v);<commit_msg>Fixed: UPDATE sql statement was being processed twiced.<commit_after>var  _  = require('underscore')
, anyDB = require('any-db')
, pool  = {}
, uri   = require('uri-js')
, River = require('da_river').River
;


// ================================================================
// ================== Helpers =====================================
// ================================================================
//

var double_slashs = /\/\//g;
var mb            = function (num) { return num * 1024 * 1024; };
var now           = function () { return (new Date).getTime(); };

function replace_var(target, v_name, str) {
  return target.replace(new RegExp("\\" + v_name + '(?=[^a-zA-Z0-9\\_]|$)', 'g'), str);
}

function doc_to_update_sql(set_doc, where_doc) {
  var vals  = [];
  var ret   = "*";
  if(where_doc.returning) {
    ret = where_doc.returning.join(', ');
    delete where_doc['returning'];
  }

  var sql   = "UPDATE @table SET @set WHERE @where RETURNING " + ret + " ;";
  var set   = [];
  var where = [];

  return {
    sql: sql
    .replace('@set',   doc_to_set(set_doc, vals))
    .replace('@where', doc_to_and(where_doc, vals)),
    vals: vals
  };
}


function doc_to_set(doc, val_arr) {
  var arr = doc_to_equals(doc, val_arr);
  return arr.join(', ');
}

function doc_to_and(doc, val_arr) {
  var arr = doc_to_equals(doc, val_arr);
  return arr.join(' AND ');
}

function doc_to_equals(doc, val_arr) {
  if (!_.isObject(doc))
    doc = {id: doc};
  var i = val_arr.length;
  var sql_vals = [];
  _.each(doc, function (v, k) {
    if (_.isArray(v)) {
      var partial = k + " IN ( ";

      _.each(v, function (arr_v) {
        ++i;
        partial += ' $' + i;
        val_arr.push(arr_v);
      });

      partial += ' ) ';
      sql_vals.push(partial);
      return;
    }

    if (_.isNull(v)) {
      sql_vals.push( k + " = NULL " );
    } else if ((k.indexOf('_at') > 0) && v === '$now') {
      sql_vals.push( k +  " = (now() AT TIME ZONE 'UTC') ");
    } else {
      ++i;
      sql_vals.push( k + " = $" + i );
      val_arr.push(v);
    }

{'fraction_non_alphanumeric': 0.26790900290416264, 'fraction_numerical': 0.005324298160696999, 'mean_word_length': 2.809216589861751, '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': '3458857', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1477, 'n_words': 310}