@@ -6,7 +6,7 @@ SHOW_HELP=false
6
6
7
7
# Function to display help
8
8
display_help () {
9
- cat << EOF
9
+ cat << EOF
10
10
Usage: $0 [OPTIONS]
11
11
12
12
Options:
32
32
33
33
# Parse command line arguments
34
34
while [[ " $# " -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
41
48
done
42
49
43
50
# Show help if requested
44
51
if [ " $SHOW_HELP " = true ]; then
45
- display_help
46
- exit 0
52
+ display_help
53
+ exit 0
47
54
fi
48
55
49
56
# Check for DSN in environment variable if not specified via command line
50
57
if [ -z " $DSN " ] && [ -n " $BENDSQL_DSN " ]; then
51
- DSN=" $BENDSQL_DSN "
58
+ DSN=" $BENDSQL_DSN "
52
59
fi
53
60
54
61
# Function to execute bendsql query and handle errors
55
62
execute_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
73
80
}
74
81
75
82
# Get all databases
76
83
echo " Fetching list of databases..."
77
84
databases=$( execute_query " SELECT name FROM system.databases" )
78
85
if [ $? -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
81
88
fi
82
89
83
90
# Process each database
84
91
while 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"
116
123
117
124
echo " Processing complete."
0 commit comments