@sap/odata-v4
Version:
OData V4.0 server library
148 lines (139 loc) • 5.06 kB
JavaScript
'use strict';
const Expression = require('./Expression');
const EdmPrimitiveTypeKind = require('../edm/EdmPrimitiveTypeKind');
/**
* @extends Expression
* @hideconstructor
*/
class MethodExpression extends Expression {
/**
* Create an instance of MethodExpression.
* @param {MethodExpression.MethodKind} methodKind method
* @param {UriParameter[]} parameters parameters of the method (can be empty but should not be null)
*/
constructor(methodKind, parameters) {
super(Expression.ExpressionKind.METHOD);
/**
* @type {MethodExpression.MethodKind}
* @private
*/
this._methodKind = methodKind;
/**
* @type {UriParameter[]}
* @private
*/
this._parameters = parameters;
}
/**
* Return the method.
* @returns {MethodExpression.MethodKind} the method
*/
getMethod() {
return this._methodKind;
}
/**
* Return the method parameters.
* @returns {UriParameter[]} the method parameters (can be empty but not null)
*/
getParameters() {
return this._parameters;
}
/**
* Return the EDM type of this expression or null if there is none.
* @returns {?EdmType} the EDM type of this expression or null if there is none
*/
getType() {
switch (this._methodKind) {
case MethodExpression.MethodKind.CONTAINS:
case MethodExpression.MethodKind.STARTSWITH:
case MethodExpression.MethodKind.ENDSWITH:
return EdmPrimitiveTypeKind.Boolean;
case MethodExpression.MethodKind.LENGTH:
case MethodExpression.MethodKind.INDEXOF:
return EdmPrimitiveTypeKind.Int32;
case MethodExpression.MethodKind.SUBSTRING:
case MethodExpression.MethodKind.TOLOWER:
case MethodExpression.MethodKind.TOUPPER:
case MethodExpression.MethodKind.TRIM:
case MethodExpression.MethodKind.CONCAT:
return EdmPrimitiveTypeKind.String;
case MethodExpression.MethodKind.YEAR:
case MethodExpression.MethodKind.MONTH:
case MethodExpression.MethodKind.DAY:
case MethodExpression.MethodKind.HOUR:
case MethodExpression.MethodKind.MINUTE:
case MethodExpression.MethodKind.SECOND:
return EdmPrimitiveTypeKind.Int32;
case MethodExpression.MethodKind.FRACTIONALSECONDS:
case MethodExpression.MethodKind.TOTALSECONDS:
return EdmPrimitiveTypeKind.Decimal;
case MethodExpression.MethodKind.DATE:
return EdmPrimitiveTypeKind.Date;
case MethodExpression.MethodKind.TIME:
return EdmPrimitiveTypeKind.TimeOfDay;
case MethodExpression.MethodKind.TOTALOFFSETMINUTES:
return EdmPrimitiveTypeKind.Int32;
case MethodExpression.MethodKind.MINDATETIME:
case MethodExpression.MethodKind.MAXDATETIME:
case MethodExpression.MethodKind.NOW:
return EdmPrimitiveTypeKind.DateTimeOffset;
case MethodExpression.MethodKind.ROUND:
case MethodExpression.MethodKind.FLOOR:
case MethodExpression.MethodKind.CEILING:
// Needs to be refined if Decimal must be distinguished from Double.
return EdmPrimitiveTypeKind.Double;
case MethodExpression.MethodKind.GEODISTANCE:
case MethodExpression.MethodKind.GEOLENGTH:
return EdmPrimitiveTypeKind.Double;
case MethodExpression.MethodKind.GEOINTERSECTS:
return EdmPrimitiveTypeKind.Boolean;
case MethodExpression.MethodKind.CAST:
return this._parameters[this._parameters.length - 1].getType();
case MethodExpression.MethodKind.ISOF:
return EdmPrimitiveTypeKind.Boolean;
default:
return null;
}
}
}
/**
* Supported methods
* For the semantics of these methods please see the ODATA specification for URL conventions.
* @enum {string}
* @readonly
*/
MethodExpression.MethodKind = {
CONTAINS: 'contains',
STARTSWITH: 'startswith',
ENDSWITH: 'endswith',
LENGTH: 'length',
INDEXOF: 'indexof',
SUBSTRING: 'substring',
TOLOWER: 'tolower',
TOUPPER: 'toupper',
TRIM: 'trim',
CONCAT: 'concat',
YEAR: 'year',
MONTH: 'month',
DAY: 'day',
HOUR: 'hour',
MINUTE: 'minute',
SECOND: 'second',
FRACTIONALSECONDS: 'fractionalseconds',
TOTALSECONDS: 'totalseconds',
DATE: 'date',
TIME: 'time',
TOTALOFFSETMINUTES: 'totaloffsetminutes',
MINDATETIME: 'mindatetime',
MAXDATETIME: 'maxdatetime',
NOW: 'now',
ROUND: 'round',
FLOOR: 'floor',
CEILING: 'ceiling',
GEODISTANCE: 'geo.distance',
GEOLENGTH: 'geo.length',
GEOINTERSECTS: 'geo.intersects',
CAST: 'cast',
ISOF: 'isof'
};
module.exports = MethodExpression;