37 lines
749 B
Bash
37 lines
749 B
Bash
#!/bin/bash
|
|
Select() {
|
|
#Select table [col1 [col2 [..]]] < WHERE_key WHERE_operator WHERE_value
|
|
# [WHERE_key WHERE_operator WHERE_value
|
|
# […]]
|
|
local \
|
|
table="$1" \
|
|
col \
|
|
columns \
|
|
operator \
|
|
results \
|
|
where_statement
|
|
shift
|
|
for col
|
|
do
|
|
(( ${#columns} )) && columns+=','
|
|
columns+="$col"
|
|
done
|
|
while read key operator value
|
|
do
|
|
(( ${#where_statement} )) && where_statement+=( "AND" )
|
|
where_statement+=( "$key $operator "'"'"${value//\"/\"\"}"'"' )
|
|
done
|
|
echo "SELECT IFNULL(" \
|
|
"(SELECT $columns FROM $table" \
|
|
"WHERE ${where_statement[@]})" \
|
|
",'SQL::Select:not found'" \
|
|
");" >&3
|
|
read -u 4 results
|
|
if ! [[ $results == "SQL::Select:not found" ]]
|
|
then
|
|
echo "$results"
|
|
else
|
|
return 1
|
|
fi
|
|
}
|