3 # Copyright (C) 2006 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines for manipulation of MySQL via the DBI module.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 use vars qw( @ISA @EXPORT );
43 @ISA = qw( Exporter );
46 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
56 system( "mysqladmin create $database --user=$user --password=$password" ) == 0 or
57 die qq(ERROR: Could not create database "$database"!\n);
70 die qq(ERROR: Protected database: "$database!\n" ) if $database =~/^(mysql|information_schema)$/i;
71 system( "mysqladmin drop $database --force --user=$user --password=$password > /dev/null 2>&1" ) == 0 or
72 die qq(ERROR: Could not drop database "$database"!\n);
80 # Martin A. Hansen, May 2008.
82 # Checks if a given database exists. Returns 1 if so,
85 my ( $database, # MySQL database
86 $user, # MySQL username
87 $pass, # MySQL password
94 @databases = list_databases( $user, $pass );
96 if ( grep /^$database$/i, @databases ) {
106 # Martin A. Hansen, May 2008.
108 # Returns a list of databases available.
110 my ( $user, # MySQL username
111 $pass, # MySQL password
118 @databases = Maasha::Common::run_and_return( "mysqlshow", "--user=$user --password=$pass" );
120 splice @databases, 0, 3;
124 map { s/^\|\s+([^\s]+)\s+\|$/$1/ } @databases;
126 return wantarray ? @databases : \@databases;
136 my ( $sth, $errstr );
138 if ( not $sth = $dbh->prepare( $sql ) )
140 $errstr = $DBI::errstr;
143 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
146 if ( not $sth->execute )
148 $errstr = $DBI::errstr;
151 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
160 # Niels Larsen, April 2003.
162 # Executes a given sql query and returns the result as a hash
163 # or hash reference. The keys are set to the values of the given
166 my ( $dbh, # Database handle
168 $key, # Key string, like "id", "name", ..
173 my ( $sth, $hash, $errstr );
175 if ( not $sth = $dbh->prepare( $sql ) )
177 $errstr = $DBI::errstr;
180 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
183 if ( not $sth->execute )
185 $errstr = $DBI::errstr;
188 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
191 if ( $hash = $sth->fetchall_hashref( $key ) )
193 return wantarray ? %{ $hash } : $hash;
197 $errstr = $DBI::errstr;
200 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
209 # Niels Larsen, April 2003.
211 # Executes a given sql query and returns the result as a table
212 # or table reference.
214 my ( $dbh, # Database handle
216 $out, # Output specification, see DBI documentation.
221 my ( $sth, $table, $errstr, @status );
223 if ( not $sth = $dbh->prepare( $sql ) )
225 $errstr = $DBI::errstr;
228 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
231 if ( not $sth->execute )
233 $errstr = $DBI::errstr;
236 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
239 if ( $table = $sth->fetchall_arrayref( $out ) )
241 return wantarray ? @{ $table } : $table;
245 $errstr = $DBI::errstr;
248 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
253 sub query_hashref_list
255 # Martin A. Hansen, May 2008.
257 # Executes a SQL query and return the result
258 # as a list of hashrefs.
260 my ( $dbh, # database handle
264 # Returns datastructure.
266 my $table = $dbh->selectall_arrayref( $sql, { Slice => {} } );
268 return wantarray ? @{ $table } : $table;
278 request( $dbh, "drop table $table" );
289 @list = query_array( $dbh, "show tables" );
292 @list = map { $_->[0] } @list;
297 return wantarray ? @list : \@list;
307 if ( grep /^$name$/, list_tables( $dbh ) ) {
317 # Martin A. Hansen, May 2008.
319 # Given a database, user and password,
320 # obtains a database handle if the databse exists.
322 my ( $database, # MySQL database
324 $pass, # MySQL password
331 Maasha::Common::error( qq(Database "$database" does not exist) ) if not database_exists( $database, $user, $pass );
334 "dbi:mysql:$database",
341 ShowErrorStatement => 1,
348 Maasha::Common::error( qq($DBI::errstr) );
358 if ( not $dbh->disconnect )
360 die qq(ERROR: $DBI::errstr );
367 # Martin A. Hansen, April 2003.
369 # updates the content of a single table cell
371 my ( $dbh, # database handle
373 $column, # column where updating
374 $old_val, # the old cell content
375 $new_val, # the new cell content
378 my ( $sql, $count, $count_sql );
380 $count_sql = qq( SELECT $column FROM $table WHERE $column="$old_val"; );
382 $count = scalar query_array( $dbh, $count_sql );
386 warn qq(WARNING: More than one entry found "$count_sql"\n);
388 elsif ( $count == 0 )
391 die qq(ERROR: entry not found "$count_sql"\n);
395 $sql = qq( UPDATE $table SET $column="$new_val" WHERE $column="$old_val"; );
396 request( $dbh, $sql );
405 # Martin A. Hansen, April 2003.
407 # deletes a record form a table
409 my ( $dbh, # database handle
411 $field, # field e.g. rec no
412 $pattern, # specific pattern
417 $sql = qq(DELETE FROM $table WHERE $field = "$pattern";);
419 request( $dbh, $sql );
427 # Martin A. Hansen, April 2003.
429 # adds a record to a table;
431 my ( $dbh, # database handle
433 $fields, # row to be inserted
436 my ( $sql, $field, @fields, $quote_sql );
438 foreach $field ( @{ $fields } )
440 if ( $field eq "NULL" or $field eq '' ) {
441 push @fields, "NULL";
443 push @fields, $dbh->quote( $field );
447 $sql = "INSERT INTO $table VALUES ( " . join( ", ", @fields ) . " );";
449 request( $dbh, $sql );
457 # Martin A. Hansen, April 2003.
459 # inserts a column in a table
461 my ( $dbh, # database handle
463 $column, # name of column
464 $type, # variable type
465 $index, # enable index
471 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type, INDEX $column" . "_index ( $column ) );";
473 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type );";
476 request( $dbh, $sql );
484 # Martin A. Hansen, April 2003.
486 # deletes a column from a table
488 my ( $dbh, # databse handle
490 $column, # column to be deleted
495 $sql = "ALTER TABLE $table DROP COLUMN $column;";
497 request( $dbh, $sql );
505 # Martin A. Hansen, January 2004.
507 # loads , seperated file in to sql table
509 my ( $dbh, # database handle object
510 $path, # filename with path
511 $table, # table to load data into
512 $delimiter, # column delimiter - OPTIONAL
515 # returns database handle object
521 $sql = qq( LOAD DATA LOCAL INFILE "$path" INTO TABLE $table FIELDS TERMINATED BY '$delimiter' );
523 request( $dbh, $sql );
527 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<