-
Notifications
You must be signed in to change notification settings - Fork 0
/
gensfw_session_procs
executable file
·149 lines (125 loc) · 3.37 KB
/
gensfw_session_procs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
MISSING_DATABASE()
{
local -i result
local query=(
"SELECT COUNT(*)"
"FROM information_schema.SCHEMATA"
"WHERE SCHEMA_NAME='${database}'")
result=$(mysql -BN -e "${query[*]}" )
[ $result -eq 0 ]
}
MISSING_TABLE()
{
local -i result
local query=(
"SELECT COUNT(*)"
"FROM information_schema.TABLES"
"WHERE TABLE_SCHEMA='${database}'"
"AND TABLE_NAME='${table}'" )
result=$(mysql -BN -e "${query[*]}" )
[ $result -eq 0 ]
}
show_table_list()
{
local -a table_array
local query=(
"SELECT TABLE_NAME"
" FROM information_schema.TABLES"
" WHERE TABLE_SCHEMA='${database}'")
local IFS=$'\n'
table_array=( $(mysql -BN -e "${query[*]}" ) )
if [ $? -eq 0 ]; then
echo "${#table_array[@]} available tables:" >&2
printf " '%s'\n" "${table_array[@]}" >&2
echo >&2
fi
}
show_table_fields()
{
local table="$1"
local -a fields_array field_array
local -a query=(
"SELECT COLUMN_NAME, COLUMN_TYPE"
" FROM information_schema.COLUMNS"
" WHERE TABLE_SCHEMA='${database}'"
" AND TABLE_NAME='${table}'"
)
local IFS=$'\n'
fields_array=( $( mysql -BN -e "${query[*]}" ) )
echo >&2
echo "Fields of table '$table'" >&2
IFS=$'\t'
# get max length of field names:
local -i tflen flen=0
for field in "${fields_array[@]}"; do
field_array=( $field )
tflen="${#field_array[0]}"
if [ "${tflen}" -gt "$flen" ]; then
flen="${tflen}"
fi
done
local format="%-${flen}s : %s"
for field in "${fields_array[@]}"; do
field_array=( $field )
fname="${field_array[0]}"
ftype="${field_array[1]}"
printf "$format\n" "$fname" "$ftype" >&2
done
}
get_source_path()
{
# Must include both the colon AND the space to trim
# the spaces from the path in the array:
local IFS=": "
local -a arr=( $( whereis gensfw_session_procs ) )
zpath=$( readlink "${arr[1]}" )
zpath="${zpath%%gensfw_session_procs}"
}
write_table_procs()
{
local -a aquery=(
"SELECT"
"COLUMN_NAME, DATA_TYPE, COLUMN_KEY, COLUMN_TYPE"
"FROM information_schema.COLUMNS"
"WHERE TABLE_SCHEMA='${database}'"
"AND TABLE_NAME='${table}'"
)
mysql -X -e "${aquery[*]}" | xsltproc "${xslpath}" -
}
show_usage()
{
echo "Usage:" >&2
echo "$ gensfw_session_procs [database name] [session information table name]" >&2
echo >&2
echo "Calling the command with a valid database name and a missing" >&2
echo "or invalid table name will display the list of table in the" >&2
echo "database." >&2
echo >&2
}
get_source_path
xslpath="${zpath}gensfw_session_procs.xsl"
if [ $# -lt 1 ]; then
show_usage
else
database="$1"
if MISSING_DATABASE; then
echo "Database '$database' does not exist on this server." >&2
echo >&2
elif [ $# -lt 2 ]; then
show_usage
echo "No table requested from database '$database'" >&2
show_table_list
else
table="$2"
if MISSING_TABLE; then
echo "Table '$table' does not exist in database '$database'" >&2
show_table_list
else
show_table_fields "$table"
write_table_procs
exit 0
fi
fi
fi
exit 1