<commit_before>/*jslint node: true */
'use strict';

var Q = require("q"),
    Collection = require("./Collection"),
    Database = function (options) {
        var prop,
            i,
            len,
            table;

        options = options || {};

        //Default properties, override in options
        this.autoCollection = (options.autoCollection != null && options.autoCollection) || true;
        this.tables = {};

        options.tables = options.tables || [];
        len = options.tables.length;

        for (i = 0; i < len; i += 1) {
            table = options.tables[i];
            this.tables[table.name] = new Collection(table);
        }

//        for (prop in options) {
//            if (options.hasOwnProperty(prop)) {
//                this[prop] = options[prop];
//            }
//        }

        this.alias = {
            "create": this.create,
            "insert": this.create,
            "post": this.create,

            "read": this.read,
            "select": this.read,
            "get": this.read,

            "update": this.update,
            "patch": this.update,

            "remove": this.remove,
            "delete": this.remove
        };

        this.create = function (params) {

        };
        this.read = function (params) {

        };
        this.update = function (params) {};
        this.remove = function (params) {};
        this.query = function (params) {
            return this.alias[params.type](params);
        };
    };

module.exports = Database;<commit_msg>Implemented Database controller splitting requests to Collections<commit_after>/*jslint node: true */
'use strict';

var Q = require("q"),
    Collection = require("./Collection"),
    jsn = require("bp-utilities").jsn,
    Database = function (options) {
        var prop,
            i,
            len,
            table;

        options = options || {};

        //Default properties, override in options
        this.autoCollection = (options.autoCollection != null && options.autoCollection) || true;
        this.tables = {};

        options.tables = options.tables || [];
        len = options.tables.length;

        for (i = 0; i < len; i += 1) {
            table = options.tables[i];
            this.tables[table.name] = new Collection(table);
        }

//        for (prop in options) {
//            if (options.hasOwnProperty(prop)) {
//                this[prop] = options[prop];
//            }
//        }

        this.alias = {
            "create": this.create,
            "insert": this.create,
            "post": this.create,

            "read": this.read,
            "select": this.read,
            "get": this.read,

            "update": this.update,
            "patch": this.update,

            "remove": this.remove,
            "delete": this.remove
        };

        this.ensureTablesExist = function (tableNames) {
            var i,
                len = tableNames.length,
                cur;

            for (i = 0; i < len; i += 1) {
                cur = tableNames[i];
                if (!this.tables.hasOwnProperty(cur)) {
                    if (this.autoCollection) {
                        // Implicit tables are created without a model
                        this.tables[cur] = new Collection();
                    } else {
                        throw new ReferenceError("Table `" + cur + "` does not exist in database and will not be created");
                    }
                }
            }
        };

        this.extractQueryValues = function (query) {
            var values = query.values || {},
                tables = query.tables || {},
                keys,
                keySplit,
                queries = {},
                i,
                len,
                cur,
                defer = [];

            len = tables.length;
            for (i = 0; i < len; i += 1) {
                queries[tables[i]] = {values: {}};
            }

            keys = jsn.keys(values);
            keySplit = keys.map(function (e) {return e.split("."); });

            len = keySplit.length;
            for (i = 0; i < len; i += 1) {
                cur = keySplit[i];
                if (cur.length === 1) {
                    defer.push(keys[i]);
                } else {
                    if (!queries.hasOwnProperty(cur[0])) {
                        throw new ReferenceError("Attempt to access undefined table `" + cur[0] + "` in query; " + JSON.stringify(query));
                    }
                    queries[cur[0]].values[cur[1]] = values[keys[i]];
                }
            }

            len = defer.length;
            for (i = 0; i < len; i += 1) {
                for (cur in queries) {
                    if (queries.hasOwnProperty(cur)) {
                        queries[cur].values[defer[i]] = values[defer[i]];
                    }
                }
            }
            return queries;
        };
        this.extractQueryConditions = function (query) {
            var where = query.where || {},
                tables = query.tables || {},
                keys,
                keySplit,
                queries = {},
                i,
                len,
                indx,
                cur,
                defer = [];

            len = tables.length;
            for (i = 0; i < len; i += 1) {
                queries[tables[i]] = {where: []};
            }

            keys = where.map(function (e) {return e.value; });
            keySplit = keys.map(function (e) {return e.split("."); });

            len = keySplit.length;
            for (i = 0; i < len; i += 1) {
                cur = keySplit[i];
                if (cur.length === 1) {
                    defer.push(i);
                } else {
                    if (!queries.hasOwnProperty(cur[0])) {
                        throw new ReferenceError("Attempt to access undefined table `" + cur[0] + "` in query; " + JSON.stringify(query));
                    }
                    indx = queries[cur[0]].where.push(jsn.copy(where[i]));
                    queries[cur[0]].where[indx - 1].value = cur[1];
                }
            }

            len = defer.length;
            for (i = 0; i < len; i += 1) {
                for (cur in queries) {
                    if (queries.hasOwnProperty(cur)) {
                        queries[cur].where.push(where[i]);
                    }
                }
            }
            return queries;
        };
        this.mergeQueryParts = function (values, conditions) {
            var prop,
                result = {};
            for (prop in values) {
                if (values.hasOwnProperty(prop)) {
                    if (conditions.hasOwnProperty(prop)) {
                        result[prop] = {values: values[prop], where: conditions[prop]};
                    }
                }
            }

            for (prop in conditions) {
                if (conditions.hasOwnProperty(prop)) {
                    if (values.hasOwnProperty(prop)) {
                        result[prop] = {values: values[prop], where: conditions[prop]};
                    }
                }
            }
            return result;
        };
        this.splitQueryToTables = function (query) {
            var vals = this.extractQueryValues(query),
                conditions = this.extractQueryConditions(query);
            return this.mergeQueryParts(vals, conditions);
        };

        this.create = function (query, callback) {
            var queries = this.splitQueryToTables(query),
                tables = jsn.keys(queries),
                results = 0,
                i,
                len = tables.length,
                cur;
            try {
                for (i = 0; i < len; i += 1) {
                    cur = tables[i];
                    results += this.tables[cur].create(queries[cur]).modified;
                }
            } catch (e) {
                callback(e, null);
                return;
            }
            callback(null, {modified: results});
        };
        this.read = function (query, callback) {
            var queries = this.splitQueryToTables(query),
                tables = jsn.keys(queries),
                ret,
                results = {},
                i,
                len = tables.length,
                cur;
            try {
                for (i = 0; i < len; i += 1) {
                    cur = tables[i];
                    ret = this.tables[cur].read(queries[cur]);
                    results.modified += ret.modified;
                    results[cur] = ret.data;
                }
            } catch (e) {
                callback(e, null);
                return;
            }
            callback(null, results);
        };
        this.update = function (query, callback) {
            var queries = this.splitQueryToTables(query),
                tables = jsn.keys(queries),
                results = 0,
                i,
                len = tables.length,
                cur;
            try {
                for (i = 0; i < len; i += 1) {
                    cur = tables[i];
                    results += this.tables[cur].update(queries[cur]).modified;
                }
            } catch (e) {
                callback(e, null);
                return;
            }
            callback(null, {modified: results});
        };
        this.remove = function (query, callback) {
            var queries = this.splitQueryToTables(query),
                tables = jsn.keys(queries),
                results = 0,
                i,
                len = tables.length,
                cur;
            try {
                for (i = 0; i < len; i += 1) {
                    cur = tables[i];
                    results += this.tables[cur].remove(queries[cur]).modified;
                }
            } catch (e) {
                callback(e, null);
                return;
            }
            callback(null, {modified: results});
        };
        this.query = function (params, callback) {
            this.ensureTablesExist(params.tables);
            return this.alias[params.type](params, callback);
        };
    };

module.exports = Database;
{'fraction_non_alphanumeric': 0.12468267916422573, 'fraction_numerical': 0.004003124389767624, 'mean_word_length': 1.1176348976638413, 'pattern_counts': {'":': 20, '<': 16, '<?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': '6569908', 'n_tokens_mistral': 2735, 'n_tokens_neox': 2555, 'n_words': 595}