<commit_before>/**
 * Module dependencies
 */
var pg = require('pg');
var caminte = require('caminte');
var util = require('util');

exports.initialize = function initializeSchema(schema, callback) {
    if (!pg) {
        throw new Error('module pg is not defined, try\n  npm install pg');
    }

    var Client = pg.Client;
    var s = schema.settings;
    schema.client = new Client(s.url ? s.url : {
        host: s.host || 'localhost',
        port: s.port || 5432,
        user: s.username,
        password: <PASSWORD>,
        database: s.database,
        debug: s.debug
    });
    schema.adapter = new PG(schema.client);
    schema.adapter.connect(callback);
};

function PG(client) {
    this.name = 'postgres';
    this._models = {};
    this.client = client;
}

require('util').inherits(PG, caminte.BaseSQL);

PG.prototype.connect = function(callback) {
    this.client.connect(function(err) {
        if (!err) {
            callback();
        } else {
            console.error(err);
            throw err;
        }
    });
};

PG.prototype.tableEscaped = function(model) {
    return this.escapeName(this.table(model));
};

PG.prototype.query = function(sql, callback) {
    var time = Date.now();
    var log = this.log;
    this.client.query(sql, function(err, data) {
        if (log)
            log(sql, time);
        callback(err, data ? data.rows : null);
    });
};

/**
 * Must invoke callback(err, id)
 * @param {String} model
 * @param {Object} data
 * @param {Function} callback
 */
PG.prototype.create = function(model, data, callback) {
    var fields = this.toFields(model, data, true);
    var sql = 'INSERT INTO ' + this.tableEscaped(model) + '';
    if (fields) {
        sql += ' ' + fields;
    } else {
        sql += ' VALUES ()';
    }
    sql += ' RETURNING id';
    this.query(sql, function(err, info) {
        if (err)
            return callback(err);
        callback(err, info && info[0] && info[0].id);
    });
};

/**
 * Must invoke callback(err, data)
 * @param {String} model
 * @param {Object} data
 * @param {Function} callback
 */
PG.prototype.updateOrCreate = function(model, data, callback) {
    var pg = this;
    var fieldsNames = [];
    var fieldValues = [];
    var combined = [];
    var props = this._models[model].properties;
    Object.keys(data).forEach(function(key) {
        if (props[key] || key === 'id') {
            var k = '"' + key + '"';
            var v;
            if (key !== 'id') {
                v = pg.toDatabase(props[key], data[key]);
            } else {
                v = data[key];
            }
            fieldsNames.push(k);
            fieldValues.push(v);
            if (key !== 'id')
                combined.push(k + ' = ' + v);
        }
    });

    var sql = 'UPDATE ' + this.tableEscaped(model);
    sql += ' SET ' + combined + ' WHERE id = ' + data.id + ';';
    sql += ' INSERT INTO ' + this.tableEscaped(model);
    sql += ' (' + fieldsNames.join(', ') + ')';
    sql += ' SELECT ' + fieldValues.join(', ');
    sql += ' WHERE NOT EXISTS (SELECT 1 FROM ' + this.tableEscaped(model);
    sql += ' WHERE id = ' + data.id + ') RETURNING id';

    this.query(sql, function(err, info) {
        if (!err && info && info[0] && info[0].id) {
            data.id = info[0].id;
        }
        callback(err, data);
    });
};

/**
 * Update rows
 * @param {String} model
 * @param {Object} filter
 * @param {Object} data
 * @param {Function} callback
 */
PG.prototype.update = function(model, filter, data, callback) {
    if ('function' === typeof filter) {
        return filter(new Error("Get parametrs undefined"), null);
    }
    if ('function' === typeof data) {
        return data(new Error("Set parametrs undefined"), null);
    }
    filter = filter.where ? filter.where : filter;
    var self = this;
    var combined = [];
    Object.keys(data).forEach(function (key) {
        if (props[key] || key === 'id') {
            var k = '`' + key + '`';
            var v;
            if (key !== 'id') {
                v = self.toDatabase(props[key], data[key]);
            } else {
                v = data[key];
            }
            combined.push(k + ' = ' + v);
        }
    });

    var sql = 'UPDATE ' + self.tableEscaped(model);
    sql += ' SET ' + combined.join(', ');
    sql += ' ' + self.buildWhere(filter, self, model);

    this.query(sql, function(err, affected) {
        callback(err, affected);
    });
};<commit_msg>Added missing `props`<commit_after>/**
 * Module dependencies
 */
var pg = require('pg');
var caminte = require('caminte');
var util = require('util');

