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 );
222 if ( not $sth = $dbh->prepare( $sql ) )
224 $errstr = $DBI::errstr;
227 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
230 if ( not $sth->execute )
232 $errstr = $DBI::errstr;
235 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
238 if ( $table = $sth->fetchall_arrayref( $out ) )
240 return wantarray ? @{ $table } : $table;
244 $errstr = $DBI::errstr;
247 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
252 sub query_hashref_list
254 # Martin A. Hansen, May 2008.
256 # Executes a SQL query and return the result
257 # as a list of hashrefs.
259 my ( $dbh, # database handle
263 # Returns datastructure.
265 my $table = $dbh->selectall_arrayref( $sql, { Slice => {} } );
267 return wantarray ? @{ $table } : $table;
277 request( $dbh, "drop table $table" );
288 @list = query_array( $dbh, "show tables" );
291 @list = map { $_->[0] } @list;
296 return wantarray ? @list : \@list;
306 if ( grep /^$name$/, list_tables( $dbh ) ) {
316 # Martin A. Hansen, May 2008.
318 # Given a database, user and password,
319 # obtains a database handle if the databse exists.
321 my ( $database, # MySQL database
323 $pass, # MySQL password
330 Maasha::Common::error( qq(Database "$database" does not exist) ) if not database_exists( $database, $user, $pass );
333 "dbi:mysql:$database",
340 ShowErrorStatement => 1,
347 Maasha::Common::error( qq($DBI::errstr) );
357 if ( not $dbh->disconnect )
359 die qq(ERROR: $DBI::errstr );
366 # Martin A. Hansen, April 2003.
368 # updates the content of a single table cell
370 my ( $dbh, # database handle
372 $column, # column where updating
373 $old_val, # the old cell content
374 $new_val, # the new cell content
377 my ( $sql, $count, $count_sql );
379 $count_sql = qq( SELECT $column FROM $table WHERE $column="$old_val"; );
381 $count = scalar query_array( $dbh, $count_sql );
385 warn qq(WARNING: More than one entry found "$count_sql"\n);
387 elsif ( $count == 0 )
390 die qq(ERROR: entry not found "$count_sql"\n);
394 $sql = qq( UPDATE $table SET $column="$new_val" WHERE $column="$old_val"; );
395 request( $dbh, $sql );
404 # Martin A. Hansen, April 2003.
406 # deletes a record form a table
408 my ( $dbh, # database handle
410 $field, # field e.g. rec no
411 $pattern, # specific pattern
416 $sql = qq(DELETE FROM $table WHERE $field = "$pattern";);
418 request( $dbh, $sql );
426 # Martin A. Hansen, April 2003.
428 # adds a record to a table;
430 my ( $dbh, # database handle
432 $fields, # row to be inserted
435 my ( $sql, $field, @fields, $quote_sql );
437 foreach $field ( @{ $fields } )
439 if ( $field eq "NULL" or $field eq '' ) {
440 push @fields, "NULL";
442 push @fields, $dbh->quote( $field );
446 $sql = "INSERT INTO $table VALUES ( " . join( ", ", @fields ) . " );";
448 request( $dbh, $sql );
456 # Martin A. Hansen, April 2003.
458 # inserts a column in a table
460 my ( $dbh, # database handle
462 $column, # name of column
463 $type, # variable type
464 $index, # enable index
470 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type, INDEX $column" . "_index ( $column ) );";
472 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type );";
475 request( $dbh, $sql );
483 # Martin A. Hansen, April 2003.
485 # deletes a column from a table
487 my ( $dbh, # databse handle
489 $column, # column to be deleted
494 $sql = "ALTER TABLE $table DROP COLUMN $column;";
496 request( $dbh, $sql );
504 # Martin A. Hansen, January 2004.
506 # loads , seperated file in to sql table
508 my ( $dbh, # database handle object
509 $path, # filename with path
510 $table, # table to load data into
511 $delimiter, # column delimiter - OPTIONAL
514 # returns database handle object
520 $sql = qq( LOAD DATA LOCAL INFILE "$path" INTO TABLE $table FIELDS TERMINATED BY '$delimiter' );
522 request( $dbh, $sql );
526 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<