mirror of https://gitee.com/bigwinds/arangodb
made AQL DOCUMENT function polymorphic
This commit is contained in:
parent
052fa0ee86
commit
83866ba58b
15
CHANGELOG
15
CHANGELOG
|
@ -1,6 +1,21 @@
|
|||
v1.5.x (XXXX-XX-XX)
|
||||
-------------------
|
||||
|
||||
* made AQL `DOCUMENT()` function polymorphic and work with just one parameter.
|
||||
|
||||
This allows using the `DOCUMENT` function like this:
|
||||
|
||||
DOCUMENT('users/john')
|
||||
DOCUMENT([ 'users/john', 'users/amy' ])
|
||||
|
||||
in addition to the existing use cases:
|
||||
|
||||
DOCUMENT(users, 'users/john')
|
||||
DOCUMENT(users, 'john')
|
||||
DOCUMENT(users, [ 'users/john' ])
|
||||
DOCUMENT(users, [ 'users/john', 'users/amy' ])
|
||||
DOCUMENT(users, [ 'john', 'amy' ])
|
||||
|
||||
* issue #424: Documentation about IDs needs to be upgraded
|
||||
|
||||
* removed command-line option `--log.format`
|
||||
|
|
|
@ -1515,7 +1515,7 @@ function categories:
|
|||
- @FN{COLLECTIONS()}: returns a list of collections. Each collection is returned as a document
|
||||
with attributes `name` and `_id`.
|
||||
|
||||
- @FN{DOCUMENT(@FA{collection}, @FA{id})}: returns the document that id uniquely identified by
|
||||
- @FN{DOCUMENT(@FA{collection}, @FA{id})}: returns the document which is uniquely identified by
|
||||
the @FA{id}. ArangoDB will try to find the document using the `_id` value of the document
|
||||
in the specified collection. If there is a mismatch between the @FA{collection} passed and
|
||||
the collection specified in @FA{id}, then `null` will be returned. Additionally, if the
|
||||
|
@ -1523,6 +1523,23 @@ function categories:
|
|||
found, `null` 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.
|
||||
|
||||
Examples:
|
||||
|
||||
DOCUMENT(users, "users/john")
|
||||
DOCUMENT(users, "john")
|
||||
|
||||
DOCUMENT(users, [ "users/john", "users/amy" ])
|
||||
DOCUMENT(users, [ "john", "amy" ])
|
||||
|
||||
Note: the @FN{DOCUMENT} function is polymorphic since ArangoDB 1.4. It can now be used with
|
||||
a single parameter @FA{id} as follows:
|
||||
|
||||
- @FN{DOCUMENT(@FA{id})}: in this case, @FA{id} must either be a document handle string
|
||||
(consisting of collection name and document key) or a list of document handle strings, e.g.
|
||||
|
||||
DOCUMENT("users/john")
|
||||
DOCUMENT([ "users/john", "users/amy" ])
|
||||
|
||||
High-level operations {#AqlOperations}
|
||||
======================================
|
||||
|
||||
|
|
|
@ -669,8 +669,7 @@ TRI_associative_pointer_t* TRI_CreateFunctionsAql (void) {
|
|||
REGISTER_FUNCTION("ATTRIBUTES", "ATTRIBUTES", true, false, "a|b,b", 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,sl", NULL);
|
||||
REGISTER_FUNCTION("DOCUMENT_HANDLE", "DOCUMENT_HANDLE", false, false, "sl", NULL);
|
||||
REGISTER_FUNCTION("DOCUMENT", "DOCUMENT", false, false, "hsl|sl", NULL);
|
||||
REGISTER_FUNCTION("MATCHES", "MATCHES", true, false, ".,l|b", NULL);
|
||||
REGISTER_FUNCTION("UNSET", "UNSET", true, false, "a,sl|+", NULL);
|
||||
REGISTER_FUNCTION("KEEP", "KEEP", true, false, "a,sl|+", NULL);
|
||||
|
|
|
@ -649,35 +649,6 @@ function LIST (value) {
|
|||
/// @brief get a document by its unique id or their unique ids
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
function DOCUMENT (collection, id) {
|
||||
"use strict";
|
||||
|
||||
if (TYPEWEIGHT(id) === TYPEWEIGHT_LIST) {
|
||||
var c = COLLECTION(collection);
|
||||
|
||||
var result = [ ], i;
|
||||
for (i = 0; i < id.length; ++i) {
|
||||
try {
|
||||
result.push(c.document(id[i]));
|
||||
}
|
||||
catch (e1) {
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
return COLLECTION(collection).document(id);
|
||||
}
|
||||
catch (e2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief get a document by its unique id or their unique ids
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
function DOCUMENT_HANDLE (id) {
|
||||
"use strict";
|
||||
|
||||
|
@ -701,6 +672,45 @@ function DOCUMENT_HANDLE (id) {
|
|||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief get a document by its unique id or their unique ids
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
function DOCUMENT (collection, id) {
|
||||
"use strict";
|
||||
|
||||
// we're polymorphic
|
||||
if (id === undefined) {
|
||||
// called with a single parameter
|
||||
var weight = TYPEWEIGHT(collection);
|
||||
|
||||
if (weight === TYPEWEIGHT_STRING || weight === TYPEWEIGHT_LIST) {
|
||||
return DOCUMENT_HANDLE(collection);
|
||||
}
|
||||
}
|
||||
|
||||
if (TYPEWEIGHT(id) === TYPEWEIGHT_LIST) {
|
||||
var c = COLLECTION(collection);
|
||||
|
||||
var result = [ ], i;
|
||||
for (i = 0; i < id.length; ++i) {
|
||||
try {
|
||||
result.push(c.document(id[i]));
|
||||
}
|
||||
catch (e1) {
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
return COLLECTION(collection).document(id);
|
||||
}
|
||||
catch (e2) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief get all documents from the specified collection
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -3950,7 +3960,6 @@ exports.GET_INDEX = GET_INDEX;
|
|||
exports.DOCUMENT_MEMBER = DOCUMENT_MEMBER;
|
||||
exports.LIST = LIST;
|
||||
exports.DOCUMENT = DOCUMENT;
|
||||
exports.DOCUMENT_HANDLE = DOCUMENT_HANDLE;
|
||||
exports.GET_DOCUMENTS = GET_DOCUMENTS;
|
||||
exports.GET_DOCUMENTS_INCREMENTAL_INIT = GET_DOCUMENTS_INCREMENTAL_INIT;
|
||||
exports.GET_DOCUMENTS_INCREMENTAL_CONT = GET_DOCUMENTS_INCREMENTAL_CONT;
|
||||
|
|
|
@ -1740,14 +1740,26 @@ function ahuacatlFunctionsTestSuite () {
|
|||
|
||||
var expected, actual;
|
||||
|
||||
// test with two parameters
|
||||
expected = [ { title: "123", value : 456 } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(" + cn + ", \"" + d1._id + "\")");
|
||||
assertEqual(expected, actual);
|
||||
|
||||
actual = getQueryResults("RETURN DOCUMENT(" + cn + ", \"" + d1._key + "\")");
|
||||
assertEqual(expected, actual);
|
||||
|
||||
expected = [ { title: "nada", value : 123 } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(" + cn + ", \"" + d2._id + "\")");
|
||||
assertEqual(expected, actual);
|
||||
|
||||
actual = getQueryResults("RETURN DOCUMENT(" + cn + ", \"" + d2._key + "\")");
|
||||
assertEqual(expected, actual);
|
||||
|
||||
// test with one parameter
|
||||
expected = [ { title: "nada", value : 123 } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(\"" + d2._id + "\")");
|
||||
assertEqual(expected, actual);
|
||||
|
||||
internal.db._drop(cn);
|
||||
},
|
||||
|
||||
|
@ -1765,14 +1777,30 @@ function ahuacatlFunctionsTestSuite () {
|
|||
|
||||
var expected, actual;
|
||||
|
||||
// test with two parameters
|
||||
expected = [ { title: "123", value : 456, zxy: 1 } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", { "@cn" : cn, "id" : d1._id });
|
||||
assertEqual(expected, actual);
|
||||
|
||||
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", { "@cn" : cn, "id" : d1._key });
|
||||
assertEqual(expected, actual);
|
||||
|
||||
expected = [ { title: "nada", value : 123, zzzz : false } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", { "@cn" : cn, "id" : d2._id });
|
||||
assertEqual(expected, actual);
|
||||
|
||||
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", { "@cn" : cn, "id" : d2._key });
|
||||
assertEqual(expected, actual);
|
||||
|
||||
// test with one parameter
|
||||
expected = [ { title: "123", value : 456, zxy: 1 } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@id)", { "id" : d1._id });
|
||||
assertEqual(expected, actual);
|
||||
|
||||
expected = [ { title: "nada", value : 123, zzzz : false } ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@id)", { "id" : d2._id });
|
||||
assertEqual(expected, actual);
|
||||
|
||||
internal.db._drop(cn);
|
||||
},
|
||||
|
||||
|
@ -1791,6 +1819,7 @@ function ahuacatlFunctionsTestSuite () {
|
|||
|
||||
var expected, actual;
|
||||
|
||||
// test with two parameters
|
||||
expected = [ [
|
||||
{ title: "123", value : 456, zxy : 1 },
|
||||
{ title: "nada", value : 123, zzzz : false },
|
||||
|
@ -1803,6 +1832,12 @@ function ahuacatlFunctionsTestSuite () {
|
|||
expected = [ [ { title: "nada", value : 123, zzzz : false } ] ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", { "@cn" : cn, "id" : [ d2._id ] }, true);
|
||||
assertEqual(expected, actual);
|
||||
|
||||
// test with one parameter
|
||||
expected = [ [ { title: "nada", value : 123, zzzz : false } ] ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@id)", { "id" : [ d2._id ] }, true);
|
||||
assertEqual(expected, actual);
|
||||
|
||||
|
||||
cx.remove(d3);
|
||||
|
||||
|
@ -1810,6 +1845,10 @@ function ahuacatlFunctionsTestSuite () {
|
|||
actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", { "@cn" : cn, "id" : [ d2._id, d3._id, "abc/def" ] }, true);
|
||||
assertEqual(expected, actual);
|
||||
|
||||
expected = [ [ { title: "nada", value : 123, zzzz : false } ] ];
|
||||
actual = getQueryResults("RETURN DOCUMENT(@id)", { "id" : [ d2._id, d3._id, "abc/def" ] }, true);
|
||||
assertEqual(expected, actual);
|
||||
|
||||
internal.db._drop(cn);
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue