1
0
Fork 0

make `CONCAT` and `CONCAT_SEPARATOR` more useful

This commit is contained in:
Jan Steemann 2014-11-12 00:07:03 +01:00
parent 487f35affb
commit 8c08a3b24a
5 changed files with 81 additions and 18 deletions

View File

@ -6,6 +6,8 @@ v2.3.0 (XXX-XX-XX)
* added AQL functions `ASSEMBLE` and `VALUES` * added AQL functions `ASSEMBLE` and `VALUES`
* made AQL functions `CONCAT` and `CONCAT_SEPARATOR` work with list arguments
v2.3.0-beta2 (2014-11-08) v2.3.0-beta2 (2014-11-08)
------------------------- -------------------------

View File

@ -3,11 +3,19 @@
For string processing, AQL offers the following functions: For string processing, AQL offers the following functions:
- *CONCAT(value1, value2, ... valuen)*: Concatenate the strings - *CONCAT(value1, value2, ... valuen)*: Concatenate the strings
passed as in *value1* to *valuen*. *null* values are ignored passed as in *value1* to *valuen*. *null* values are ignored. List value arguments
are expanded automatically, and their individual members will be concatenated.
/* "foobarbaz" */
RETURN CONCAT('foo', 'bar', 'baz')
/* "foobarbaz" */
RETURN CONCAT([ 'foo', 'bar', 'baz' ])
- *CONCAT_SEPARATOR(separator, value1, value2, ... valuen)*: - *CONCAT_SEPARATOR(separator, value1, value2, ... valuen)*:
Concatenate the strings passed as arguments *value1* to *valuen* using the Concatenate the strings passed as arguments *value1* to *valuen* using the
*separator* string. *null* values are ignored *separator* string. *null* values are ignored. List value arguments
are expanded automatically, and their individual members will be concatenated.
- *CHAR_LENGTH(value)*: Return the number of characters in *value*. This is - *CHAR_LENGTH(value)*: Return the number of characters in *value*. This is
a synonym for *LENGTH(value)* a synonym for *LENGTH(value)*

View File

