@@ -12,34 +12,28 @@ SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")"
1212
1313# Function to execute a cypher query from the given file (first argument) with the default method
1414execute_cypher () {
15- execute_cypher_http " ${1 } " || exit 1
15+ execute_cypher_http " ${@ } " || exit 1 # "${@}": Get all function arguments and forward them
1616}
1717
1818# Function to execute a cypher query from the given file (first argument) with the default method and just return the number of results
1919execute_cypher_summarized () {
20- execute_cypher_http_summarized " ${1 } " || exit 1
20+ execute_cypher_http_summarized " ${@ } " || exit 1 # "${@}": Get all function arguments and forward them
2121}
2222
2323# Function to execute a cypher query from the given file (first argument) with the default method and fail if there is no result
2424execute_cypher_expect_results () {
25- execute_cypher_http_expect_results " ${1 } " || exit 1
25+ execute_cypher_http_expect_results " ${@ } " || exit 1 # "${@}": Get all function arguments and forward them
2626}
2727
2828# Function to execute a cypher query from the given file (first and only argument) using Neo4j's HTTP API
2929execute_cypher_http () {
30- # Get the Cypher file name from the first argument
31- cypherFileName=" ${1} "
32-
3330 # (Neo4j HTTP API Script) Execute the Cyper query contained in the file and print the results as CSV
34- source $SCRIPTS_DIR /executeQuery.sh " ${cypherFileName } " || exit 1
31+ source $SCRIPTS_DIR /executeQuery.sh " ${@ } " || exit 1 # "${@}": Get all function arguments and forward them
3532}
3633
3734# Function to execute a cypher query from the given file (first and only argument) with a summarized (console) output using Neo4j's HTTP API
3835execute_cypher_http_summarized () {
39- # Get the Cypher file name from the first argument
40- cypherFileName=" ${1} "
41-
42- results=$( execute_cypher_http ${cypherFileName} | wc -l )
36+ results=$( execute_cypher_http " ${@ } " | wc -l ) # "${@}": Get all function arguments and forward them
4337 results=$(( results - 2 ))
4438 echo " $( basename -- " ${cypherFileName} " ) (via http) result lines: ${results} "
4539}
@@ -57,19 +51,43 @@ execute_cypher_http_expect_results() {
5751 fi
5852}
5953
54+ cypher_shell_query_parameters () {
55+ query_parameters=" "
56+ shift # ignore first argument containing the query file name
57+
58+ while [[ $# -gt 0 ]]; do
59+ arg=" ${1} "
60+ # Convert key=value argument to JSON "key": "value"
61+ json_parameter=$( echo " ${arg} " | awk -F' =' ' {print ""$1": "$2""}' | grep -iv ' \"#' )
62+ if [[ -z " ${query_parameters} " ]]; then
63+ # Add first query parameter directly
64+ query_parameters=" ${json_parameter} "
65+ else
66+ # Append next query parameter separated by a comma and a space
67+ query_parameters=" ${query_parameters} , ${json_parameter} "
68+ fi
69+ shift # iterate to next argument
70+ done
71+ echo " {${query_parameters} }"
72+ }
73+
6074# Function to execute a cypher query from the given file (first and only argument) using "cypher-shell" provided by Neo4j
6175execute_cypher_shell () {
6276 # Get the Cypher file name from the first argument
63- cypherFileName=$1
77+ cypherFileName=" ${1} "
6478
6579 # Check if NEO4J_BIN exists
6680 if [ ! -d " ${NEO4J_BIN} " ] ; then
6781 echo " executeQuery: Error: Neo4j Binary Directory <${NEO4J_BIN} > doesn't exist. Please run setupNeo4j.sh first." >&2
6882 exit 1
6983 fi
7084
85+ # Extract query parameters out of the key=value pair arguments that follow the first argument (query filename)
86+ query_parameters=$( cypher_shell_query_parameters " ${@ } " )
87+ echo " executeQuery: query_parameters=${query_parameters} "
88+
7189 # (Neo4j Cyper Shell CLI) Execute the Cyper query contained in the file and print the results as CSV
72- cat $cypherFileName | NEO4J_HOME=" ${NEO4J_DIRECTORY} " ${NEO4J_BIN} /cypher-shell -u neo4j -p " ${NEO4J_INITIAL_PASSWORD} " --format plain || exit 1
90+ cat $cypherFileName | NEO4J_HOME=" ${NEO4J_DIRECTORY} " ${NEO4J_BIN} /cypher-shell -u neo4j -p " ${NEO4J_INITIAL_PASSWORD} " --format plain --param " ${query_parameters} " || exit 1
7391
7492 # Display the name of the Cypher file without its path at the bottom of the CSV (or console) separated by an empty line
7593 # TODO Find a solution to move the source reference to the last column name
@@ -80,7 +98,7 @@ execute_cypher_shell() {
8098# Function to execute a cypher query from the given file (first and only argument) with a summarized (console) output using "cypher-shell" provided by Neo4j
8199execute_cypher_shell_summarized () {
82100 # Get the Cypher file name from the first argument
83- cypherFileName=$1
101+ cypherFileName=" ${1} "
84102
85103 results=$( execute_cypher_shell ${cypherFileName} | wc -l )
86104 results=$(( results - 2 ))
@@ -90,7 +108,7 @@ execute_cypher_shell_summarized() {
90108# Function to execute a cypher query from the given file (first and only argument) that fails on no result using "cypher-shell" provided by Neo4j
91109execute_cypher_shell_expect_results () {
92110 # Get the Cypher file name from the first argument
93- cypherFileName=$1
111+ cypherFileName=" ${1} "
94112
95113 results=$( execute_cypher_shell ${cypherFileName} | wc -l )
96114 results=$(( results - 2 ))
0 commit comments