Skip to content

Commit 2784eef

Browse files
committed
Add markdown table format for Cypher queries
1 parent b8cb809 commit 2784eef

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

scripts/executeQuery.sh

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ fi
3535
cypher_query_file_name=""
3636
no_source_reference=false
3737
omit_query_error_highlighting=false
38+
output_markdown_table=false
3839
query_parameters=""
3940

4041
# Input Arguments: Function to print usage information
4142
print_usage() {
42-
echo "executeQuery Usage: $0 <filename> [--no-source-reference-column] [--omit-query-error-highlighting]" >&2
43+
echo "executeQuery Usage: $0 <filename> [--no-source-reference-column] [--omit-query-error-highlighting] [--output-markdown-table]" >&2
4344
echo "Options:" >&2
4445
echo " --no-source-reference-column: Exclude the source reference column" >&2
4546
echo " --omit-query-error-highlighting: Log query errors in same color as infos" >&2
47+
echo " --output-markdown-table: Output the result as markdown table instead of CSV" >&2
4648
}
4749

4850
# Input Arguments: Parse the command-line arguments
4951
while [[ $# -gt 0 ]]; do
5052
arg="$1"
51-
5253
case $arg in
5354
--no-source-reference-column)
5455
no_source_reference=true
@@ -58,6 +59,10 @@ while [[ $# -gt 0 ]]; do
5859
omit_query_error_highlighting=true
5960
shift
6061
;;
62+
--output-markdown-table)
63+
output_markdown_table=true
64+
shift
65+
;;
6166
*)
6267
if [[ -z "${cypher_query_file_name}" ]]; then
6368
# Input Arguments: Read the first unnamed input argument containing the name of the cypher file
@@ -134,11 +139,35 @@ if [[ -n "${error_message}" ]]; then
134139
exit 1
135140
fi
136141

137-
# Output results in CSV format
138-
if [ "${no_source_reference}" = true ] ; then
139-
echo -n "${cypher_query_result}" | jq -r '(.results[0])? | .columns,(.data[].row)? | map(if type == "array" then join(",") else . end) | flatten | @csv'
142+
if [ "${output_markdown_table}" = "true" ] ; then
143+
# Output results in Markdown format
144+
echo "executeQuery: Will output in Markdown Table Format" >&2
145+
echo -n "${cypher_query_result}" | jq -r '
146+
# Take the first query result
147+
.results[0] as $result
148+
149+
# Extract the column names
150+
| $result.columns as $columns
151+
152+
# Build the Markdown header row
153+
| ( "| " + ( $columns | join(" | ") ) + " |" )
154+
155+
# Build the Markdown separator row
156+
, ( "| " + ( $columns | map("---") | join(" | ") ) + " |" )
157+
158+
# Build one row for each data entry
159+
, ( $result.data[].row
160+
| map(tostring)
161+
| "| " + ( join(" | ") ) + " |"
162+
)
163+
'
140164
else
141-
cypher_query_file_relative_name=${cypher_query_file_name#/**/cypher/}
142-
sourceFileReferenceInfo="Source Cypher File: ${cypher_query_file_relative_name}"
143-
echo -n "${cypher_query_result}" | jq -r --arg sourceReference "${sourceFileReferenceInfo}" '(.results[0])? | .columns + [$sourceReference], (.data[].row)? + [""] | map(if type == "array" then join(",") else . end) | flatten | @csv'
165+
# Output results in CSV format
166+
if [ "${no_source_reference}" = true ] ; then
167+
echo -n "${cypher_query_result}" | jq -r '(.results[0])? | .columns,(.data[].row)? | map(if type == "array" then join(",") else . end) | flatten | @csv'
168+
else
169+
cypher_query_file_relative_name=${cypher_query_file_name#/**/cypher/}
170+
sourceFileReferenceInfo="Source Cypher File: ${cypher_query_file_relative_name}"
171+
echo -n "${cypher_query_result}" | jq -r --arg sourceReference "${sourceFileReferenceInfo}" '(.results[0])? | .columns + [$sourceReference], (.data[].row)? + [""] | map(if type == "array" then join(",") else . end) | flatten | @csv'
172+
fi
144173
fi

0 commit comments

Comments
 (0)