|
1 | | -var {QueryField, QueryEntity, QueryUtils, MethodCallExpression, MemberExpression} = require('@themost/query'); |
| 1 | +var {QueryField, QueryEntity, QueryUtils, MethodCallExpression, MemberExpression, ObjectNameValidator} = require('@themost/query'); |
2 | 2 | var {sprintf} = require('sprintf-js'); |
3 | 3 | var _ = require('lodash'); |
4 | 4 | var {DataError} = require('@themost/common'); |
@@ -423,114 +423,33 @@ DataAttributeResolver.prototype.testAggregatedNestedAttribute = function(s) { |
423 | 423 |
|
424 | 424 | /** |
425 | 425 | * @param {string} s |
426 | | - * @returns {*} |
| 426 | + * @returns {{name: string, property?: string}|null} |
427 | 427 | */ |
428 | 428 | DataAttributeResolver.prototype.testNestedAttribute = function(s) { |
429 | 429 | if (typeof s !== 'string') |
430 | 430 | return null; |
431 | | - /** |
432 | | - * @private |
433 | | - */ |
434 | 431 | var matches; |
435 | | - /** |
436 | | - * nested attribute aggregate function with alias e.g. f(x/b) as a |
437 | | - * @ignore |
438 | | - */ |
439 | | - matches = /^(\w+)\((\w+)\/(\w+)\)\sas\s([\u0020-\u007F\u0080-\uFFFF]+)$/i.exec(s); |
440 | | - if (matches) { |
441 | | - return { name: matches[1] + '(' + matches[2] + '/' + matches[3] + ')', property:matches[4] }; |
442 | | - } |
443 | | - /** |
444 | | - * nested attribute aggregate function with alias e.g. f(x/b/c) as a |
445 | | - * @ignore |
446 | | - */ |
447 | | - matches = /^(\w+)\((\w+)\/(\w+)\/(\w+)\)\sas\s([\u0020-\u007F\u0080-\uFFFF]+)$/i.exec(s); |
448 | | - if (matches) { |
449 | | - return { name: matches[1] + '(' + matches[2] + '/' + matches[3] + '/' + matches[4] + ')', property:matches[5] }; |
450 | | - } |
451 | | - /** |
452 | | - * nested attribute aggregate function with alias e.g. f(x/b/c/d) as a |
453 | | - * @ignore |
454 | | - */ |
455 | | - matches = /^(\w+)\((\w+)\/(\w+)\/(\w+)\/(\w+)\)\sas\s([\u0020-\u007F\u0080-\uFFFF]+)$/i.exec(s); |
456 | | - if (matches) { |
457 | | - return { name: matches[1] + '(' + matches[2] + '/' + matches[3] + '/' + matches[4] + '/' + matches[5] + ')', property:matches[6] }; |
458 | | - } |
459 | | - /** |
460 | | - * nested attribute with alias e.g. x/b as a |
461 | | - * @ignore |
462 | | - */ |
463 | | - matches = /^(\w+)\/(\w+)\sas\s([\u0020-\u007F\u0080-\uFFFF]+)$/i.exec(s); |
464 | | - if (matches) { |
465 | | - return { name: matches[1] + '/' + matches[2], property:matches[3] }; |
466 | | - } |
467 | | - /** |
468 | | - * nested attribute with alias e.g. x/b/c as a |
469 | | - * @ignore |
470 | | - */ |
471 | | - matches = /^(\w+)\/(\w+)\/(\w+)\sas\s([\u0020-\u007F\u0080-\uFFFF]+)$/i.exec(s); |
472 | | - if (matches) { |
473 | | - return { name: matches[1] + '/' + matches[2] + '/' + matches[3], property:matches[4] }; |
474 | | - } |
475 | | - /** |
476 | | - * nested attribute with alias e.g. x/b/c/d as a |
477 | | - * @ignore |
478 | | - */ |
479 | | - matches = /^(\w+)\/(\w+)\/(\w+)\/(\w+)\sas\s([\u0020-\u007F\u0080-\uFFFF]+)$/i.exec(s); |
480 | | - if (matches) { |
481 | | - return { name: matches[1] + '/' + matches[2] + '/' + matches[3] + '/' + matches[4], property:matches[5] }; |
482 | | - } |
483 | | - /** |
484 | | - * nested attribute aggregate function with alias e.g. f(x/b) |
485 | | - * @ignore |
486 | | - */ |
487 | | - matches = /^(\w+)\((\w+)\/(\w+)\)$/i.exec(s); |
488 | | - if (matches) { |
489 | | - return { name: matches[1] + '(' + matches[2] + '/' + matches[3] + ')' }; |
490 | | - } |
491 | | - /** |
492 | | - * nested attribute aggregate function with alias e.g. f(x/b/c) |
493 | | - * @ignore |
494 | | - */ |
495 | | - matches = /^(\w+)\((\w+)\/(\w+)\/(\w+)\)$/i.exec(s); |
496 | | - if (matches) { |
497 | | - return { name: matches[1] + '(' + matches[2] + '/' + matches[3] + '/' + matches[4] + ')' }; |
498 | | - } |
499 | | - /** |
500 | | - * nested attribute aggregate function with alias e.g. f(x/b/c/d) |
501 | | - * @ignore |
502 | | - */ |
503 | | - matches = /^(\w+)\((\w+)\/(\w+)\/(\w+)\/(\w+)\)$/i.exec(s); |
504 | | - if (matches) { |
505 | | - return { name: matches[1] + '(' + matches[2] + '/' + matches[3] + '/' + matches[4] + matches[5] + ')' }; |
506 | | - } |
507 | | - /** |
508 | | - * nested attribute with alias e.g. x/b |
509 | | - * @ignore |
510 | | - */ |
511 | | - matches = /^(\w+)\/(\w+)$/.exec(s); |
512 | | - if (matches) { |
513 | | - return { name: s }; |
514 | | - } |
515 | 432 |
|
516 | | - /** |
517 | | - * nested attribute with alias e.g. x/b/c |
518 | | - * @ignore |
519 | | - */ |
520 | | - matches = /^(\w+)\/(\w+)\/(\w+)$/.exec(s); |
| 433 | + var pattern = (ObjectNameValidator.validator && ObjectNameValidator.validator.pattern) || new RegExp(ObjectNameValidator.Patterns.Default); |
| 434 | + var exprFuncWithAlias = new RegExp('^(\\w+)\\((\\w+(?:\\/\\w+)+)\\)(?:\\s+as\\s+' + pattern.source + ')?$'); |
| 435 | + matches = exprFuncWithAlias.exec(s); |
521 | 436 | if (matches) { |
522 | | - return { name: s }; |
| 437 | + // matches[1]: the function name |
| 438 | + // matches[2]: the nested attribute |
| 439 | + // matches[3]: the alias (optional) |
| 440 | + return { name: matches[2], property: matches[3] }; |
523 | 441 | } |
524 | 442 |
|
| 443 | + var exprWithAlias = new RegExp('^(\\w+(?:\\/\\w+)+)(\\s+as\\s+' + pattern.source + ')?$') |
525 | 444 | /** |
526 | | - * nested attribute with alias e.g. x/b/c/d |
527 | | - * @ignore |
| 445 | + * nested attribute with alias e.g. a/b/../c as a |
528 | 446 | */ |
529 | | - matches = /^(\w+)\/(\w+)\/(\w+)\/(\w+)$/.exec(s); |
| 447 | + matches = exprWithAlias.exec(s); |
530 | 448 | if (matches) { |
531 | | - return { name: s }; |
| 449 | + // matches[2]: the nested attribute |
| 450 | + // matches[3]: the alias (optional) |
| 451 | + return { name: matches[1], property: matches[3] }; |
532 | 452 | } |
533 | | - |
534 | 453 | }; |
535 | 454 |
|
536 | 455 | /** |
|
0 commit comments