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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 use vars qw( @ISA @EXPORT );
42 @ISA = qw( Exporter );
45 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
55 system( "mysqladmin create $database --user=$user --password=$password" ) == 0 or
56 die qq(ERROR: Could not create database "$database"!\n);
64 # Martin A. Hansen, May 2008.
66 # Checks if a given database exists. Returns 1 if so,
69 my ( $database, # MySQL database
70 $user, # MySQL username
71 $pass, # MySQL password
78 @databases = list_databases( $user, $pass );
80 if ( grep /^$database$/i, @databases ) {
90 # Martin A. Hansen, May 2008.
92 # Returns a list of databases available.
94 my ( $user, # MySQL username
95 $pass, # MySQL password
102 @databases = Maasha::Common::run_and_return( "mysqlshow", "--user=$user --password=$pass" );
104 splice @databases, 0, 3;
108 map { s/^\|\s+([^\s]+)\s+\|$/$1/ } @databases;
110 return wantarray ? @databases : \@databases;
120 my ( $sth, $errstr );
122 if ( not $sth = $dbh->prepare( $sql ) )
124 $errstr = $DBI::errstr;
127 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
130 if ( not $sth->execute )
132 $errstr = $DBI::errstr;
135 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
144 # Niels Larsen, April 2003.
146 # Executes a given sql query and returns the result as a hash
147 # or hash reference. The keys are set to the values of the given
150 my ( $dbh, # Database handle
152 $key, # Key string, like "id", "name", ..
157 my ( $sth, $hash, $errstr );
159 if ( not $sth = $dbh->prepare( $sql ) )
161 $errstr = $DBI::errstr;
164 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
167 if ( not $sth->execute )
169 $errstr = $DBI::errstr;
172 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
175 if ( $hash = $sth->fetchall_hashref( $key ) )
177 return wantarray ? %{ $hash } : $hash;
181 $errstr = $DBI::errstr;
184 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
193 # Niels Larsen, April 2003.
195 # Executes a given sql query and returns the result as a table
196 # or table reference.
198 my ( $dbh, # Database handle
200 $out, # Output specification, see DBI documentation.
205 my ( $sth, $table, $errstr, @status );
207 if ( not $sth = $dbh->prepare( $sql ) )
209 $errstr = $DBI::errstr;
212 die qq(ERROR: $errstr, "SQL PREPARE ERROR" );
215 if ( not $sth->execute )
217 $errstr = $DBI::errstr;
220 die qq(ERROR: $errstr, "SQL EXECUTE ERROR" );
223 if ( $table = $sth->fetchall_arrayref( $out ) )
225 return wantarray ? @{ $table } : $table;
229 $errstr = $DBI::errstr;
232 die qq(ERROR: $errstr, "DATABASE RETRIEVE ERROR" );
237 sub query_hashref_list
239 # Martin A. Hansen, May 2008.
241 # Executes a SQL query and return the result
242 # as a list of hashrefs.
244 my ( $dbh, # database handle
248 # Returns datastructure.
250 my $table = $dbh->selectall_arrayref( $sql, { Slice => {} } );
252 return wantarray ? @{ $table } : $table;
262 request( $dbh, "drop table $table" );
273 @list = query_array( $dbh, "show tables" );
276 @list = map { $_->[0] } @list;
281 return wantarray ? @list : \@list;
291 if ( grep /^$name$/, list_tables( $dbh ) ) {
301 # Martin A. Hansen, May 2008.
303 # Given a database, user and password,
304 # obtains a database handle if the databse exists.
306 my ( $database, # MySQL database
308 $pass, # MySQL password
315 Maasha::Common::error( qq(Database "$database" does not exist) ) if not database_exists( $database, $user, $pass );
318 "dbi:mysql:$database",
325 ShowErrorStatement => 1,
332 Maasha::Common::error( qq($DBI::errstr) );
342 if ( not $dbh->disconnect )
344 die qq(ERROR: $DBI::errstr );
351 # Martin A. Hansen, April 2003.
353 # updates the content of a single table cell
355 my ( $dbh, # database handle
357 $column, # column where updating
358 $old_val, # the old cell content
359 $new_val, # the new cell content
362 my ( $sql, $count, $count_sql );
364 $count_sql = qq( SELECT $column FROM $table WHERE $column="$old_val"; );
366 $count = scalar query_array( $dbh, $count_sql );
370 warn qq(WARNING: More than one entry found "$count_sql"\n);
372 elsif ( $count == 0 )
375 die qq(ERROR: entry not found "$count_sql"\n);
379 $sql = qq( UPDATE $table SET $column="$new_val" WHERE $column="$old_val"; );
380 request( $dbh, $sql );
389 # Martin A. Hansen, April 2003.
391 # deletes a record form a table
393 my ( $dbh, # database handle
395 $field, # field e.g. rec no
396 $pattern, # specific pattern
401 $sql = qq(DELETE FROM $table WHERE $field = "$pattern";);
403 request( $dbh, $sql );
411 # Martin A. Hansen, April 2003.
413 # adds a record to a table;
415 my ( $dbh, # database handle
417 $fields, # row to be inserted
420 my ( $sql, $field, @fields, $quote_sql );
422 foreach $field ( @{ $fields } )
424 if ( $field eq "NULL" or $field eq '' ) {
425 push @fields, "NULL";
427 push @fields, $dbh->quote( $field );
431 $sql = "INSERT INTO $table VALUES ( " . join( ", ", @fields ) . " );";
433 request( $dbh, $sql );
441 # Martin A. Hansen, April 2003.
443 # inserts a column in a table
445 my ( $dbh, # database handle
447 $column, # name of column
448 $type, # variable type
449 $index, # enable index
455 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type, INDEX $column" . "_index ( $column ) );";
457 $sql = "ALTER TABLE $table ADD COLUMN ( $column $type );";
460 request( $dbh, $sql );
468 # Martin A. Hansen, April 2003.
470 # deletes a column from a table
472 my ( $dbh, # databse handle
474 $column, # column to be deleted
479 $sql = "ALTER TABLE $table DROP COLUMN $column;";
481 request( $dbh, $sql );
489 # Martin A. Hansen, January 2004.
491 # loads , seperated file in to sql table
493 my ( $dbh, # database handle object
494 $path, # filename with path
495 $table, # table to load data into
496 $delimiter, # column delimiter - OPTIONAL
499 # returns database handle object
505 $sql = qq( LOAD DATA LOCAL INFILE "$path" INTO TABLE $table FIELDS TERMINATED BY '$delimiter' );
507 SQL::request( $dbh, $sql );
511 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<