Magellan Linux

Diff of /alx-src/branches/alxconf-060/functions/mysqlfunctions

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

alx-src/branches/alxconf_20060908/alxconfig-ng/functions/mysqlfunctions revision 543 by niro, Wed Feb 4 19:51:39 2009 UTC alx-src/branches/alxconf-060/functions/mysqlfunctions revision 2152 by niro, Tue May 17 21:33:57 2011 UTC
# Line 1  Line 1 
1  #!/bin/bash  # $Id$
2    # mysql functions for bash
 #  
 # mysql functions for the bash  
 # Version 0.1  
 #  
 # Niels Rogalla <niro@magellan-linux.de>  
 #  
 # $Header: /home/cvsd/alx-cvs/alx-src/alxconfig-ng/functions/mysqlfunctions,v 1.8 2005-10-09 21:32:33 niro Exp $  
3    
4  mysql_command()  mysql_command()
5  {  {
# Line 17  mysql_command() Line 10  mysql_command()
10   local SQL_DB   local SQL_DB
11   local SQL_COMMAND   local SQL_COMMAND
12    
13   SQL_USER=$1   SQL_USER="$1"
14   SQL_PASS=$2   SQL_PASS="$2"
15   SQL_HOST=$3   SQL_HOST="$3"
16   SQL_DB=$4   SQL_DB="$4"
17   SQL_COMMAND=$5   SQL_COMMAND="$5"
18    
19   #fallback (SQL_OPTS not, they are optional )   # fallback (SQL_OPTS not, they are optional )
20   if [ -z "${SQL_USER}" \   if [ -z "${SQL_USER}" \
21   -o -z "${SQL_PASS}" \   -o -z "${SQL_PASS}" \
22   -o -z "${SQL_HOST}" \   -o -z "${SQL_HOST}" \
# Line 36  mysql_command() Line 29  mysql_command()
29   echo   echo
30   return 1   return 1
31   fi   fi
32    
33   mysql \   mysql \
34   --user="${SQL_USER}" \   --user="${SQL_USER}" \
35   --password="${SQL_PASS}" \   --password="${SQL_PASS}" \
# Line 46  mysql_command() Line 39  mysql_command()
39   --skip-column-names \   --skip-column-names \
40   --execute="${SQL_COMMAND}" \   --execute="${SQL_COMMAND}" \
41   || return 1   || return 1
42    
43   return 0   return 0
44  }  }
45    
# Line 67  mysql_enum_colums() Line 60  mysql_enum_colums()
60   local key   local key
61   local default   local default
62   local extra   local extra
63    
64   SQL_USER=$1   SQL_USER=$1
65   SQL_PASS=$2   SQL_PASS=$2
66   SQL_HOST=$3   SQL_HOST=$3
67   SQL_DB=$4   SQL_DB=$4
68   SQL_TABLE=$5   SQL_TABLE=$5
69    
70   #show the column names ?   # show the column names ?
71   if [ -n "${6}" -a "${6}" == "show" ]   if [ -n "${6}" -a "${6}" == "show" ]
72   then   then
73   SHOWTABLES=true   SHOWTABLES=true
# Line 82  mysql_enum_colums() Line 75  mysql_enum_colums()
75   SHOWTABLES=false   SHOWTABLES=false
76   fi   fi
77    
78   #fallback (SQL_OPTS not, they are optional )   # fallback (SQL_OPTS not, they are optional )
79   if [ -z "${SQL_USER}" \   if [ -z "${SQL_USER}" \
80   -o -z "${SQL_PASS}" \   -o -z "${SQL_PASS}" \
81   -o -z "${SQL_HOST}" \   -o -z "${SQL_HOST}" \
# Line 148  mysqldo() Line 141  mysqldo()
141   "$@" \   "$@" \
142   && return 0 || return 1   && return 0 || return 1
143  }  }
144    
145    # read tables and evaluate all variables
146    # cmd:    evaluate_table cfg_network
147    # result: cfg_network_hostname=DUMMY_HOSTNAME
148    evaluate_table()
149    {
150     local table="$1"
151     local where_statement
152    
153     if [[ -z $2 ]]
154     then
155     where_statement="where serial='${ALX_SERIAL}'"
156     else
157     where_statement="$2"
158     fi
159    
160     eval $(mysql \
161     --user="${SQL_USER}" \
162     --password="${SQL_PASS}" \
163     --host="${SQL_HOST}" \
164     --database="${SQL_DB}" \
165     --xml \
166     --execute="select * from ${table} ${where_statement}" \
167     | xml sel -T -t -m //row/field \
168     -v "concat('${table}_', @name, '=', '\"', self::field, '\"')" -n)
169    }
170    
171    # runs a statement and evaluate the resulting columns
172    # cmd:    evaluate_command "select serial from cfg_serial where serial=10"
173    # result: serial=10
174    # or with a prefix given
175    # cmd:    evaluate_command "select serial from cfg_serial where serial=10" "cfg_serial"
176    # result: cfg_serial_serial=10
177    evaluate_statement()
178    {
179     local statement="$1"
180     local prefix="$2"
181    
182     if [[ ! -z ${prefix} ]]
183     then
184     prefix="${prefix}_"
185     fi
186    
187     eval $(mysql \
188     --user="${SQL_USER}" \
189     --password="${SQL_PASS}" \
190     --host="${SQL_HOST}" \
191     --database="${SQL_DB}" \
192     --xml \
193     --execute="${statement}" \
194     | xml sel -T -t -m //row/field \
195     -v "concat('${prefix}', @name, '=', '\"', self::field, '\"')" -n)
196    }

Legend:
Removed from v.543  
changed lines
  Added in v.2152