all files / query-builder/ query-library.js

94.41% Statements 422/447
89.07% Branches 269/302
100% Functions 26/26
94.38% Lines 420/445
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678   230× 230×   190× 177×   13×   230× 23×   207× 207×   13× 13×   64×   27× 27×   19× 19×   10× 10×           27× 27×     24× 24×   27× 27×                       76× 76× 76× 201× 201× 201× 49× 26× 26×     23× 23×     201× 201×   201× 201×   18× 18×     44× 44×                 38× 10×   28× 10×   18×     15×   38×                                                                                                 201× 201× 125×     76×   19× 19× 16× 16×     19× 19×   39× 39× 39× 39× 39× 39× 39× 39× 39× 39× 39× 39× 39× 93× 93× 93× 93× 93×         84× 15× 15× 15× 15× 15× 15× 15× 15× 15× 15× 15×       15× 11×       11× 11×       69× 69×   69×   69×   62× 45× 45× 49× 49× 49×   49×   49×                     41×                                 32×   49× 23× 18× 18× 18×   23×             23×   23×   23× 77× 77× 20× 20×     23×         17×       69×   65×     60×   69× 69×                   10× 10× 10×     10× 10×     16× 16× 16× 16×   31×     31× 67× 15× 15× 15×     52× 52× 16×   16× 16×                     16×             44×   44× 42× 42× 38× 38× 38×     17× 17×           38×     32×                                                 42×     36×             31×   15× 15× 11× 11×       15×   15×   273×        
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var QueryLibrary = (function () {
        function QueryLibrary(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        QueryLibrary.prototype.destroy = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.removeEventListener();
        };
        QueryLibrary.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.on('query-library', this.queryLibrary, this);
            this.parent.on('destroyed', this.destroy, this);
        };
        QueryLibrary.prototype.removeEventListener = function () {
            this.parent.off('query-library', this.queryLibrary);
            this.parent.off('destroyed', this.destroy);
        };
        QueryLibrary.prototype.queryLibrary = function (args) {
            switch (args.prop) {
                case 'getMongoFromRules':
                    args.value['obj']['mongoQuery'] = this.getMongoFromRules(args.value['rule'], args.value['mongoQuery']);
                    break;
                case 'mongoParser':
                    this.mongoParser(args.value['mongoQuery'], args.value['rule'], args.value['mongoLocale']);
                    break;
                case 'getParameterSql':
                    args.value['obj']['sql'] = this.getParameterSql(args.value['rule']);
                    break;
                case 'getNamedParameterSql':
                    args.value['obj']['sql'] = this.getNamedParameterSql(args.value['rule']);
                    break;
                case 'convertParamSqlToSql':
                    args.value['obj']['sql'] = this.convertParamSqlToSql(args.value['sql']);
                    break;
                case 'convertNamedParamSqlToSql':
                    args.value['obj']['sql'] = this.convertNamedParamSqlToSql(args.value['sql']);
                    break;
            }
        };
        QueryLibrary.prototype.getMongoFromRules = function (rule, mongoQuery) {
            mongoQuery = '{';
            if (rule.condition === 'or') {
                mongoQuery += '"$or":[';
                mongoQuery = this.convertMongoQuery(rule.rules, mongoQuery) + ']';
            }
            else {
                mongoQuery += '"$and":[';
                mongoQuery = this.convertMongoQuery(rule.rules, mongoQuery) + ']';
            }
            mongoQuery += '}';
            return mongoQuery;
        };
        QueryLibrary.prototype.getOperatorFromMongoOperator = function (operator) {
            var operatorValue;
            switch (operator) {
                case '$ne':
                    operatorValue = 'notequal';
                    break;
                case '$gt':
                    operatorValue = 'greaterthan';
                    break;
                case '$gte':
                    operatorValue = 'greaterthanorequal';
                    break;
                case '$lt':
                    operatorValue = 'lessthan';
                    break;
                case '$lte':
                    operatorValue = 'lessthanorequal';
                    break;
                case '$nin':
                    operatorValue = 'notin';
                    break;
            }
            return operatorValue;
        };
        QueryLibrary.prototype.convertMongoQuery = function (rules, mongoQuery) {
            var _this = this;
            var i = 0;
            rules.forEach(function (item) {
                i++;
                mongoQuery += '{';
                if (item.rules !== undefined) {
                    if (item.condition === 'or') {
                        mongoQuery += ' "$or":[';
                        mongoQuery = _this.convertMongoQuery(item.rules, mongoQuery) + ']';
                    }
                    else {
                        mongoQuery += ' "$and":[';
                        mongoQuery = _this.convertMongoQuery(item.rules, mongoQuery) + ']';
                    }
                }
                var itVal = item.type === 'string' && item.operator !== 'in' && item.operator !== 'notin' && item.value && item.value.trim() !== '' ? item.value.replace(/'/g, '\\') : '';
                if (item.type === 'string' && (item.operator === 'in' || item.operator === 'notin') && item.value && item.value.length === 1) {
                    itVal = item.value[0].replace(/'/g, '\\');
                }
                var field = item.field ? item.field.substring(0) : '';
                switch (item.operator) {
                    case 'contains':
                        mongoQuery += '"' + field + '":{"$regex":"' + itVal + '"}';
                        break;
                    case 'notcontains':
                        mongoQuery += '"' + field + '":{"$not":{"$regex":"' + item.value + '"}}';
                        break;
                    case 'startswith':
                        mongoQuery += '"' + field + '":{"$regex":"^' + itVal + '"}';
                        break;
                    case 'notstartswith':
                        mongoQuery += '"' + field + '":{"$not":{"$regex":"^' + item.value + '"}}';
                        break;
                    case 'endswith':
                        mongoQuery += '"' + field + '":{"$regex":"' + itVal + '$"}';
                        break;
                    case 'notendswith':
                        mongoQuery += '"' + field + '":{"$not":{"$regex":"' + item.value + '$"}}';
                        break;
                    case 'isnull':
                        mongoQuery += '"' + field + '": null';
                        break;
                    case 'isnotnull':
                        mongoQuery += '"' + field + '":{"$ne": null}';
                        break;
                    case 'isempty':
                        mongoQuery += '"' + field + '": ""';
                        break;
                    case 'isnotempty':
                        mongoQuery += '"' + field + '":{"$ne": ""}';
                        break;
                    case 'equal':
                        if (item.type === 'string') {
                            mongoQuery += '"' + field + '":"' + itVal + '"';
                        }
                        else if (item.type === 'date') {
                            mongoQuery += '"' + field + '":"' + item.value + '"';
                        }
                        else if (item.type === 'boolean') {
                            mongoQuery += '"' + field + '":' + item.value + '';
                        }
                        else {
                            mongoQuery += '"' + field + '":' + item.value + '';
                        }
                        break;
                    case 'notequal':
                        if (item.type === 'string') {
                            mongoQuery += '"' + field + '":{"$ne":"' + itVal + '"}';
                        }
                        else if (item.type === 'date') {
                            mongoQuery += '"' + field + '":{"$ne":"' + item.value + '"}';
                        }
                        else {
                            mongoQuery += '"' + field + '":{"$ne":' + item.value + '}';
                        }
                        break;
                    case 'in':
                        if (item.type === 'string') {
                            if (item.value.length > 1) {
                                var s = item.value.map(function (x, j) { return (j < item.value.length ? "\"" + x + "\"" : ''); }).toString();
                                s = s.endsWith(',') ? s.substring(0, s.length - 1) : s;
                                mongoQuery += '"' + field + '": { "$in": [' + s + ']}';
                            }
                            else {
                                mongoQuery += '"' + field + '": { "$in": ["' + itVal + '"]}';
                            }
                        }
                        else Eif (item.type === 'number') {
                            if (item.value.length > 1) {
                                mongoQuery += '"' + field + '": { "$in": [' + item.value.toString() + ']}';
                            }
                            else {
                                mongoQuery += '"' + field + '": { "$in": [' + item.value + ']}';
                            }
                        }
                        break;
                    case 'notin':
                        if (item.type === 'string') {
                            if (item.value.length > 1) {
                                var s = item.value.map(function (x, j) { return (j < item.value.length ? "\"" + x + "\"" : ''); }).toString();
                                s = s.endsWith(',') ? s.substring(0, s.length - 1) : s;
                                mongoQuery += '"' + field + '": { "$nin": [' + s + ']}';
                            }
                            else {
                                mongoQuery += '"' + field + '": { "$nin": ["' + itVal + '"]}';
                            }
                        }
                        else Eif (item.type === 'number') {
                            if (item.value.length > 1) {
                                mongoQuery += '"' + field + '": { "$nin": [' + item.value.toString() + ']}';
                            }
                            else {
                                mongoQuery += '"' + field + '": { "$nin": [' + item.value + ']}';
                            }
                        }
                        break;
                    case 'greaterthan':
                        if (item.type === 'number') {
                            mongoQuery += '"' + field + '": { "$gt": ' + item.value + '}';
                        }
                        else {
                            mongoQuery += '"' + field + '": { "$gt": "' + item.value + '"}';
                        }
                        break;
                    case 'greaterthanorequal':
                        if (item.type === 'number') {
                            mongoQuery += '"' + field + '": { "$gte": ' + item.value + '}';
                        }
                        else {
                            mongoQuery += '"' + field + '": { "$gte": "' + item.value + '"}';
                        }
                        break;
                    case 'between':
                        if (item.type === 'number') {
                            mongoQuery += '"' + field + '": {"$gte":' + item.value[0] + ', "$lte":' + item.value[1] + '}';
                        }
                        else {
                            mongoQuery += '"' + field + '": {"$gte": "' + item.value[0] + '", "$lte": "' + item.value[1] + '"}';
                        }
                        break;
                    case 'notbetween':
                        if (item.type === 'number') {
                            mongoQuery += '"$or":[{"' + field + '": {"$lt":' + item.value[0] + '}}, {"' + field + '": {"$gt":' + item.value[1] + '}}]';
                        }
                        else {
                            mongoQuery += '"$or":[{"' + field + '": {"$lt": "' + item.value[0] + '"}}, {"' + field + '": {"$gt": "' + item.value[1] + '"}}]';
                        }
                        break;
                    case 'lessthan':
                        if (item.type === 'number') {
                            mongoQuery += '"' + field + '": { "$lt": ' + item.value + '}';
                        }
                        else {
                            mongoQuery += '"' + field + '": { "$lt": "' + item.value + '"}';
                        }
                        break;
                    case 'lessthanorequal':
                        if (item.type === 'number') {
                            mongoQuery += '"' + field + '": { "$lte": ' + item.value + '}';
                        }
                        else {
                            mongoQuery += '"' + field + '": { "$lte": "' + item.value + '"}';
                        }
                        break;
                }
                mongoQuery += '}';
                if (rules.length !== i) {
                    mongoQuery += ',';
                }
            });
            return mongoQuery;
        };
        QueryLibrary.prototype.mongoParser = function (mongoQuery, rule, mongoLocale) {
            var mongoList;
            if (Object.keys(mongoQuery).indexOf('$and') > -1) {
                mongoList = mongoQuery['$and'];
                rule.condition = 'and';
            }
            else Eif (Object.keys(mongoQuery).indexOf('$or') > -1) {
                mongoList = mongoQuery['$or'];
                rule.condition = 'or';
            }
            rule.rules = [];
            this.mongoRecursion(mongoList, rule.rules, mongoLocale);
        };
        QueryLibrary.prototype.mongoRecursion = function (mongoList, rules, mongoLocale) {
            var operatorValue;
            var type;
            var stringValue;
            var key;
            var betweenValue;
            var fieldType;
            var condition;
            var value;
            var subRules;
            var rule;
            var keyObj;
            var ruleValue;
            for (var i = 0, len = mongoList.length; i < len; i++) {
                var betweenOperatorArray = [];
                var inOperatorArray = [];
                condition = Object.keys(mongoList[i])[0];
                value = mongoList[i][condition];
                if (condition === '$and') {
                    if (this.parent.enableNotCondition) {
                        subRules = { condition: condition.replace('$', ''), rules: [], not: false };
                    }
                    else {
                        subRules = { condition: condition.replace('$', ''), rules: [] };
                    }
                    rules.push(subRules);
                    this.mongoRecursion(mongoList[i][condition], rules[rules.length - 1].rules, mongoLocale);
                }
                else if (condition === '$or') {
                    var notBetween = void 0;
                    var innerObject = [];
                    var keys = [];
                    var firstKey = [];
                    var secondKey = [];
                    var innerKeys = [];
                    var firstValue = [];
                    var secondValue = [];
                    var innerFirstValue = [];
                    var innerSecondValue = [];
                    if (Array.isArray(value) && value.length === 2) {
                        keys = Object.keys(value);
                        innerFirstValue = value[keys[0]];
                        innerSecondValue = value[keys[1]];
                        Eif (typeof innerFirstValue === 'object') {
                            innerObject = Object.keys(innerFirstValue)[0];
                            innerKeys = Object.keys(innerFirstValue[Object.keys(innerFirstValue)[0]]);
                            firstKey = innerKeys[0];
                            secondKey = Object.keys(innerSecondValue[Object.keys(innerSecondValue)[0]])[0];
                            if (firstKey === '$lt' && secondKey === '$gt') {
                                operatorValue = 'notbetween';
                                firstValue = innerFirstValue[innerObject][firstKey];
                                secondValue = innerSecondValue[innerObject][secondKey];
                                type = typeof firstValue === 'number' ? 'number' : 'date';
                                ruleValue = [firstValue, secondValue];
                                rule = { field: innerObject, label: innerObject, value: ruleValue, operator: operatorValue, type: type };
                                rules.push(rule);
                                notBetween = true;
                            }
                        }
                    }
                    if (!notBetween) {
                        if (this.parent.enableNotCondition) {
                            subRules = { condition: condition.replace('$', ''), rules: [], not: false };
                        }
                        else {
                            subRules = { condition: condition.replace('$', ''), rules: [] };
                        }
                        rules.push(subRules);
                        this.mongoRecursion(mongoList[i][condition], rules[rules.length - 1].rules, mongoLocale);
                    }
                }
                else {
                    value = mongoList[i][condition];
                    if (value === null) {
                        operatorValue = 'isnull';
                    }
                    if (typeof value === 'boolean') {
                        operatorValue = 'equal';
                        type = 'boolean';
                        ruleValue = value;
                    }
                    if (typeof (value) === 'number') {
                        ruleValue = value;
                        type = 'number';
                        operatorValue = 'equal';
                    }
                    else if (typeof (value) === 'object' && value !== null) {
                        keyObj = Object.keys(value);
                        for (var i_1 = 0; i_1 < keyObj.length; i_1++) {
                            key = keyObj[i_1];
                            stringValue = (value)[keyObj[i_1]];
                            if (key === '$ne' && ej2_base_1.isNullOrUndefined(stringValue)) {
                                operatorValue = 'isnotnull';
                                ruleValue = null;
                            }
                            if (key === '$ne' && typeof stringValue === 'boolean') {
                                operatorValue = 'notequal';
                                ruleValue = stringValue;
                                type = 'boolean';
                            }
                            if (keyObj.length >= 2 && keyObj[i_1]) {
                                Iif (typeof (stringValue) == 'object') {
                                    operatorValue = 'notbetween';
                                    condition = Object.keys(stringValue)[0];
                                    betweenValue = [Object.keys(stringValue[condition])[0]];
                                    betweenOperatorArray.push(stringValue[condition][betweenValue]);
                                    type = 'number';
                                }
                                else {
                                    operatorValue = 'between';
                                    betweenOperatorArray.push(stringValue);
                                }
                                if (typeof (stringValue) === 'number') {
                                    type = 'number';
                                }
                            }
                            else if (typeof (stringValue) === 'object' && stringValue !== null) {
                                Iif (key === '$not' && Object.keys(stringValue)[0] === '$regex') {
                                    if (stringValue['$regex'].indexOf('^') > -1) {
                                        operatorValue = 'notstartswith';
                                        ruleValue = stringValue['$regex'].replace('^', '');
                                    }
                                    else if (stringValue['$regex'].indexOf('$') > -1) {
                                        operatorValue = 'notendswith';
                                        ruleValue = stringValue['$regex'].replace('$', '');
                                    }
                                    else {
                                        operatorValue = 'notcontains';
                                        ruleValue = stringValue['$regex'];
                                    }
                                }
                                else {
                                    operatorValue = key === '$in' ? 'in' : 'notin';
                                    inOperatorArray = stringValue;
                                    type = typeof (stringValue[0]) === 'number' ? 'number' : 'string';
                                }
                            }
                            else if (typeof (stringValue) === 'number') {
                                operatorValue = this.getOperatorFromMongoOperator(key);
                                type = 'number';
                                ruleValue = stringValue;
                            }
                            if (typeof (stringValue) === 'string') {
                                if (key === '$regex') {
                                    operatorValue = 'contains';
                                    ruleValue = stringValue;
                                    type = 'string';
                                }
                                if (key === '$ne') {
                                    if (stringValue !== null && stringValue.length > 0 && isNaN(Date.parse(stringValue))) {
                                        operatorValue = 'notequal';
                                        ruleValue = stringValue;
                                    }
                                    else Iif (ej2_base_1.isNullOrUndefined(stringValue)) {
                                        operatorValue = 'isnotnull';
                                        ruleValue = stringValue;
                                    }
                                    else Eif (stringValue === '') {
                                        operatorValue = 'isnotempty';
                                        ruleValue = stringValue;
                                    }
                                    type = 'string';
                                }
                                if (stringValue.indexOf('^') > -1) {
                                    operatorValue = 'startswith';
                                    ruleValue = stringValue.replace('^', '');
                                    type = 'string';
                                }
                                if (stringValue.indexOf('$') > -1 && key !== '$not') {
                                    operatorValue = 'endswith';
                                    ruleValue = stringValue.replace('$', '');
                                    type = 'string';
                                }
                                for (var _i = 0, _a = this.parent.columns; _i < _a.length; _i++) {
                                    var column = _a[_i];
                                    if (column.field === condition) {
                                        fieldType = column.type;
                                        break;
                                    }
                                }
                                if (!isNaN(Date.parse(stringValue)) || fieldType === 'date') {
                                    operatorValue = operatorValue || this.getOperatorFromMongoOperator(key);
                                    type = 'date';
                                    ruleValue = stringValue;
                                }
                            }
                        }
                    }
                    else if (value && typeof (value) === 'string' && !isNaN(Date.parse(value))) {
                        operatorValue = 'equal';
                        ruleValue = value;
                        type = 'date';
                    }
                    else if (typeof (value) === 'string' && value !== '' && value !== 'true' && value !== 'false') {
                        operatorValue = 'equal';
                        ruleValue = value;
                        type = 'string';
                    }
                    else if (typeof (value) === 'string' && value === '') {
                        operatorValue = 'isempty';
                        ruleValue = value;
                        type = 'string';
                    }
                    if (betweenOperatorArray && betweenOperatorArray.length > 1) {
                        rule = { field: condition, label: condition, value: betweenOperatorArray, operator: operatorValue, type: type };
                    }
                    else if (inOperatorArray && inOperatorArray.length > 1) {
                        rule = { field: condition, label: condition, value: inOperatorArray, operator: operatorValue, type: type };
                    }
                    else {
                        rule = { field: condition, label: condition, value: ruleValue, operator: operatorValue, type: type };
                    }
                    rules.push(rule);
                    operatorValue = '';
                }
            }
        };
        QueryLibrary.prototype.convertParamSqlToSql = function (sql) {
            var paramSql = sql.sql;
            var paramValues = sql.params;
            var parts = paramSql.split('?');
            var normalSql = parts[0];
            for (var i = 0; i < paramValues.length; i++) {
                normalSql += (typeof (paramValues[i]) === 'string' ? "'" + paramValues[i] + "'" + parts[i + 1] : paramValues[i] + parts[i + 1]);
            }
            Eif (normalSql.length >= 2 && normalSql[0] === '(' && normalSql[normalSql.length - 1] === ')') {
                normalSql = normalSql.slice(1, -1);
            }
            normalSql = normalSql.replace(/!= ''(?! =)/g, 'IS NOT EMPTY').replace(/= ''/g, 'IS EMPTY');
            return normalSql;
        };
        QueryLibrary.prototype.convertNamedParamSqlToSql = function (sql) {
            var namedParamSql = sql.sql;
            var params = sql.params;
            var normalSql = namedParamSql;
            Object.keys(params).forEach(function (paramName) {
                var paramValue = params[paramName];
                paramName = ':' + paramName;
                normalSql = normalSql.replace(paramName, typeof (paramValue) === 'string' ? "'" + paramValue + "'" : String(paramValue));
            });
            Eif (normalSql.length >= 2 && normalSql[0] === '(' && normalSql[normalSql.length - 1] === ')') {
                normalSql = normalSql.slice(1, -1);
            }
            normalSql = normalSql.replace(/!= ''(?! =)/g, 'IS NOT EMPTY').replace(/= ''/g, 'IS EMPTY');
            return normalSql;
        };
        QueryLibrary.prototype.getParameterSql = function (qbrule) {
            var qbRule = ej2_base_1.extend({}, qbrule, null, true);
            var value = this.updateRuleValue(qbRule, false);
            return this.getParameterSQLVal(this.parent.getSqlFromRules(qbRule), value['ruleVal']);
        };
        QueryLibrary.prototype.getNamedParameterSql = function (qbrule) {
            var qbRule = ej2_base_1.extend({}, qbrule, null, true);
            var value = this.updateRuleValue(qbRule, true);
            return this.getNamedParameterSQLVal(this.parent.getSqlFromRules(qbRule), value['namedRuleVal']);
        };
        QueryLibrary.prototype.getParameterSQLVal = function (content, ruleValue) {
            var replacedString = content.replace(/[%']/g, '');
            return { sql: '(' + replacedString + ')', params: ruleValue };
        };
        QueryLibrary.prototype.getNamedParameterSQLVal = function (content, ruleValue) {
            var replacedString = content.replace(/[%']/g, '');
            return { sql: '(' + replacedString + ')', params: ruleValue };
        };
        QueryLibrary.prototype.updateRuleValue = function (rule, isNamedParameter) {
            var ruleVal = [];
            var namedRuleVal = {};
            var namedParameters = [];
            return this.updateValue(rule.rules, isNamedParameter, ruleVal, namedRuleVal, namedParameters);
        };
        QueryLibrary.prototype.updateValue = function (rules, isNamedParameter, ruleVal, namedRuleVal, namedParameters) {
            Iif (ej2_base_1.isNullOrUndefined(rules)) {
                return { ruleVal: ruleVal, namedRuleVal: namedRuleVal };
            }
            for (var i = 0; i < rules.length; i++) {
                if (rules[i].rules) {
                    var value = this.updateValue(rules[i].rules, isNamedParameter, ruleVal, namedRuleVal, namedParameters);
                    ruleVal = value['ruleVal'];
                    namedRuleVal = value['namedRuleVal'];
                }
                else {
                    var namedField = void 0;
                    if (rules[i].value instanceof Array) {
                        for (var j = 0; j < (rules[i].value).length; j++) {
                            if (isNamedParameter) {
                                namedField = this.getNamedParameter(rules[i].field, namedParameters);
                            }
                            Eif (!ej2_base_1.isNullOrUndefined(rules[i].value[j])) {
                                if (rules[i].type === 'string' || rules[i].type === 'date') {
                                    if (isNamedParameter) {
                                        namedRuleVal[namedField] = rules[i].value[j];
                                    }
                                    else {
                                        ruleVal.push(rules[i].value[j]);
                                    }
                                }
                                else {
                                    if (isNamedParameter) {
                                        namedRuleVal[namedField] = rules[i].value[j];
                                    }
                                    else {
                                        ruleVal.push(rules[i].value[j]);
                                    }
                                }
                            }
                            if (isNamedParameter) {
                                rules[i].value[j] = ':' + namedField;
                            }
                            else {
                                rules[i].value[j] = '?';
                            }
                        }
                    }
                    else {
                        if (isNamedParameter) {
                            namedField = this.getNamedParameter(rules[i].field, namedParameters);
                        }
                        if (rules[i].operator.indexOf('null') < 1) {
                            Eif (rules[i].type !== 'string' || (rules[i].type === 'string' && (rules[i].value !== '' || rules[i].value === 0))) {
                                if (rules[i].type === 'string' || rules[i].type === 'date') {
                                    Eif (rules[i].operator.indexOf('empty') < 1) {
                                        var value = rules[i].value.toString();
                                        switch (rules[i].operator) {
                                            case 'startswith':
                                            case 'notstartswith':
                                                value = value + '%';
                                                break;
                                            case 'endswith':
                                            case 'notendswith':
                                                value = '%' + value;
                                                break;
                                            case 'contains':
                                            case 'notcontains':
                                                value = '%' + value + '%';
                                                break;
                                        }
                                        if (isNamedParameter) {
                                            namedRuleVal[namedField] = value;
                                        }
                                        else {
                                            ruleVal.push(value);
                                        }
                                    }
                                    else {
                                        if (isNamedParameter) {
                                            namedRuleVal[namedField] = '';
                                        }
                                        else {
                                            ruleVal.push('');
                                        }
                                        if (rules[i].operator === 'isempty') {
                                            rules[i].operator = 'equal';
                                        }
                                        else {
                                            rules[i].operator = 'notequal';
                                        }
                                    }
                                }
                                else {
                                    Eif (!ej2_base_1.isNullOrUndefined(rules[i].value)) {
                                        Iif (isNamedParameter) {
                                            namedRuleVal[namedField] = rules[i].value;
                                        }
                                        else {
                                            ruleVal.push(rules[i].value);
                                        }
                                    }
                                }
                                if (isNamedParameter) {
                                    rules[i].value = ':' + namedField;
                                }
                                else {
                                    rules[i].value = '?';
                                }
                            }
                        }
                    }
                }
            }
            return { ruleVal: ruleVal, namedRuleVal: namedRuleVal };
        };
        QueryLibrary.prototype.getNamedParameter = function (field, namedParameters) {
            var newField = null;
            if (namedParameters.length > 0) {
                for (var i = namedParameters.length - 1; i >= 0; i--) {
                    var currField = namedParameters[i];
                    if (currField.indexOf(field) > -1) {
                        var idx = parseInt(currField.split('_')[1], 10) + 1;
                        newField = field + '_' + idx;
                        namedParameters.push(newField);
                        break;
                    }
                }
            }
            if (!newField) {
                newField = field + '_1';
                namedParameters.push(newField);
            }
            return newField;
        };
        QueryLibrary.prototype.getModuleName = function () {
            return 'query-library';
        };
        return QueryLibrary;
    }());
    exports.QueryLibrary = QueryLibrary;
});