<commit_before>var pg     = require('pg');
var should = require('should');
var async  = require('async');
var debug  = require('debug')('OSMBC:model:pgMap')

var config = require('../config.js');

function generateQuery(table,obj,order) {
  debug('generateQuery');
  var whereClause = "";

  if (typeof(obj)=='string') {
    whereClause = obj;
  } else {
    if (obj) {
      for (var k in obj) {
        var value = obj[k];
        var op = "=";
        if (typeof(value)=='string') {
          // escape the Apostroph
          value = value.replace("'","''");
          // check first operator in string
          if (value.substring(0,2)=="!=") {
            op = "!=";
            value = value.substring(2,9999);
          }           
        }

        var n = "data->>'"+k+"'"+op+"'"+value+"'"; 

        if (value=="") {
          if (op == "=") {
            n = "("+n+" or (data->'"+k+"') is null)";
          }
          if (op == "!=") {
            n = "("+n+" and (data->'"+k+"') is not null)";
          }
        }
        if (whereClause =="") whereClause = " where "+n;
        else whereClause += " and "+n;
      }    
    }
  }
  var orderby = " order by id";
  if (order) {
    should.exist(order.column);
    if (order.column != "id") orderby = " order by data->>'"+order.column+"'";
    if (order.desc) {
      orderby += " desc";
    }
    orderby += " , id ";
    if (order.limit) {
      orderby += " LIMIT "+order.limit;
    }
  }
  var query = "select id,data from "+table+whereClause+orderby;
  debug(query);
  return query;
}

 
module.exports.save = function(callback) {
  debug("save");
  var self = this;

  var table = self._meta.table;

  // first check, wether ID is known or not
  if (self.id == 0) {
    // we have to create the object
    debug("Object has to be created");

    // store first version in database
    self.version = 1;
    pg.connect(config.pgstring, function(err, client, pgdone) {
      if (err) {
        pgdone();
        return (callback(err));
      }
      var sqlquery = "insert into "+table+"(data) values ($1) returning id";
      debug("Query %s",sqlquery);
      var startTime = new Date().getTime();
      var query = client.query(sqlquery, [self]);
      query.on('row',function(row) {
        debug("Created Row ID %s",row.id);
        self.id = row.id;
      })
      query.on('end',function (result) {

        var endTime = new Date().getTime();
       // console.log("SQL: ["+ (endTime - startTime)/1000 +"] insert to "+ table);
        
        pgdone();
        return callback(null,self);
      })
    })
  } else {
    debug("Object will be updated, current version is %s",self.version);
    // we have to change the beer
    pg.connect(config.pgstring, function(err, client, pgdone) {
      if (err) {
        pgdone();
        return (callback(err));
      }
      async.series([
        function(cb) {
          var versionsEqual = false;

          var query = client.query("select (data->>'version')::int as version from "+table+" where id = $1",[self.id]);
            var startTime = new Date().getTime();
            query.on('row',function(row) {
            debug('Version in Database %s',row.version)
            if (row.version==null) {
              // No Data in Database, so no conflict.
              versionsEqual = true;
            }
            else if (row.version == self.version) {
              debug('No Error');
              versionsEqual = true;
            }
          })
          query.on('end',function(result){
            var err = null;
            var endTime = new Date().getTime();
         //   console.log("SQL: ["+ (endTime - startTime)/1000 +"]("+table+" versionCheck");
            if (!versionsEqual) {
              debug('send error')
              err = new Error("Version Nummber differs");
            }
            return cb(err);
          })
        }
        ],
        function(err) {
          if (err) {
            debug('Foreward Error');
            pgdone();
            return callback(err);
          }
          self.version += 1;
          var query = client.query("update "+table+" set data = $2 where id = $1", [self.id,self]);<commit_msg>Orthographie.<commit_after>var pg     = require('pg');
var should = require('should');
var async  = require('async');
var debug  = require('debug')('OSMBC:model:pgMap')

var config = require('../config.js');

function generateQuery(table,obj,order) {
  debug('generateQuery');
  var whereClause = "";

  if (typeof(obj)=='string') {
    whereClause = obj;
  } else {
    if (obj) {
      for (var k in obj) {
        var value = obj[k];
        var op = "=";
        if (typeof(value)=='string') {
          // escape the Apostroph
          value = value.replace("'","''");
          // check first operator in string
          if (value.substring(0,2)=="!=") {
            op = "!=";
            value = value.substring(2,9999);
          }           
        }

        var n = "data->>'"+k+"'"+op+"'"+value+"'"; 

        if (value=="") {
          if (op == "=") {
            n = "("+n+" or (data->'"+k+"') is null)";
          }
          if (op == "!=") {
            n = "("+n+" and (data->'"+k+"') is not null)";
          }
        }
        if (whereClause =="") whereClause = " where "+n;
        else whereClause += " and "+n;
      }    
    }
  }
  var orderby = " order by id";
  if (order) {
    should.exist(order.column);
    if (order.column != "id") orderby = " order by data->>'"+order.column+"'";
    if (order.desc) {
      orderby += " desc";
    }
    orderby += " , id ";
    if (order.limit) {
      orderby += " LIMIT "+order.limit;
    }
  }
  var query = "select id,data from "+table+whereClause+orderby;
  debug(query);
  return query;
}

 
module.exports.save = function(callback) {
  debug("save");
  var self = this;

  var table = self._meta.table;

  // first check, wether ID is known or not
  if (self.id == 0) {
    // we have to create the object
    debug("Object has to be created");

    // store first version in database
    self.version = 1;
    pg.connect(config.pgstring, function(err, client, pgdone) {
      if (err) {
        pgdone();
        return (callback(err));
      }
      var sqlquery = "insert into "+table+"(data) values ($1) returning id";
      debug("Query %s",sqlquery);
      var startTime = new Date().getTime();
      var query = client.query(sqlquery, [self]);
      query.on('row',function(row) {
        debug("Created Row ID %s",row.id);
        self.id = row.id;
      })
      query.on('end',function (result) {

        var endTime = new Date().getTime();
       // console.log("SQL: ["+ (endTime - startTime)/1000 +"] insert to "+ table);
        
        pgdone();
        return callback(null,self);
      })
    })
  } else {
    debug("Object will be updated, current version is %s",self.version);
    // we have to change the beer
    pg.connect(config.pgstring, function(err, client, pgdone) {
      if (err) {
        pgdone();
        return (callback(err));
      }
      async.series([
        function(cb) {
          var versionsEqual = false;

          var query = client.query("select (data->>'version')::int as version from "+table+" where id = $1",[self.id]);
            var startTime = new Date().getTime();
            query.on('row',function(row) {
            debug('Version in Database %s',row.version)
            if (row.version==null) {
              // No Data in Database, so no conflict.
              versionsEqual = true;
            }
            else if (row.version == self.version) {
              debug('No Error');
              versionsEqual = true;
            }
          })
          query.on('end',function(result){
            var err = null;
            var endTime = new Date().getTime();
         //   console.log("SQL: ["+ (endTime - startTime)/1000 +"]("+table+" versionCheck");
            if (!versionsEqual) {
              debug('send error')
              err = new Error("Version Number differs");
            }
            return cb(err);
          })
        }
        ],
        function(err) {
          if (err) {
            debug('Foreward Error');
            pgdone();
            return callback(err);
          }
          self.version += 1;
          var query = client.query("update "+table+" set data = $2 where id = $1", [self.id,self]);
{'fraction_non_alphanumeric': 0.1697121700704646, 'fraction_numerical': 0.005254986265376806, 'mean_word_length': 1.8367208672086721, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '294895', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2530, 'n_words': 641}