exports.initialize = function initializeSchema(schema, callback) {
    if (!pg) {
        throw new Error('module pg is not defined, try\n  npm install pg');
    }

    var Client = pg.Client;
    var s = schema.settings;
    schema.client = new Client(s.url ? s.url : {
        host: s.host || 'localhost',
        port: s.port || 5432,
        user: s.username,
        password: <PASSWORD>,
        database: s.database,
        debug: s.debug
    });
    schema.adapter = new PG(schema.client);
    schema.adapter.connect(callback);
};

function PG(client) {
    this.name = 'postgres';
    this._models = {};
    this.client = client;
}

require('util').inherits(PG, caminte.BaseSQL);

PG.prototype.connect = function(callback) {
    this.client.connect(function(err) {
        if (!err) {
            callback();
        } else {
            console.error(err);
            throw err;
        }
    });
};

PG.prototype.tableEscaped = function(model) {
    return this.escapeName(this.table(model));
};

PG.prototype.query = function(sql, callback) {
    var time = Date.now();
    var log = this.log;
    this.client.query(sql, function(err, data) {
        if (log)
            log(sql, time);
        callback(err, data ? data.rows : null);
    });
};

/**
 * Must invoke callback(err, id)
 * @param {String} model
 * @param {Object} data
 * @param {Function} callback
 */
PG.prototype.create = function(model, data, callback) {
    var fields = this.toFields(model, data, true);
    var sql = 'INSERT INTO ' + this.tableEscaped(model) + '';
    if (fields) {
        sql += ' ' + fields;
    } else {
        sql += ' VALUES ()';
    }
    sql += ' RETURNING id';
    this.query(sql, function(err, info) {
        if (err)
            return callback(err);
        callback(err, info && info[0] && info[0].id);
    });
};

/**
 * Must invoke callback(err, data)
 * @param {String} model
 * @param {Object} data
 * @param {Function} callback
 */
PG.prototype.updateOrCreate = function(model, data, callback) {
    var pg = this;
    var fieldsNames = [];
    var fieldValues = [];
    var combined = [];
    var props = this._models[model].properties;
    Object.keys(data).forEach(function(key) {
        if (props[key] || key === 'id') {
            var k = '"' + key + '"';
            var v;
            if (key !== 'id') {
                v = pg.toDatabase(props[key], data[key]);
            } else {
                v = data[key];
            }
            fieldsNames.push(k);
            fieldValues.push(v);
            if (key !== 'id')
                combined.push(k + ' = ' + v);
        }
    });

    var sql = 'UPDATE ' + this.tableEscaped(model);
    sql += ' SET ' + combined + ' WHERE id = ' + data.id + ';';
    sql += ' INSERT INTO ' + this.tableEscaped(model);
    sql += ' (' + fieldsNames.join(', ') + ')';
    sql += ' SELECT ' + fieldValues.join(', ');
    sql += ' WHERE NOT EXISTS (SELECT 1 FROM ' + this.tableEscaped(model);
    sql += ' WHERE id = ' + data.id + ') RETURNING id';

    this.query(sql, function(err, info) {
        if (!err && info && info[0] && info[0].id) {
            data.id = info[0].id;
        }
        callback(err, data);
    });
};

/**
 * Update rows
 * @param {String} model
 * @param {Object} filter
 * @param {Object} data
 * @param {Function} callback
 */
PG.prototype.update = function(model, filter, data, callback) {
    if ('function' === typeof filter) {
        return filter(new Error("Get parametrs undefined"), null);
    }
    if ('function' === typeof data) {
        return data(new Error("Set parametrs undefined"), null);
    }
    filter = filter.where ? filter.where : filter;
    var self = this;
    var combined = [];
    var props = this._models[model].properties;
    Object.keys(data).forEach(function (key) {
        if (props[key] || key === 'id') {
            var k = '`' + key + '`';
            var v;
            if (key !== 'id') {
                v = self.toDatabase(props[key], data[key]);
            } else {
                v = data[key];
            }
            combined.push(k + ' = ' + v);
        }
    });

    var sql = 'UPDATE ' + self.tableEscaped(model);
    sql += ' SET ' + combined.join(', ');
    sql += ' ' + self.buildWhere(filter, self, model);

    this.query(sql, function(err, affected) {
        callback(err, affected);
    });
};
{'fraction_non_alphanumeric': 0.17097637093178777, 'fraction_numerical': 0.002229157378510923, 'mean_word_length': 2.123216150365472, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1844449', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2785, 'n_words': 673}