@ -104,8 +104,8 @@ std::unordered_map<std::string, Function const> const Executor::FunctionNames{
{ "TO_LIST", Function("TO_LIST", "AQL_TO_LIST", ".", true, false, true) }, { "TO_LIST", Function("TO_LIST", "AQL_TO_LIST", ".", true, false, true) },
// string functions // string functions
{ "CONCAT", Function("CONCAT", "AQL_CONCAT", "sz,sz|+", true, false, true) }, { "CONCAT", Function("CONCAT", "AQL_CONCAT", "szl|+", true, false, true) },
{ "CONCAT_SEPARATOR", Function("CONCAT_SEPARATOR", "AQL_CONCAT_SEPARATOR", "s,sz,sz|+", true, false, true) }, { "CONCAT_SEPARATOR", Function("CONCAT_SEPARATOR", "AQL_CONCAT_SEPARATOR", "s,szl|+", true, false, true) },
{ "CHAR_LENGTH", Function("CHAR_LENGTH", "AQL_CHAR_LENGTH", "s", true, false, true) }, { "CHAR_LENGTH", Function("CHAR_LENGTH", "AQL_CHAR_LENGTH", "s", true, false, true) },
{ "LOWER", Function("LOWER", "AQL_LOWER", "s", true, false, true) }, { "LOWER", Function("LOWER", "AQL_LOWER", "s", true, false, true) },
{ "UPPER", Function("UPPER", "AQL_UPPER", "s", true, false, true) }, { "UPPER", Function("UPPER", "AQL_UPPER", "s", true, false, true) },

View File

@ -1547,15 +1547,25 @@ function ARITHMETIC_MODULUS (lhs, rhs) {
function AQL_CONCAT () { function AQL_CONCAT () {
"use strict"; "use strict";
var result = '', i; var result = '', i, j;
for (i = 0; i < arguments.length; ++i) { for (i = 0; i < arguments.length; ++i) {
var element = arguments[i]; var element = arguments[i];
if (TYPEWEIGHT(element) === TYPEWEIGHT_NULL) { var weight = TYPEWEIGHT(element);
if (weight === TYPEWEIGHT_NULL) {
continue; continue;
} }
else if (weight === TYPEWEIGHT_LIST) {
for (j = 0; j < element.length; ++j) {
if (TYPEWEIGHT(element[j]) !== TYPEWEIGHT_NULL) {
result += AQL_TO_STRING(element[j]);
}
}
}
else {
result += AQL_TO_STRING(element); result += AQL_TO_STRING(element);
} }
}
return result; return result;
} }
@ -1567,12 +1577,13 @@ function AQL_CONCAT () {
function AQL_CONCAT_SEPARATOR () { function AQL_CONCAT_SEPARATOR () {
"use strict"; "use strict";
var separator, found = false, result = '', i; var separator, found = false, result = '', i, j;
for (i = 0; i < arguments.length; ++i) { for (i = 0; i < arguments.length; ++i) {
var element = arguments[i]; var element = arguments[i];
var weight = TYPEWEIGHT(element);
if (i > 0 && TYPEWEIGHT(element) === TYPEWEIGHT_NULL) { if (i > 0 && weight === TYPEWEIGHT_NULL) {
continue; continue;
} }
@ -1584,8 +1595,22 @@ function AQL_CONCAT_SEPARATOR () {
result += separator; result += separator;
} }
if (weight === TYPEWEIGHT_LIST) {
found = false;
for (j = 0; j < element.length; ++j) {
if (TYPEWEIGHT(element[j]) !== TYPEWEIGHT_NULL) {
if (found) {
result += separator;
}
result += AQL_TO_STRING(element[j]);
found = true; found = true;
}
}
}
else {
result += AQL_TO_STRING(element); result += AQL_TO_STRING(element);
found = true;
}
} }
return result; return result;

View File

@ -773,13 +773,22 @@ function ahuacatlStringFunctionsTestSuite () {
assertEqual(expected, actual); assertEqual(expected, actual);
}, },
////////////////////////////////////////////////////////////////////////////////
/// @brief test concat function
////////////////////////////////////////////////////////////////////////////////
testConcatList : function () {
var expected = [ "theQuickBrownアボカドJumps名称について" ];
var actual = getQueryResults("FOR r IN [ 1 ] return CONCAT([ 'the', 'Quick', '', null, 'Brown', null, 'アボカド', 'Jumps', '名称について' ])");
assertEqual(expected, actual);
},
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief test concat function /// @brief test concat function
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testConcatInvalid : function () { testConcatInvalid : function () {
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT()"); assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT()");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT(\"yes\")");
assertEqual([ "yestrue" ], getQueryResults("RETURN CONCAT(\"yes\", true)")); assertEqual([ "yestrue" ], getQueryResults("RETURN CONCAT(\"yes\", true)"));
assertEqual([ "yes4" ], getQueryResults("RETURN CONCAT(\"yes\", 4)")); assertEqual([ "yes4" ], getQueryResults("RETURN CONCAT(\"yes\", 4)"));
assertEqual([ "yes" ], getQueryResults("RETURN CONCAT(\"yes\", [ ])")); assertEqual([ "yes" ], getQueryResults("RETURN CONCAT(\"yes\", [ ])"));
@ -791,7 +800,7 @@ function ahuacatlStringFunctionsTestSuite () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief test concatseparator function /// @brief test concat_separator function
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testConcatSeparator1 : function () { testConcatSeparator1 : function () {
@ -801,7 +810,7 @@ function ahuacatlStringFunctionsTestSuite () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief test concatseparator function /// @brief test concat_separator function
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testConcatSeparator2 : function () { testConcatSeparator2 : function () {
@ -811,13 +820,32 @@ function ahuacatlStringFunctionsTestSuite () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief test concatseparator function /// @brief test concat_separator function
////////////////////////////////////////////////////////////////////////////////
testConcatSeparatorList1 : function () {
var expected = [ "the,Quick,Brown,Fox,Jumps,higher,than,you" ];
var actual = getQueryResults("FOR r IN [ 1 ] return CONCAT_SEPARATOR(',', [ 'the', 'Quick', null, 'Brown', null, 'Fox', 'Jumps' ], 'higher', [ 'than', 'you' ])");
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test concat_separator function
////////////////////////////////////////////////////////////////////////////////
testConcatSeparatorList2 : function () {
var expected = [ "the*/*/Quick*/*/Brown*/*/*/*/Fox*/*/Jumps*/*/higher*/*/than*/*/you" ];
var actual = getQueryResults("FOR r IN [ 1 ] return CONCAT_SEPARATOR('*/*/', [ 'the', 'Quick', null, 'Brown', '', 'Fox', 'Jumps' ], [ ], 'higher', [ 'than', 'you' ])");
assertEqual(expected, actual);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test concat_separator function
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testConcatSeparatorInvalid : function () { testConcatSeparatorInvalid : function () {
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT_SEPARATOR()"); assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT_SEPARATOR()");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT_SEPARATOR(\"yes\")"); assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT_SEPARATOR(\"yes\")");
assertQueryError(errors.ERROR_QUERY_FUNCTION_ARGUMENT_NUMBER_MISMATCH.code, "RETURN CONCAT_SEPARATOR(\"yes\", \"yes\")");
assertEqual([ "yesnullyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(null, \"yes\", \"yes\")")); assertEqual([ "yesnullyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(null, \"yes\", \"yes\")"));
assertEqual([ "yestrueyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(true, \"yes\", \"yes\")")); assertEqual([ "yestrueyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(true, \"yes\", \"yes\")"));
assertEqual([ "yes4yes" ], getQueryResults("RETURN CONCAT_SEPARATOR(4, \"yes\", \"yes\")")); assertEqual([ "yes4yes" ], getQueryResults("RETURN CONCAT_SEPARATOR(4, \"yes\", \"yes\")"));
@ -825,7 +853,7 @@ function ahuacatlStringFunctionsTestSuite () {
assertEqual([ "yes[object Object]yes" ], getQueryResults("RETURN CONCAT_SEPARATOR({ }, \"yes\", \"yes\")")); assertEqual([ "yes[object Object]yes" ], getQueryResults("RETURN CONCAT_SEPARATOR({ }, \"yes\", \"yes\")"));
assertEqual([ "trueyesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", true, \"yes\")")); assertEqual([ "trueyesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", true, \"yes\")"));
assertEqual([ "4yesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", 4, \"yes\")")); assertEqual([ "4yesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", 4, \"yes\")"));
assertEqual([ "yesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", [ ], \"yes\")")); assertEqual([ "yes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", [ ], \"yes\")"));
assertEqual([ "[object Object]yesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", { }, \"yes\")")); assertEqual([ "[object Object]yesyes" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", { }, \"yes\")"));
assertEqual([ "yesyestrue" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", \"yes\", true)")); assertEqual([ "yesyestrue" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", \"yes\", true)"));
assertEqual([ "yesyes4" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", \"yes\", 4)")); assertEqual([ "yesyes4" ], getQueryResults("RETURN CONCAT_SEPARATOR(\"yes\", \"yes\", 4)"));