mirror of https://gitee.com/bigwinds/arangodb
Started implementing fluent AQL interface. Right now allows edges
This commit is contained in:
parent
aec7099a3e
commit
d558fa8fbd
|
@ -100,9 +100,66 @@ var findOrCreateCollectionByName = function (name, type) {
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- Fluent AQL bindins
|
// --SECTION-- Fluent AQL Interface
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// --SECTION-- Fluent AQL Interface
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
var AQLStatement = function(query, isEdgeQuery) {
|
||||||
|
this.query = query;
|
||||||
|
this.edgeQuery = isEdgeQuery || false;
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLStatement.prototype.printQuery = function() {
|
||||||
|
return this.query;
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLStatement.prototype.isEdgeQuery = function() {
|
||||||
|
return this.edgeQuery;
|
||||||
|
};
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// --SECTION-- AQL Generator
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
var AQLGenerator = function(graphName) {
|
||||||
|
this.stack = [];
|
||||||
|
this.bindVars = {
|
||||||
|
"graphName": graphName
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLGenerator.prototype.edges = function(startVertex, direction) {
|
||||||
|
var query = "GRAPH_EDGES(@graphName,@startVertex_"
|
||||||
|
+ this.stack.length + ","
|
||||||
|
+ direction + ")";
|
||||||
|
this.bindVars["startVertex_" + this.stack.length] = startVertex;
|
||||||
|
var stmt = new AQLStatement(query, true);
|
||||||
|
this.stack.push(stmt);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLGenerator.prototype.restrict = function() {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLGenerator.prototype.printQuery = function() {
|
||||||
|
return this.stack.map(function(stmt) {
|
||||||
|
return stmt.printQuery();
|
||||||
|
}).join(" ");
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLGenerator.prototype.execute = function() {
|
||||||
|
//TODO Implement -> db._query().execute()
|
||||||
|
};
|
||||||
|
|
||||||
|
AQLGenerator.prototype.toArray = function() {
|
||||||
|
return this.exectue().toArray();
|
||||||
|
};
|
||||||
|
|
||||||
var bindFluentAQLFunctionsToArray = function(arr) {
|
var bindFluentAQLFunctionsToArray = function(arr) {
|
||||||
var filter = arr.filter.bind(arr);
|
var filter = arr.filter.bind(arr);
|
||||||
arr.restrict = function(collections) {
|
arr.restrict = function(collections) {
|
||||||
|
@ -261,6 +318,7 @@ var _create = function (graphName, edgeDefinitions) {
|
||||||
|
|
||||||
var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollections) {
|
var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollections) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
this.__name = graphName;
|
||||||
this.__vertexCollections = vertexCollections;
|
this.__vertexCollections = vertexCollections;
|
||||||
this.__edgeCollections = edgeCollections;
|
this.__edgeCollections = edgeCollections;
|
||||||
this.__edgeDefinitions = edgeDefinitions;
|
this.__edgeDefinitions = edgeDefinitions;
|
||||||
|
@ -297,10 +355,11 @@ Graph.prototype._vertexCollections = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @brief _edges(vertexId).
|
/// @brief _EDGES(vertexId).
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
Graph.prototype._edges = function(vertexId) {
|
// might be needed from AQL itself
|
||||||
|
Graph.prototype._EDGES = function(vertexId) {
|
||||||
var edgeCollections = this._edgeCollections();
|
var edgeCollections = this._edgeCollections();
|
||||||
var result = [];
|
var result = [];
|
||||||
|
|
||||||
|
@ -311,10 +370,18 @@ Graph.prototype._edges = function(vertexId) {
|
||||||
result = result.concat(edgeCollection.edges(vertexId));
|
result = result.concat(edgeCollection.edges(vertexId));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
bindFluentAQLFunctionsToArray(result);
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// @brief _edges(vertexId).
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
Graph.prototype._edges = function(vertexId) {
|
||||||
|
var AQLStmt = new AQLGenerator(this.__name);
|
||||||
|
return AQLStmt.edges(vertexId, "any");
|
||||||
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @brief inEdges(vertexId).
|
/// @brief inEdges(vertexId).
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -252,7 +252,7 @@ function GeneralGraphCreationSuite() {
|
||||||
// --SECTION-- Simple Queries
|
// --SECTION-- Simple Queries
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
function GeneralGraphSimpleQueriesSuite() {
|
function GeneralGraphAQLQueriesSuite() {
|
||||||
|
|
||||||
var dropInclExcl = function() {
|
var dropInclExcl = function() {
|
||||||
var col = db._collection("_graphs");
|
var col = db._collection("_graphs");
|
||||||
|
@ -290,6 +290,44 @@ function GeneralGraphSimpleQueriesSuite() {
|
||||||
/// @brief test: restrict construct on edges
|
/// @brief test: restrict construct on edges
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
test_edges: function() {
|
||||||
|
var g = createInclExcl();
|
||||||
|
var incEdge1 = g.included.save(
|
||||||
|
"v1/1",
|
||||||
|
"v2/1",
|
||||||
|
{
|
||||||
|
included: true
|
||||||
|
}
|
||||||
|
)._id;
|
||||||
|
var incEdge2 = g.included.save(
|
||||||
|
"v1/2",
|
||||||
|
"v1/1",
|
||||||
|
{
|
||||||
|
included: true
|
||||||
|
}
|
||||||
|
)._id;
|
||||||
|
var incEdge3 = g.excluded.save(
|
||||||
|
"v1/1",
|
||||||
|
"v3/1",
|
||||||
|
{
|
||||||
|
included: true
|
||||||
|
}
|
||||||
|
)._id;
|
||||||
|
var query = g._edges("v1/1");
|
||||||
|
assertEqual(query.printQuery(), "GRAPH_EDGES("
|
||||||
|
+ "@graphName,@startVertex_0,any)");
|
||||||
|
var bindVars = query.bindVars;
|
||||||
|
assertEqual(bindVars.graphName, "graph");
|
||||||
|
assertEqual(bindVars.startVertex_0, "v1/1");
|
||||||
|
/*
|
||||||
|
var result = query.toArray();
|
||||||
|
assertEqual(result.length, 3);
|
||||||
|
assertTrue(findIdInResult(result, incEdge1));
|
||||||
|
assertTrue(findIdInResult(result, incEdge2));
|
||||||
|
assertFalse(findIdInResult(result, incEdge3));
|
||||||
|
*/
|
||||||
|
},
|
||||||
|
|
||||||
test_restrictOnEdges: function() {
|
test_restrictOnEdges: function() {
|
||||||
var g = createInclExcl();
|
var g = createInclExcl();
|
||||||
var incEdge1 = g.included.save(
|
var incEdge1 = g.included.save(
|
||||||
|
@ -415,7 +453,7 @@ function GeneralGraphSimpleQueriesSuite() {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
jsunity.run(GeneralGraphCreationSuite);
|
jsunity.run(GeneralGraphCreationSuite);
|
||||||
jsunity.run(GeneralGraphSimpleQueriesSuite);
|
jsunity.run(GeneralGraphAQLQueriesSuite);
|
||||||
|
|
||||||
return jsunity.done();
|
return jsunity.done();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue