@@ -6,7 +6,7 @@ SHOW_HELP=false
66
77# Function to display help
88display_help () {
9- cat << EOF
9+ cat << EOF
1010Usage: $0 [OPTIONS]
1111
1212Options:
3232
3333# Parse command line arguments
3434while [[ " $# " -gt 0 ]]; do
35- case $1 in
36- --dsn) DSN=" $2 " ; shift ;;
37- --help) SHOW_HELP=true ;;
38- * ) echo " Error: Unknown parameter: $1 " ; display_help; exit 1 ;;
39- esac
40- shift
35+ case $1 in
36+ --dsn)
37+ DSN=" $2 "
38+ shift
39+ ;;
40+ --help) SHOW_HELP=true ;;
41+ * )
42+ echo " Error: Unknown parameter: $1 "
43+ display_help
44+ exit 1
45+ ;;
46+ esac
47+ shift
4148done
4249
4350# Show help if requested
4451if [ " $SHOW_HELP " = true ]; then
45- display_help
46- exit 0
52+ display_help
53+ exit 0
4754fi
4855
4956# Check for DSN in environment variable if not specified via command line
5057if [ -z " $DSN " ] && [ -n " $BENDSQL_DSN " ]; then
51- DSN=" $BENDSQL_DSN "
58+ DSN=" $BENDSQL_DSN "
5259fi
5360
5461# Function to execute bendsql query and handle errors
5562execute_query () {
56- local query=" $1 "
57- local result
58- local bendsql_cmd=" bendsql"
59-
60- # Add DSN if specified
61- if [ -n " $DSN " ]; then
62- bendsql_cmd=" $bendsql_cmd --dsn=\" $DSN \" "
63- fi
64-
65- bendsql_cmd=" $bendsql_cmd --query=\" $query \" "
66-
67- # Use eval to properly handle quotes in the command
68- result=$( eval " $bendsql_cmd " 2>&1 )
69- if [ $? -ne 0 ]; then
70- return 1
71- fi
72- echo " $result " | tail -n +2 # Skip header row
63+ local query=" $1 "
64+ local result
65+ local bendsql_cmd=" bendsql"
66+
67+ # Add DSN if specified
68+ if [ -n " $DSN " ]; then
69+ bendsql_cmd=" $bendsql_cmd --dsn=\" $DSN \" "
70+ fi
71+
72+ bendsql_cmd=" $bendsql_cmd --query=\" $query \" "
73+
74+ # Use eval to properly handle quotes in the command
75+ result=$( eval " $bendsql_cmd " 2>&1 )
76+ if [ $? -ne 0 ]; then
77+ return 1
78+ fi
79+ echo " $result " | tail -n +2 # Skip header row
7380}
7481
7582# Get all databases
7683echo " Fetching list of databases..."
7784databases=$( execute_query " SELECT name FROM system.databases" )
7885if [ $? -ne 0 ]; then
79- echo " Error: Failed to get databases list" >&2
80- exit 1
86+ echo " Error: Failed to get databases list" >&2
87+ exit 1
8188fi
8289
8390# Process each database
8491while IFS= read -r db; do
85- if [ -z " $db " ]; then
86- continue
87- fi
88-
89- echo " Processing database: $db "
90-
91- # Try to get columns directly
92- columns_query=" SELECT * FROM system.columns WHERE database = '$db '"
93- if ! execute_query " $columns_query " > /dev/null 2>&1 ; then
94- # If error, get tables first
95- tables=$( execute_query " SELECT name FROM system.tables WHERE database = '$db '" )
96- if [ $? -ne 0 ]; then
97- echo " Error: Failed to get tables for database '$db '" >&2
98- continue
99- fi
100-
101- # Process each table
102- while IFS= read -r table; do
103- if [ -z " $table " ]; then
104- continue
105- fi
106-
107- echo " Processing table: $table "
108- # Try to get columns for this table
109- table_columns_query=" SELECT * FROM system.columns WHERE database = '$db ' AND table = '$table '"
110- if ! execute_query " $table_columns_query " > /dev/null 2>&1 ; then
111- echo " Error encountered for database: '$db ', table: '$table '"
112- fi
113- done <<< " $tables"
114- fi
115- done <<< " $databases"
92+ if [ -z " $db " ]; then
93+ continue
94+ fi
95+
96+ echo " Processing database: $db "
97+
98+ # Try to get columns directly
99+ columns_query=" SELECT * FROM system.columns WHERE database = '$db '"
100+ if ! execute_query " $columns_query " > /dev/null 2>&1 ; then
101+ # If error, get tables first
102+ tables=$( execute_query " SELECT name FROM system.tables WHERE database = '$db '" )
103+ if [ $? -ne 0 ]; then
104+ echo " Error: Failed to get tables for database '$db '" >&2
105+ continue
106+ fi
107+
108+ # Process each table
109+ while IFS= read -r table; do
110+ if [ -z " $table " ]; then
111+ continue
112+ fi
113+
114+ echo " Processing table: $table "
115+ # Try to get columns for this table
116+ table_columns_query=" SELECT * FROM system.columns WHERE database = '$db ' AND table = '$table '"
117+ if ! execute_query " $table_columns_query " > /dev/null 2>&1 ; then
118+ echo " Error encountered for database: '$db ', table: '$table '"
119+ fi
120+ done <<< " $tables"
121+ fi
122+ done <<< " $databases"
116123
117124echo " Processing complete."
0 commit comments