<commit_before>/*
    Copyright 2010 <NAME>

    This file is part of PolicyFeed module.

    PolicyFeed is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PolicyFeed is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with PolicyFeed.  If not, see <http://www.gnu.org/licenses/>.
*/

var config = require("config");
var gluestick = require("gluestick");

var DB = gluestick.loadModule("DB_users");


/**
 *
 */
exports.exists = function(id) {
    return this.read(id);
}


/**
 *
 */
exports.emailExists = function(email) {
    return this.select({email: email}).length;
}


/**
 *
 */
exports.read = function(id) {
    return this.select_one({id: id});
}


/**
 *
 */
exports.select = function(filter, sort) {
    var sql = "select * from users where ";
    var values = [];
    var sep = "";
    for (var field in filter) {

        if (filter[field] instanceof Array) {
            sql += sep + field + " IN ('" + filter[field].join("','") + "')";
        } else {
            sql += sep + field + "=?";
            values.push(filter[field]);
        }
        
        sep = " AND ";
    }

    if (sort) {
        sep = "";
        sql += " ORDER BY ";
        for (var field in sort) {
            sql += sep + field + " " + sort[field];
            sep = ",";
        }
    }

    var result = [];
    var rs = DB.prepared_query(sql, values);
    print(rs, sql, values);
    if (rs) {
        result = DB.get_all(rs) || [];
        rs.getStatement().close();
    }
    return result;
}


/**
 *
 */
exports.select_one = function(filter, sort) {
    var rows = this.select(filter, sort);
    if (rows)
        return rows[0];
    else
        return rows;
}


/**
 *
 */
exports.getByEmail = function(email) {
    return this.select_one({email: email});
}

/**
 *
 */
exports.getByKey = function(key) {
    return this.select_one({key: key});
}


/**
 *
 */
exports.write = function(id, data) {
    var fields = [];
    var values = [];
    for (var key in data) {
        fields.push(key);
        values.push(data[key]);
    }

    if (!this.exists(id)) {
        if (!data.key) {
            fields.push("key");
            values.push(String.random(40));
        }
        var sql = "INSERT INTO users (`" + fields.join("`,`") + "`) VALUES(" + "?,".repeat(values.length - 1) + "?)";
    } else {
        values.push(id);
        var sql = "UPDATE users SET `" + fields.join('`=?, ') + "`=? WHERE id=?";
    }

    var rs = DB.prepared_query(sql, values);
    if (rs) {
        if (rs.getStatement) {
            rs.getStatement().close();
            return true;
        } else {
            return rs;
        }
    }
}

<commit_msg>Refactored PolicyFeed users storage to extend a DB table module.<commit_after>/*
    Copyright 2010 <NAME>

    This file is part of PolicyFeed module.

    PolicyFeed is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PolicyFeed is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with PolicyFeed.  If not, see <http://www.gnu.org/licenses/>.
*/

// Requirements:
var gluestick = require("gluestick");
var ringo_objects = require("ringo/utils/objects");
var ringo_strings = require("ringo/utils/strings");

// Extends:
gluestick.extendModule(exports, "ctl/Storage/DbTable");

// Configuration for ctl/DB/Table:
exports.connect("DB_users", "users");


/**
 *
 */
exports.emailExists = function(email) {
    return this.list({email: email}).length;
}


/**
 *
 */
exports.getByEmail = function(email) {
    return this.read({email: email});
}

/**
 *
 */
exports.getByKey = function(key) {
    return this.read({key: key});
}


/**
 *
 */
var parent_create = ringo_objects.clone(exports.create);
exports.create = function(id, data) {
    if (!data.key) {
        data.key = ringo_strings.random(40);
    }
    return parent_create(id, data);
}


{'fraction_non_alphanumeric': 0.13725490196078433, 'fraction_numerical': 0.003373392367699768, 'mean_word_length': 2.4703730797366497, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6510610', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1417, 'n_words': 441}