jssql icon indicating copy to clipboard operation
jssql copied to clipboard

Changing the function countRows

Open davidmgallardo opened this issue 9 years ago • 0 comments

I modified the countRows function to work properly, as in the js is a field that is called ID which I have in the database, and if not on all tables to be set up would require a field that is called by that way. With this change does not predisposed anyone to create a field with that name, but you can create any field and can pass as a parameter to the function countRows. As you can see in the code I added a new parameter to JSON (field) that receive the value of the field that we have to our table and we want to make the counter.

dbhelper.prototype.countRows = function (obj, callback) {
    //var sql = 'SELECT COUNT(id) as counter FROM ' + obj.table;
    var sql = 'SELECT COUNT(' + obj.field + ') as counter FROM ' + obj.table;
    if (obj.joiner) {
        sql = 'SELECT COUNT(' + obj.joiner + '.id) as counter FROM ' + obj.table;
    }
    if (obj.where) {
        sql += ' WHERE ' + obj.where;
    }
    Ti.API.info(sql);
    var result = this.getData(sql);
    this.db.close();

    if (callback) {
        callback(result[0].counter);
    } else {
        return result[0].counter;
    }
};

The function call would in this way:

var total=db.countRows({
        field: 'idAviso',
        table:  'avisos',
        where:   "aviso='" + mensajeAviso + "' and tipoAviso='" + tipo + "' and fechaAviso='" + fecha + "'"
    });

I hope I explained well, and can help a little to improve this fantastic tool. A greeting

davidmgallardo avatar Sep 02 '15 08:09 davidmgallardo