Beispiel #1
0
db.sqlValue = function(value, options)
{
    if (value == "null") return "NULL";
    switch (((options && options.type) || lib.typeName(value))) {
    case "expr":
    case "buffer":
        return value;

    case "real":
    case "float":
    case "double":
    case "decimal":
        return lib.toNumber(value, options);

    case "int":
    case "int32":
    case "bigint":
    case "smallint":
    case "integer":
    case "number":
    case "counter":
    case "now":
        return lib.toNumber(value, options);

    case "bool":
    case "boolean":
        return lib.toBool(value);

    case "date":
        return this.sqlQuote(lib.toDate(value).toISOString());

    case "time":
    case "timestamp":
        return this.sqlQuote(lib.toDate(value).toLocaleTimeString());

    case "mtime":
        return /^[0-9\.]+$/.test(value) ? this.toNumber(value, options) : this.sqlQuote(lib.toDate(value).toISOString());

    default:
        return this.sqlQuote(value);
    }
}
Beispiel #2
0
aws.ec2CreateTags = function(id, name, options, callback)
{
    if (typeof options == "function") callback = options, options = null;

    var tags = { "ResourceId.1": id }, i = 1;
    switch (lib.typeName(name)) {
    case "string":
        tags["Tag.1.Key"] = 'Name';
        tags["Tag.1.Value"] = name;
        i++;
        break;

    case "array":
        for (let j = 0; j < name.length - 1; j += 2) {
            tags["Tag." + i + ".Key"] = name[j];
            tags["Tag." + i + ".Value"] = String(name[j + 1]);
            i++;
        }
        break;

    case "object":
        for (const p in name) {
            tags["Tag." + i + ".Key"] = p;
            tags["Tag." + i + ".Value"] = String(name[p]);
            i++;
        }
        break;
    }
    // Additional tags
    if (options && options.tags) {
        for (const p in options.tags) {
            tags["Tag." + i + ".Key"] = p;
            tags["Tag." + i + ".Value"] = String(options.tags[p]);
            i++;
        }
    }
    if (i == 1) return lib.tryCall(callback);
    this.queryEC2("CreateTags", tags, options, callback);
}
Beispiel #3
0
 filter(function(x) { return ["string","number"].indexOf(lib.typeName(req.options.expected[x])) > -1 }).
Beispiel #4
0
db.sqlExpr = function(pool, name, value, options)
{
    var self = this;
    if (!name || typeof value == "undefined") return "";
    var type = options.type || "string";
    var op = (options.op || "").toLowerCase();
    var sql = "";
    switch (op) {
    case "not_in":
    case "not in":
    case "in":
        var list = [];
        // Convert type into array
        switch (lib.typeName(value)) {
        case "object":
            for (var p in value) list.push(value[p]);
            break;

        case "array":
            list = value;
            break;

        case "string":
            // For number array allow to be separated by comma as well, either one but not to be mixed
            if ((type == "number" || type == "int") && value.indexOf(',') > -1) {
                list = value.split(',');
                break;
            } else
            if (value.indexOf('|') > -1) {
                list = value.split('|');
                break;
            }

        default:
            list.push(value);
        }
        if (!list.length) break;
        sql += this.sqlColumn(name, pool) + " " + op + " (" + self.sqlValueIn(list, type) + ")";
        break;

    case "between":
    case "not between":
    case "not_between":
        // If we cannot parse out 2 values, treat this as exact operator
        var list = [];
        switch (lib.typeName(value)) {
        case "array":
            list = value;
            break;

        case "string":
            // For number array allow to be separated by comma as well, either one but not to be mixed
            if ((type == "number" || type == "int") && value.indexOf(',') > -1) {
                list = value.split(',');
                break;
            } else
            if (value.indexOf('|') > -1) {
                list = value.split('|');
                break;
            }
        }
        if (list.length > 1) {
            if (pool.configOptions.noBetween) {
                sql += this.sqlColumn(name, pool) + ">=" + this.sqlValue(list[0], options) + " AND " + name + "<=" + this.sqlValue(list[1], options);
            } else {
                sql += this.sqlColumn(name, pool)  + " " + op + " " + this.sqlValue(list[0], type) + " AND " + this.sqlValue(list[1], options);
            }
        } else {
            sql += this.sqlColumn(name, pool) + "=" + this.sqlValue(value, options);
        }
        break;

    case "null":
    case "not null":
    case "not_null":
        sql += this.sqlColumn(name, pool) + " IS " + op;
        break;

    case '@@':
        switch (lib.typeName(value)) {
        case "string":
            sql += this.sqlColumn(name, pool) + op + " to_tsquery('" + (options.lang || "english") + "'," + this.sqlQuote(value) + ")";
            break;

        case "array":
            value = value.map(function(x) { return "plainto_tsquery('" + (options.lang || "english") + "'," + self.sqlQuote(x) + ")" }).join('||');
            sql += this.sqlColumn(name, pool) + op + " (" +  value + ")";
            break;
        }
        break;

    case '~* any':
    case '!~* any':
        sql += this.sqlQuote(value) + " " + op + "(" + this.sqlColumn(name, pool) + ")";
        break;

    case 'contains':
    case 'not contains':
    case 'not_contains':
        value = '%' + value + '%';
        sql += this.sqlColumn(name, pool) + " LIKE " + this.sqlValue(value, options);
        break;

    case 'like%':
    case "ilike%":
    case "not like%":
    case "not ilike%":
        value += '%';
        op = op.substr(0, op.length-1);

    case '>':
    case '>=':
    case '<':
    case '<=':
    case '<>':
    case '!=':
    case "not like":
    case "like":
    case "ilike":
    case "not ilike":
    case "not similar to":
    case "similar to":
    case "regexp":
    case "not regexp":
    case "~":
    case "~*":
    case "!~":
    case "!~*":
    case 'match':
        sql += this.sqlColumn(name, pool) + " " + op + " " + this.sqlValue(value, options);
        break;

    case "iregexp":
    case "not iregexp":
        sql += "LOWER(" + this.sqlColumn(name, pool) + ") " + (op[0] == 'n' ? "NOT" : "") + " REGEXP " + this.sqlValue(value, options);
        break;

    case 'begins_with':
        sql += this.sqlColumn(name, pool) + " > " + this.sqlQuote(value.substr(0, value.length-1) + String.fromCharCode(value.charCodeAt(value.length-1) - 1));
        sql += " AND " + this.sqlColumn(name, pool) + " < " + this.sqlQuote(value.substr(0, value.length-1) + String.fromCharCode(value.charCodeAt(value.length-1) + 1));
        break;

    case 'expr':
        if (options.expr) {
            var str = options.expr;
            if (value.indexOf('|') > -1) value = value.split('|');
            str = str.replace(/%s/g, this.sqlValue(value, options));
            str = str.replace(/%1/g, this.sqlValue(value[0], options));
            str = str.replace(/%2/g, this.sqlValue(value[1], options));
            sql += str;
        }
        break;

    default:
        sql += this.sqlColumn(name, pool) + "=" + this.sqlValue(value, options);
        break;
    }
    return sql;
}