Skip to content

Commit

Permalink
Use sync open data parser (#113)
Browse files Browse the repository at this point in the history
* implement sync open data parser

* implement expression as super class (#112)

* update type declarations

* 2.5.30
  • Loading branch information
kbarbounakis authored Oct 28, 2024
1 parent 8a3ec6c commit d749752
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 18 deletions.
5 changes: 0 additions & 5 deletions expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ MethodCallExpression.prototype.exprOf = function() {
var name = '$'.concat(this.name);
//set arguments array
method[name] = [] ;
if (this.args.length===0)
throw new Error('Unsupported method expression. Method arguments cannot be empty.');
method[name].push.apply(method[name], this.args.map(function (arg) {
if (typeof arg.exprOf === 'function') {
return arg.exprOf();
Expand Down Expand Up @@ -306,9 +304,6 @@ LangUtils.inherits(SimpleMethodCallExpression, MethodCallExpression);
SimpleMethodCallExpression.prototype.exprOf = function() {
var method = {};
var name = '$'.concat(this.name);
//set arguments array
if (this.args.length === 0)
throw new Error('Unsupported method expression. Method arguments cannot be empty.');
if (this.args.length === 1) {
method[name] = {};
var arg;
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./expressions";
export * from "./query";
export * from "./utils";
export * from "./object-name.validator";
export * from "./simple-open-data-parser";
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var _expressions = require("./expressions");
var _query = require("./query");
var _utils = require("./utils");
var _validator = require("./object-name.validator");
var { SimpleOpenDataParser } = require("./simple-open-data-parser");

module.exports.SqlFormatter = _formatter.SqlFormatter;

Expand Down Expand Up @@ -58,3 +59,5 @@ module.exports.SqlUtils = _utils.SqlUtils;

module.exports.ObjectNameValidator = _validator.ObjectNameValidator;
module.exports.InvalidObjectNameError = _validator.InvalidObjectNameError;

module.exports.SimpleOpenDataParser = SimpleOpenDataParser;
5 changes: 3 additions & 2 deletions odata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export declare interface OperatorType {
}

export declare class Token {
syntax: string;
constructor(tokenType: string);

static TokenType: TokenType;
Expand Down Expand Up @@ -108,7 +109,7 @@ export declare class OpenDataParser {

parse(str: string, callback: (err?: Error, res?: any) => void);
parseAsync(str: string): Promise<any>;
getOperator(token: string): string;
getOperator(token: Token | IdentifierToken | SyntaxToken | LiteralToken): string;
moveNext();
expect();
expectAny();
Expand Down Expand Up @@ -190,4 +191,4 @@ export declare class OpenDataParser {
}): Promise<any>;


}
}
19 changes: 11 additions & 8 deletions odata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ var _ = require("lodash");
var {trim} = require('lodash');
var {LangUtils} = require("@themost/common");
var {sprintf} = require('sprintf-js');
var {SwitchExpression, SelectAnyExpression, OrderByAnyExpression, isLogicalOperator,
var {SwitchExpression, SelectAnyExpression, OrderByAnyExpression, AnyExpressionFormatter, isLogicalOperator,
createLogicalExpression, isArithmeticOperator, createArithmeticExpression,
isArithmeticExpression, isLogicalExpression, isComparisonOperator,
createMemberExpression,
createComparisonExpression, isMethodCallExpression, isMemberExpression} = require('./expressions');
var {whilst} = require('async');
var {whilst, series} = require('async');
const { MethodCallExpression } = require('./expressions');
/**
* @class
Expand Down Expand Up @@ -36,7 +36,7 @@ function OpenDataParser() {
this.tokens = [];
/**
* Gets current token
* @type {Token}
* @type {Token | IdentifierToken | LiteralToken | SyntaxToken}
*/
this.currentToken = undefined;
/**
Expand Down Expand Up @@ -84,7 +84,7 @@ OpenDataParser.create = function() {

/**
* Gets the logical or arithmetic operator of the given token
* @param token
* @param {Token | IdentifierToken} token
*/
OpenDataParser.prototype.getOperator = function(token) {
if (token.type===Token.TokenType.Identifier) {
Expand Down Expand Up @@ -485,7 +485,7 @@ OpenDataParser.prototype.parseOrderBySequenceAsync = function(str) {

/**
* @param {{$select?:string,$filter?:string,$orderBy?:string,$groupBy?:string,$top:number,$skip:number}} queryOptions
* @param {function(Error,*)} callback
* @param {function(Error,*=)} callback
*/
OpenDataParser.prototype.parseQueryOptions = function(queryOptions, callback) {
const self = this;
Expand Down Expand Up @@ -588,7 +588,7 @@ OpenDataParser.prototype.parseCommon = function(callback) {
if (self.atEnd()) {
callback.call(self, null, result);
}
//method call exception for [,] or [)] tokens e.g indexOf(Title,'...')
//method call exception for "," or "()" tokens e.g indexOf(Title,'...')
else if ((self.currentToken.syntax===SyntaxToken.Comma.syntax) ||
(self.currentToken.syntax===SyntaxToken.ParenClose.syntax)) {
callback.call(self, null, result);
Expand All @@ -600,7 +600,7 @@ OpenDataParser.prototype.parseCommon = function(callback) {
}
else {
self.moveNext();
// if current operator is a logical operator ($or, $and etc)
// if current operator is a logical operator ($or, $and etc.)
// parse right operand by using parseCommon() method
// important note: the current expression probably is not using parentheses
// e.g. (category eq 'Laptops' or category eq 'Desktops') and round(price,2) ge 500 and round(price,2) le 1000
Expand Down Expand Up @@ -903,7 +903,7 @@ OpenDataParser.prototype.parseMember = function(callback) {
//format identifier
identifier += '/' + this.currentToken.identifier;
}
//support member to member comparison (with $it identifier e.g. $it/address/city or $it/category etc)
//support member to member comparison (with $it identifier e.g. $it/address/city or $it/category etc.)
if (/^\$it\//.test(identifier)) {
identifier= identifier.replace(/^\$it\//,'');
}
Expand Down Expand Up @@ -1577,6 +1577,7 @@ Token.Operator ={
*/
function LiteralToken(value, literalType)
{
// noinspection JSUnresolvedReference
LiteralToken.super_.call(this, Token.TokenType.Literal);
this.value = value;
this.literalType = literalType;
Expand Down Expand Up @@ -1623,6 +1624,7 @@ LiteralToken.Null = new LiteralToken(null, LiteralToken.LiteralType.Null);
*/
function IdentifierToken(name)
{
// noinspection JSUnresolvedReference
IdentifierToken.super_.call(this, Token.TokenType.Identifier);
this.identifier = name;
}
Expand All @@ -1639,6 +1641,7 @@ IdentifierToken.prototype.valueOf = function() {
*/
function SyntaxToken(chr)
{
// noinspection JSUnresolvedReference
SyntaxToken.super_.call(this, Token.TokenType.Syntax);
this.syntax = chr;
}
Expand Down
22 changes: 20 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "@themost/query",
"version": "2.5.29",
"version": "2.5.30",
"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.",
"main": "index.js",
"scripts": {
"test": "jest"
},
"engines": {
"node": ">=14"
},
"peerDependencies": {
"@themost/common": "^2.5.0"
},
Expand Down
17 changes: 17 additions & 0 deletions simple-open-data-parser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SyncSeriesEventEmitter } from "@themost/events";
import { ExpressionBase, OrderByAnyExpression } from "./expressions";
import { OpenDataParser } from "./odata";

export declare class SimpleOpenDataParser extends OpenDataParser {

resolvingMember: SyncSeriesEventEmitter<{target: SimpleOpenDataParser, member: string}>;
resolvingMethod: SyncSeriesEventEmitter<{target: SimpleOpenDataParser, method: string}>;

parseSync(data: string): ExpressionBase;
parseSelectSequenceSync(str: string): Array<ExpressionBase>;
parseOrderBySequenceSync(str: string): Array<OrderByAnyExpression>;
parseGroupBySequenceSync(str: string): Array<ExpressionBase>;
parseQueryOptionsSync(queryOptions: { $select?: string; $filter?: string; $expand?: string; $groupBy?: string; $orderBy?: string; $levels?: any; $top?: any; $skip?: any; }): any;
parseExpandSequenceSync(str: string): Array<{ name: string; options: { $select?: string; $filter?: string; $expand?: string; $groupBy?: string; $orderBy?: string; $levels?: any; $top?: any; $skip?: any; }; }>;

}
Loading

0 comments on commit d749752

Please sign in to comment.