Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/composer-common/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
out
lib/introspect/parser.js
lib/acl/parser.js
lib/query/parser.js
test/data
108 changes: 108 additions & 0 deletions packages/composer-common/lib/query/orderby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const IllegalModelException = require('../introspect/illegalmodelexception');
const Sort = require('./sort');

/**
* Defines the ORDER BY specification for a SELECT statement
*
* @private
* @class
* @memberof module:composer-common
*/
class OrderBy {

/**
* Create an OrderBy from an Abstract Syntax Tree. The AST is the
* result of parsing.
*
* @param {Select} select - the Select for this order by
* @param {string} ast - the AST created by the parser
* @throws {IllegalModelException}
*/
constructor(select, ast) {
if(!select || !ast) {
throw new IllegalModelException('Invalid Select or AST');
}

this.ast = ast;
this.select = select;
this.process();
}

/**
* Visitor design pattern
* @param {Object} visitor - the visitor
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
accept(visitor,parameters) {
return visitor.visit(this, parameters);
}

/**
* Returns the Select that owns this OrderBy.
*
* @return {Select} the owning Select
*/
getSelect() {
return this.select;
}

/**
* Process the AST and build the model
*
* @throws {IllegalModelException}
* @private
*/
process() {
this.sortCriteria = null;

if(this.ast.sort) {
this.sortCriteria = [];

for(let n=0; n < this.ast.sort.length; n++) {
this.sortCriteria.push( new Sort(this, this.ast.sort[n]));
}
}
}

/**
* Semantic validation of the structure of this select.
*
* @throws {IllegalModelException}
* @private
*/
validate() {
// TODO (DCS) check that the fields we are sorting by exist!
}

/**
* Returns a new object representing this Query that is
* suitable for serializing as JSON.
* @return {Object} A new object suitable for serializing as JSON.
*/
toJSON() {
let result = {
sortCriteria: this.sortCriteria.toJSON()
};
return result;
}
}

module.exports = OrderBy;
14,498 changes: 14,498 additions & 0 deletions packages/composer-common/lib/query/parser.js

Large diffs are not rendered by default.

Loading