1
0
Fork 0

extended AQL document() function to take id lists

This commit is contained in:
Jan Steemann 2012-12-05 14:10:06 +01:00
parent 23789e0b8c
commit a9df4d22cb
4 changed files with 80 additions and 15 deletions

View File

@ -480,7 +480,7 @@ TRI_associative_pointer_t* TRI_InitialiseFunctionsAql (void) {
REGISTER_FUNCTION("HAS", "HAS", true, false, "az,s", NULL);
REGISTER_FUNCTION("MERGE", "MERGE", true, false, "a,a|+", NULL);
REGISTER_FUNCTION("MERGE_RECURSIVE", "MERGE_RECURSIVE", true, false, "a,a|+", NULL);
REGISTER_FUNCTION("DOCUMENT", "DOCUMENT", false, false, "h,.", NULL);
REGISTER_FUNCTION("DOCUMENT", "DOCUMENT", false, false, "h,sl", NULL);
// geo functions
REGISTER_FUNCTION("NEAR", "GEO_NEAR", false, false, "h,n,n,n|s", NULL);

View File

@ -981,7 +981,8 @@
/// in the specified collection. If there is a mismatch between the @FA{collection} passed and
/// the collection specified in @FA{id}, then no document will be returned. Additionally, if the
/// @FA{collection} matches the collection value specified in @FA{id} but the document cannot be
/// found, no document will be returned.
/// found, no document will be returned. This function also allows @FA{id} to be a list of ids.
/// In this case, the function will return a list of all documents that could be found.
///
/// @section AqlOperations High-level operations
///

View File

@ -412,10 +412,24 @@ function AHUACATL_LIST (value) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get a document by its unique id
/// @brief get a document by its unique id or their unique ids
////////////////////////////////////////////////////////////////////////////////
function AHUACATL_DOCUMENT (collection, id) {
if (AHUACATL_TYPEWEIGHT(id) === AHUACATL_TYPEWEIGHT_LIST) {
var c = AHUACATL_COLLECTION(collection);
var result = [ ];
for (var i = 0; i < id.length; ++i) {
try {
result.push(c.document(id[i]));
}
catch (e) {
}
}
return result;
}
try {
return AHUACATL_COLLECTION(collection).document(id);
}

View File

@ -53,6 +53,24 @@ function ahuacatlFunctionsTestSuite () {
////////////////////////////////////////////////////////////////////////////////
function getQueryResults (query, isFlat, bindVars) {
var sanitize = function (row) {
var keys = [ ];
for (var k in row) {
if (row.hasOwnProperty(k) && k != '_rev' && k != '_key' && k != '_id') {
keys.push(k);
}
}
keys.sort();
var resultRow = { };
for (var k in keys) {
if (keys.hasOwnProperty(k)) {
resultRow[keys[k]] = row[keys[k]];
}
}
return resultRow;
};
var result = executeQuery(query, bindVars).getRows();
var results = [ ];
@ -66,21 +84,16 @@ function ahuacatlFunctionsTestSuite () {
results.push(row);
}
else {
var keys = [ ];
for (var k in row) {
if (row.hasOwnProperty(k) && k != '_rev' && k != '_key' && k != '_id') {
keys.push(k);
if (Array.isArray(row)) {
var x = [ ];
for (var j = 0; j < row.length; ++j) {
x.push(sanitize(row[j]));
}
results.push(x);
}
keys.sort();
var resultRow = { };
for (var k in keys) {
if (keys.hasOwnProperty(k)) {
resultRow[keys[k]] = row[keys[k]];
}
else {
results.push(sanitize(row));
}
results.push(resultRow);
}
}
@ -1128,6 +1141,43 @@ function ahuacatlFunctionsTestSuite () {
internal.db._drop(cn);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test document function
////////////////////////////////////////////////////////////////////////////////
testDocumentMulti1 : function () {
var cn = "UnitTestsAhuacatlFunctions";
internal.db._drop(cn);
var cx = internal.db._create(cn);
var d1 = cx.save({ "title" : "123", "value" : 456, "zxy" : 1 });
var d2 = cx.save({ "title" : "nada", "value" : 123, "zzzz" : false });
var d3 = cx.save({ "title" : "boom", "value" : 3321, "zzzz" : null });
var expected, actual;
expected = [ [
{ title: "123", value : 456, zxy : 1 },
{ title: "nada", value : 123, zzzz : false },
{ title: "boom", value : 3321, zzzz : null }
] ];
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", false, { "@cn" : cn, "id" : [ d1._id, d2._id, d3._id ] });
assertEqual(expected, actual);
expected = [ [ { title: "nada", value : 123, zzzz : false } ] ];
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", false, { "@cn" : cn, "id" : [ d2._id ] });
assertEqual(expected, actual);
cx.remove(d3);
expected = [ [ { title: "nada", value : 123, zzzz : false } ] ];
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", false, { "@cn" : cn, "id" : [ d2._id, d3._id, "abc/def" ] });
assertEqual(expected, actual);
internal.db._drop(cn);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test document function
////////////////////////////////////////////////////////////////////////////////