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
9 changes: 9 additions & 0 deletions packages/composer-common/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ class ClassDeclaration {
return false;
}

/**
* Returns true if this class is the definition of an event
*
* @return {boolean} true if the class is an event
*/
isEvent() {
return false;
}

/**
* Returns true if this class can be pointed to by a relationship
*
Expand Down
9 changes: 9 additions & 0 deletions packages/composer-common/lib/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class EventDeclaration extends ClassDeclaration {
// this.properties.push(new Field(this, ast));
// }
}

/**
* Returns true if this class is the definition of an event
*
* @return {boolean} true if the class is an event
*/
isEvent() {
return true;
}
}

module.exports = EventDeclaration;
18 changes: 11 additions & 7 deletions packages/composer-common/lib/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ParseException = require('./parseexception');
const ModelUtil = require('../modelutil');
const Globalize = require('../globalize');

// const util = require('util');

/**
* Class representing a Model File. A Model File contains a single namespace
Expand Down Expand Up @@ -94,22 +95,27 @@ class ModelFile {
// console.log('\n========\n');
for(let n=0; n < this.ast.body.length; n++ ) {
let thing = this.ast.body[n];
let doDecorate = (thing.classExtension === null && decorate);

if (thing.classExtension === null && decorate){
// all good keep decorating
} else {
decorate=false;
}

if(thing.type === 'AssetDeclaration') {
if (doDecorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Asset' } }; }
if (decorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Asset' } }; }
this.declarations.push( new AssetDeclaration(this, thing) );
}
else if(thing.type === 'TransactionDeclaration') {
if (doDecorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Transaction' } }; }
if (decorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Transaction' } }; console.log('@14gracel CALLED>>>>>>>>>>>>>');}
this.declarations.push( new TransactionDeclaration(this, thing) );
}
else if(thing.type === 'EventDeclaration') {
if (doDecorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Event' } }; }
if (decorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Event' } }; }
this.declarations.push( new EventDeclaration(this, thing) );
}
else if(thing.type === 'ParticipantDeclaration') {
if (doDecorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Participant' } }; }
if (decorate){thing.classExtension = { type: 'ClassExtension', class: { type: 'Identifier', name: '_cst_Participant' } }; }
this.declarations.push( new ParticipantDeclaration(this, thing) );
}
else if(thing.type === 'EnumDeclaration') {
Expand All @@ -126,8 +132,6 @@ class ModelFile {
}),this.modelFile);
}
}


}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class RelationshipDeclaration extends Property {
// you can't have a relationship with a primitive...
if(ModelUtil.isPrimitiveType(this.getType())) {
throw new IllegalModelException('Relationship ' + this.getName() + ' cannot be to the primitive type ' + this.getType(), classDecl.getModelFile(), this.ast.location );
}
else {
} else {
let namespace = this.getParent().getModelFile().getNamespace();

// we first try to get the type from our own model file
Expand All @@ -76,7 +75,9 @@ class RelationshipDeclaration extends Property {

// console.log('>>RelationshipDeclaration');
// console.log(this);
if((namespace === ModelUtil.getSystemNamespace()) && classDeclaration.isSystemRelationshipTarget() === false) {
if (namespace === ModelUtil.getSystemNamespace() && classDecl.isEvent()) {
// Special case, allow relationship to Transaction in system Event
} else if((namespace === ModelUtil.getSystemNamespace()) && classDeclaration.isSystemRelationshipTarget() === false) {
throw new IllegalModelException('Relationship ' + this.getName() + ' must be to an asset, participant or transaction, but is to ' + this.getFullyQualifiedTypeName(), classDecl.getModelFile(), this.ast.location);
} else if(classDeclaration.isRelationshipTarget() === false) {
throw new IllegalModelException('Relationship ' + this.getName() + ' must be to an asset or participant, but is to ' + this.getFullyQualifiedTypeName(), classDecl.getModelFile(), this.ast.location);
Expand All @@ -85,7 +86,7 @@ class RelationshipDeclaration extends Property {
}

/**
* Returns a string representation of this property§
* Returns a string representation of this property
* @return {String} the string version of the property.
*/
toString() {
Expand Down
3 changes: 3 additions & 0 deletions packages/composer-common/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ class ModelManager {
*/
clearModelFiles() {
this.modelFiles = {};
let systemModelPath = path.join(path.dirname(__filename), '../models/system.cto');
let systemModelContents = fs.readFileSync(systemModelPath, ENCODING);
this.addModelFile(systemModelContents);
}

/**
Expand Down
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;
Loading