Skip to content

Commit d749752

Browse files
authored
Use sync open data parser (#113)
* implement sync open data parser * implement expression as super class (#112) * update type declarations * 2.5.30
1 parent 8a3ec6c commit d749752

10 files changed

+639
-18
lines changed

expressions.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ MethodCallExpression.prototype.exprOf = function() {
201201
var name = '$'.concat(this.name);
202202
//set arguments array
203203
method[name] = [] ;
204-
if (this.args.length===0)
205-
throw new Error('Unsupported method expression. Method arguments cannot be empty.');
206204
method[name].push.apply(method[name], this.args.map(function (arg) {
207205
if (typeof arg.exprOf === 'function') {
208206
return arg.exprOf();
@@ -306,9 +304,6 @@ LangUtils.inherits(SimpleMethodCallExpression, MethodCallExpression);
306304
SimpleMethodCallExpression.prototype.exprOf = function() {
307305
var method = {};
308306
var name = '$'.concat(this.name);
309-
//set arguments array
310-
if (this.args.length === 0)
311-
throw new Error('Unsupported method expression. Method arguments cannot be empty.');
312307
if (this.args.length === 1) {
313308
method[name] = {};
314309
var arg;

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from "./expressions";
66
export * from "./query";
77
export * from "./utils";
88
export * from "./object-name.validator";
9+
export * from "./simple-open-data-parser";

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var _expressions = require("./expressions");
66
var _query = require("./query");
77
var _utils = require("./utils");
88
var _validator = require("./object-name.validator");
9+
var { SimpleOpenDataParser } = require("./simple-open-data-parser");
910

1011
module.exports.SqlFormatter = _formatter.SqlFormatter;
1112

@@ -58,3 +59,5 @@ module.exports.SqlUtils = _utils.SqlUtils;
5859

5960
module.exports.ObjectNameValidator = _validator.ObjectNameValidator;
6061
module.exports.InvalidObjectNameError = _validator.InvalidObjectNameError;
62+
63+
module.exports.SimpleOpenDataParser = SimpleOpenDataParser;

odata.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export declare interface OperatorType {
2727
}
2828

2929
export declare class Token {
30+
syntax: string;
3031
constructor(tokenType: string);
3132

3233
static TokenType: TokenType;
@@ -108,7 +109,7 @@ export declare class OpenDataParser {
108109

109110
parse(str: string, callback: (err?: Error, res?: any) => void);
110111
parseAsync(str: string): Promise<any>;
111-
getOperator(token: string): string;
112+
getOperator(token: Token | IdentifierToken | SyntaxToken | LiteralToken): string;
112113
moveNext();
113114
expect();
114115
expectAny();
@@ -190,4 +191,4 @@ export declare class OpenDataParser {
190191
}): Promise<any>;
191192

192193

193-
}
194+
}

odata.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ var _ = require("lodash");
33
var {trim} = require('lodash');
44
var {LangUtils} = require("@themost/common");
55
var {sprintf} = require('sprintf-js');
6-
var {SwitchExpression, SelectAnyExpression, OrderByAnyExpression, isLogicalOperator,
6+
var {SwitchExpression, SelectAnyExpression, OrderByAnyExpression, AnyExpressionFormatter, isLogicalOperator,
77
createLogicalExpression, isArithmeticOperator, createArithmeticExpression,
88
isArithmeticExpression, isLogicalExpression, isComparisonOperator,
99
createMemberExpression,
1010
createComparisonExpression, isMethodCallExpression, isMemberExpression} = require('./expressions');
11-
var {whilst} = require('async');
11+
var {whilst, series} = require('async');
1212
const { MethodCallExpression } = require('./expressions');
1313
/**
1414
* @class
@@ -36,7 +36,7 @@ function OpenDataParser() {
3636
this.tokens = [];
3737
/**
3838
* Gets current token
39-
* @type {Token}
39+
* @type {Token | IdentifierToken | LiteralToken | SyntaxToken}
4040
*/
4141
this.currentToken = undefined;
4242
/**
@@ -84,7 +84,7 @@ OpenDataParser.create = function() {
8484

8585
/**
8686
* Gets the logical or arithmetic operator of the given token
87-
* @param token
87+
* @param {Token | IdentifierToken} token
8888
*/
8989
OpenDataParser.prototype.getOperator = function(token) {
9090
if (token.type===Token.TokenType.Identifier) {
@@ -485,7 +485,7 @@ OpenDataParser.prototype.parseOrderBySequenceAsync = function(str) {
485485

486486
/**
487487
* @param {{$select?:string,$filter?:string,$orderBy?:string,$groupBy?:string,$top:number,$skip:number}} queryOptions
488-
* @param {function(Error,*)} callback
488+
* @param {function(Error,*=)} callback
489489
*/
490490
OpenDataParser.prototype.parseQueryOptions = function(queryOptions, callback) {
491491
const self = this;
@@ -588,7 +588,7 @@ OpenDataParser.prototype.parseCommon = function(callback) {
588588
if (self.atEnd()) {
589589
callback.call(self, null, result);
590590
}
591-
//method call exception for [,] or [)] tokens e.g indexOf(Title,'...')
591+
//method call exception for "," or "()" tokens e.g indexOf(Title,'...')
592592
else if ((self.currentToken.syntax===SyntaxToken.Comma.syntax) ||
593593
(self.currentToken.syntax===SyntaxToken.ParenClose.syntax)) {
594594
callback.call(self, null, result);
@@ -600,7 +600,7 @@ OpenDataParser.prototype.parseCommon = function(callback) {
600600
}
601601
else {
602602
self.moveNext();
603-
// if current operator is a logical operator ($or, $and etc)
603+
// if current operator is a logical operator ($or, $and etc.)
604604
// parse right operand by using parseCommon() method
605605
// important note: the current expression probably is not using parentheses
606606
// e.g. (category eq 'Laptops' or category eq 'Desktops') and round(price,2) ge 500 and round(price,2) le 1000
@@ -903,7 +903,7 @@ OpenDataParser.prototype.parseMember = function(callback) {
903903
//format identifier
904904
identifier += '/' + this.currentToken.identifier;
905905
}
906-
//support member to member comparison (with $it identifier e.g. $it/address/city or $it/category etc)
906+
//support member to member comparison (with $it identifier e.g. $it/address/city or $it/category etc.)
907907
if (/^\$it\//.test(identifier)) {
908908
identifier= identifier.replace(/^\$it\//,'');
909909
}
@@ -1577,6 +1577,7 @@ Token.Operator ={
15771577
*/
15781578
function LiteralToken(value, literalType)
15791579
{
1580+
// noinspection JSUnresolvedReference
15801581
LiteralToken.super_.call(this, Token.TokenType.Literal);
15811582
this.value = value;
15821583
this.literalType = literalType;
@@ -1623,6 +1624,7 @@ LiteralToken.Null = new LiteralToken(null, LiteralToken.LiteralType.Null);
16231624
*/
16241625
function IdentifierToken(name)
16251626
{
1627+
// noinspection JSUnresolvedReference
16261628
IdentifierToken.super_.call(this, Token.TokenType.Identifier);
16271629
this.identifier = name;
16281630
}
@@ -1639,6 +1641,7 @@ IdentifierToken.prototype.valueOf = function() {
16391641
*/
16401642
function SyntaxToken(chr)
16411643
{
1644+
// noinspection JSUnresolvedReference
16421645
SyntaxToken.super_.call(this, Token.TokenType.Syntax);
16431646
this.syntax = chr;
16441647
}

package-lock.json

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "@themost/query",
3-
"version": "2.5.29",
3+
"version": "2.5.30",
44
"description": "@themost/query is a query builder for SQL. It includes a wide variety of helper functions for building complex SQL queries under node.js.",
55
"main": "index.js",
66
"scripts": {
77
"test": "jest"
88
},
9+
"engines": {
10+
"node": ">=14"
11+
},
912
"peerDependencies": {
1013
"@themost/common": "^2.5.0"
1114
},

simple-open-data-parser.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SyncSeriesEventEmitter } from "@themost/events";
2+
import { ExpressionBase, OrderByAnyExpression } from "./expressions";
3+
import { OpenDataParser } from "./odata";
4+
5+
export declare class SimpleOpenDataParser extends OpenDataParser {
6+
7+
resolvingMember: SyncSeriesEventEmitter<{target: SimpleOpenDataParser, member: string}>;
8+
resolvingMethod: SyncSeriesEventEmitter<{target: SimpleOpenDataParser, method: string}>;
9+
10+
parseSync(data: string): ExpressionBase;
11+
parseSelectSequenceSync(str: string): Array<ExpressionBase>;
12+
parseOrderBySequenceSync(str: string): Array<OrderByAnyExpression>;
13+
parseGroupBySequenceSync(str: string): Array<ExpressionBase>;
14+
parseQueryOptionsSync(queryOptions: { $select?: string; $filter?: string; $expand?: string; $groupBy?: string; $orderBy?: string; $levels?: any; $top?: any; $skip?: any; }): any;
15+
parseExpandSequenceSync(str: string): Array<{ name: string; options: { $select?: string; $filter?: string; $expand?: string; $groupBy?: string; $orderBy?: string; $levels?: any; $top?: any; $skip?: any; }; }>;
16+
17+
}

0 commit comments

Comments
 (0)