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);
65 # Martin A. Hansen, May 2008.
67 # Checks if a given database exists. Returns 1 if so,
70 my ( $database, # MySQL database
71 $user, # MySQL username
72 $pass, # MySQL password
79 @databases = list_databases( $user, $pass );
81 if ( grep /^$database$/i, @databases ) {
91 # Martin A. Hansen, May 2008.
93 # Returns a list of databases available.
95 my ( $user, # MySQL username
96 $pass, # MySQL password
103 @databases = Maasha::Common::run_and_return( "mysqlshow", "--user=$user --password=$pass" );
105 splice @databases, 0, 3;
109 map { s/^\|\s+([^\s]+)\s+\|$/$1/ } @databases;
111 return wantarray ? @databases : \@databases;
121 my ( $sth, $errstr );
123 if ( not $sth = $dbh->prepare( $sql ) )
125 $errstr = $DBI::errstr;
128 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
131 if ( not $sth->execute )
133 $errstr = $DBI::errstr;
136 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
145 # Niels Larsen, April 2003.
147 # Executes a given sql query and returns the result as a hash
148 # or hash reference. The keys are set to the values of the given
151 my ( $dbh, # Database handle
153 $key, # Key string, like "id", "name", ..
158 my ( $sth, $hash, $errstr );
160 if ( not $sth = $dbh->prepare( $sql ) )
162 $errstr = $DBI::errstr;
165 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
168 if ( not $sth->execute )
170 $errstr = $DBI::errstr;
173 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
176 if ( $hash = $sth->fetchall_hashref( $key ) )
178 return wantarray ? %{ $hash } : $hash;
182 $errstr = $DBI::errstr;
185 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
194 # Niels Larsen, April 2003.
196 # Executes a given sql query and returns the result as a table
197 # or table reference.
199 my ( $dbh, # Database handle
201 $out, # Output specification, see DBI documentation.
206 my ( $sth, $table, $errstr, @status );
208 if ( not $sth = $dbh->prepare( $sql ) )
210 $errstr = $DBI::errstr;
213 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
216 if ( not $sth->execute )
218 $errstr = $DBI::errstr;
221 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
224 if ( $table = $sth->fetchall_arrayref( $out ) )
226 return wantarray ? @{ $table } : $table;
230 $errstr = $DBI::errstr;
233 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
238 sub query_hashref_list
240 # Martin A. Hansen, May 2008.
242 # Executes a SQL query and return the result
243 # as a list of hashrefs.
245 my ( $dbh, # database handle
249 # Returns datastructure.
251 my $table = $dbh->selectall_arrayref( $sql, { Slice => {} } );
253 return wantarray ? @{ $table } : $table;
263 request( $dbh, "drop table $table" );
274 @list = query_array( $dbh, "show tables" );
277 @list = map { $_->[0] } @list;
282 return wantarray ? @list : \@list;
292 if ( grep /^$name$/, list_tables( $dbh ) ) {
302 # Martin A. Hansen, May 2008.
304 # Given a database, user and password,
305 # obtains a database handle if the databse exists.
307 my ( $database, # MySQL database
309 $pass, # MySQL password
316 Maasha::Common::error( qq(Database "$database" does not exist) ) if not database_exists( $database, $user, $pass );
319 "dbi:mysql:$database",
326 ShowErrorStatement => 1,
333 Maasha::Common::error( qq($DBI::errstr) );
343 if ( not $dbh->disconnect )
345 die qq(ERROR: $DBI::errstr );
352 # Martin A. Hansen, April 2003.
354 # updates the content of a single table cell
356 my ( $dbh, # database handle
358 $column, # column where updating
359 $old_val, # the old cell content
360 $new_val, # the new cell content
363 my ( $sql, $count, $count_sql );
365 $count_sql = qq( SELECT $column FROM $table WHERE $column="$old_val"; );
367 $count = scalar query_array( $dbh, $count_sql );
371 warn qq(WARNING: More than one entry found "$count_sql"\n);
373 elsif ( $count == 0 )
376 die qq(ERROR: entry not found "$count_sql"\n);
380 $sql = qq( UPDATE $table SET $column="$new_val" WHERE $column="$old_val"; );
381 request( $dbh, $sql );
390 # Martin A. Hansen, April 2003.
392 # deletes a record form a table
394 my ( $dbh, # database handle
396 $field, # field e.g. rec no
397 $pattern, # specific pattern
402 $sql = qq(DELETE FROM $table WHERE $field = "$pattern";);
404 request( $dbh, $sql );
412 # Martin A. Hansen, April 2003.
414 # adds a record to a table;
416 my ( $dbh, # database handle
418 $fields, # row to be inserted
421 my ( $sql, $field, @fields, $quote_sql );
423 foreach $field ( @{ $fields } )
425 if ( $field eq "NULL" or $field eq '' ) {
426 push @fields, "NULL";
428 push @fields, $dbh->quote( $field );
432 $sql = "INSERT INTO $table VALUES ( " . join( ", ", @fields ) . " );";
434 request( $dbh, $sql );
442 # Martin A. Hansen, April 2003.
444 # inserts a column in a table
446 my ( $dbh, # database handle
448 $column, # name of column
449 $type, # variable type
450 $index, # enable index
456 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type, INDEX $column" . "_index ( $column ) );";
458 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type );";
461 request( $dbh, $sql );
469 # Martin A. Hansen, April 2003.
471 # deletes a column from a table
473 my ( $dbh, # databse handle
475 $column, # column to be deleted
480 $sql = "ALTER TABLE $table DROP COLUMN $column;";
482 request( $dbh, $sql );
490 # Martin A. Hansen, January 2004.
492 # loads , seperated file in to sql table
494 my ( $dbh, # database handle object
495 $path, # filename with path
496 $table, # table to load data into
497 $delimiter, # column delimiter - OPTIONAL
500 # returns database handle object
506 $sql = qq( LOAD DATA LOCAL INFILE "$path" INTO TABLE $table FIELDS TERMINATED BY '$delimiter' );
508 SQL::request( $dbh, $sql );
512 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<