<commit_before>
var sql = require('../sql/sql'),
    query = require('../sql/query');


exports.update = function (args, cb) {
    switch (args.action) {
        case 'insert':
        case 'update':
            var types = ['view', 'oneToOne', 'manyToOne'];
            break;
        case 'remove':
            var types = ['oneToOne', 'manyToOne', 'view'];
            break;
    }

    (function loopTypes (k) {
        if (k == types.length) return cb();
        
        args.type = types[k];
        args.tables = args.data[types[k]];
        if (!args.tables) return loopTypes(++k);
        var keys = Object.keys(args.tables);

        (function loopTables (i) {
            if (i == keys.length) return loopTypes(++k);
            
            var records = args.tables[keys[i]].records;
            if (!records) return loopTables(++i);

            (function loopRecords (j) {
                if (j == records.length) return loopTables(++i);

                var record = records[j],
                    settings = args.settings[keys[i]];
                tbl.inline(args, settings, record);
                var data = tbl.query(args, settings, record);

                tbl.execute(data.action, args, data.query, record, settings, function (err) {
                    if (err) return cb(err);
                    loopRecords(++j);
                });
            }(0));
        }(0));
    }(0));
}


var tbl = {
    // modifies inline record.columns[fk] with parent [pk]
    inline: function (args, settings, record) {
        if (args.type == 'view') return;

        var fk = args.settings[args.name].editview[args.type][settings.table.name];
        fk = fk instanceof Array ? fk : [fk];
        for (var i=0; i < fk.length; i++) {
            record.columns[fk[i]] = args.id[i];
        }
    },
    // settings - current table settings, record - current record data
    query: function (args, settings, record) {
        if (record.insert) {
            var data = this._kvp(args.name, record.columns, settings.columns),
                str = query.replace('insert',
                    sql.table(settings.table.name), data.keys.join(), data.values.join(), sql.list('',settings.table.pk));
            
            var action = 'insert';

        } else if (record.remove || args.action == 'remove') {
            var pk = settings.table.pk;
                str = query.replace('delete', sql.table(settings.table.name), sql.andValues(settings.table.name, pk, record.pk));

            var action = 'remove';

        } else { // update
            var pk = settings.table.pk,
                data = this._kvp(args.name, record.columns, settings.columns),
                str = query.replace('update',
                    sql.table(settings.table.name), data.pairs.join(), sql.andValues(settings.table.name, pk, record.pk));

            var action = 'update';
        }

        return {action:action, query:str};
    },
    _pks: function (args, settings, record, result) {
        var pks = settings.table.pk instanceof Array ? settings.table.pk : [settings.table.pk],
            columns = record.columns,
            values = [];

        for (var i=0; i < pks.length; i++) {
            if (columns[pks[i]] === undefined) {
                values.push(result.insertId);
            } else {
                values.push(columns[pks[i]]);
            }
        }

        record.pk = values;
        if (args.type == 'view' && args.action == 'insert')
            args.id = values;
    },
    execute: function (action, args, str, record, settings, cb) {
        args.log && console.log('tbl'.green, str);

        // insert or update
        if (action != 'remove') {
            args.db.client.query(str, function (err, result) {
                if (err) return cb(err);

                if (action == 'insert') {
                    tbl._pks(args, settings, record, result);
                }

                var queries = many.queries(action, args, record, settings.columns);
                many.execute(args, queries, function (err) {
                    if (err) return cb(err);
                    cb();
                });
            });
        }
        // remove
        else {<commit_msg>Database record updates are disabled if debug flag is set<commit_after>
var sql = require('../sql/sql'),
    query = require('../sql/query');


exports.update = function (args, cb) {
    switch (args.action) {
        case 'insert':
        case 'update':
            var types = ['view', 'oneToOne', 'manyToOne'];
            break;
        case 'remove':
            var types = ['oneToOne', 'manyToOne', 'view'];
            break;
    }

    (function loopTypes (k) {
        if (k == types.length) return cb();
        
        args.type = types[k];
        args.tables = args.data[types[k]];
        if (!args.tables) return loopTypes(++k);
        var keys = Object.keys(args.tables);

        (function loopTables (i) {
            if (i == keys.length) return loopTypes(++k);
            
            var records = args.tables[keys[i]].records;
            if (!records) return loopTables(++i);

            (function loopRecords (j) {
                if (j == records.length) return loopTables(++i);

                var record = records[j],
                    settings = args.settings[keys[i]];
                tbl.inline(args, settings, record);
                var data = tbl.query(args, settings, record);

                tbl.execute(data.action, args, data.query, record, settings, function (err) {
                    if (err) return cb(err);
                    loopRecords(++j);
                });
            }(0));
        }(0));
    }(0));
}


var tbl = {
    // modifies inline record.columns[fk] with parent [pk]
    inline: function (args, settings, record) {
        if (args.type == 'view') return;

        var fk = args.settings[args.name].editview[args.type][settings.table.name];
        fk = fk instanceof Array ? fk : [fk];
        for (var i=0; i < fk.length; i++) {
            record.columns[fk[i]] = args.id[i];
        }
    },
    // settings - current table settings, record - current record data
    query: function (args, settings, record) {
        if (record.insert) {
            var data = this._kvp(args.name, record.columns, settings.columns),
                str = query.replace('insert',
                    sql.table(settings.table.name), data.keys.join(), data.values.join(), sql.list('',settings.table.pk));
            
            var action = 'insert';

        } else if (record.remove || args.action == 'remove') {
            var pk = settings.table.pk;
                str = query.replace('delete', sql.table(settings.table.name), sql.andValues(settings.table.name, pk, record.pk));

            var action = 'remove';

        } else { // update
            var pk = settings.table.pk,
                data = this._kvp(args.name, record.columns, settings.columns),
                str = query.replace('update',
                    sql.table(settings.table.name), data.pairs.join(), sql.andValues(settings.table.name, pk, record.pk));

            var action = 'update';
        }

        return {action:action, query:str};
    },
    _pks: function (args, settings, record, result) {
        var pks = settings.table.pk instanceof Array ? settings.table.pk : [settings.table.pk],
            columns = record.columns,
            values = [];

        for (var i=0; i < pks.length; i++) {
            if (columns[pks[i]] === undefined) {
                values.push(result.insertId);
            } else {
                values.push(columns[pks[i]]);
            }
        }

        record.pk = values;
        if (args.type == 'view' && args.action == 'insert')
            args.id = values;
    },
    execute: function (action, args, str, record, settings, cb) {
        args.log && console.log('tbl'.green, str);
        if (args.debug) return cb();

        // insert or update
        if (action != 'remove') {
            args.db.client.query(str, function (err, result) {
                if (err) return cb(err);

                if (action == 'insert') {
                    tbl._pks(args, settings, record, result);
                }

                var queries = many.queries(action, args, record, settings.columns);
                many.execute(args, queries, function (err) {
                    if (err) return cb(err);
                    cb();
                });
            });
        }
        // remove
        else {
{'fraction_non_alphanumeric': 0.1465931746217896, 'fraction_numerical': 0.001172745396974317, 'mean_word_length': 1.6459820043437792, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1825456', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2360, 'n_words': 589}