2 # dbi.pl: DBI (mysql/pgsql/sqlite) database frontend.
4 # Version: v0.9a (20021124)
6 # Notes: based on db_mysql.pl
7 # overhauled to be 31337.
13 use vars qw($dbh $shm $bot_data_dir);
18 # &sqlOpenDB($dbname, $dbtype, $sqluser, $sqlpass, $nofail);
20 my ($db, $type, $user, $pass, $no_fail) = @_;
21 # this is a mess. someone fix it, please.
22 if ($type =~ /^SQLite(2)?$/i) {
23 $db = "dbname=$db.sqlite";
24 } elsif ($type =~ /^pg/i) {
29 my $dsn = "DBI:$type:$db";
31 # SQLHost should be unset for SQLite
32 if (exists $param{'SQLHost'} and $param{'SQLHost'}) {
33 $dsn .= ":$param{SQLHost}";
34 $hoststr = " to $param{'SQLHost'}";
36 # SQLite ignores $user and $pass
37 $dbh = DBI->connect($dsn, $user, $pass);
39 if ($dbh && !$dbh->err) {
40 &status("Opened $type connection$hoststr");
42 &ERROR("Cannot connect$hoststr.");
43 &ERROR("Since $type is not available, shutting down bot!");
44 &ERROR( $dbh->errstr ) if ($dbh);
49 return 0 if ($no_fail);
56 return 0 unless ($dbh);
58 my $x = $param{SQLHost};
59 my $hoststr = ($x) ? " to $x" : "";
61 &status("Closed DBI connection$hoststr.");
68 # Usage: &sqlQuote($str);
70 return $dbh->quote($_[0]);
74 # Usage: &sqlSelectMany($table, $select, [$where_href], [$other]);
75 # Return: $sth (Statement handle object)
77 my($table, $select, $where_href, $other) = @_;
78 my $query = "SELECT $select FROM $table";
81 if (!defined $select or $select =~ /^\s*$/) {
82 &WARN("sqlSelectMany: select == NULL.");
86 if (!defined $table or $table =~ /^\s*$/) {
87 &WARN("sqlSelectMany: table == NULL.");
92 my $where = &hashref2where($where_href);
93 $query .= " WHERE $where" if ($where);
95 $query .= " $other" if ($other);
97 if (!($sth = $dbh->prepare($query))) {
98 &ERROR("sqlSelectMany: prepare: $DBI::errstr");
104 return if (!$sth->execute);
110 # Usage: &sqlSelect($table, $select, [$where_href, [$other]);
111 # Return: scalar if one element, array if list of elements.
112 # Note: Suitable for one column returns, that is, one column in $select.
113 # Todo: Always return array?
115 my $sth = &sqlSelectMany(@_);
117 &WARN("sqlSelect failed.");
120 my @retval = $sth->fetchrow_array;
123 if (scalar @retval > 1) {
125 } elsif (scalar @retval == 1) {
133 # Usage: &sqlSelectColArray($table, $select, [$where_href], [$other]);
135 sub sqlSelectColArray {
136 my $sth = &sqlSelectMany(@_);
140 &WARN("sqlSelect failed.");
144 while (my @row = $sth->fetchrow_array) {
145 push(@retval, $row[0]);
153 # Usage: &sqlSelectColHash($table, $select, [$where_href], [$other], [$type]);
154 # Return: type = 1: $retval{ col2 }{ col1 } = 1;
155 # Return: no type: $retval{ col1 } = col2;
156 # Note: does not support $other, yet.
157 sub sqlSelectColHash {
158 my ($table, $select, $where_href, $other, $type) = @_;
159 my $sth = &sqlSelectMany($table, $select, $where_href, $other);
161 &WARN("sqlSelectColhash failed.");
166 if (defined $type and $type == 2) {
167 &DEBUG("sqlSelectColHash: type 2!");
168 while (my @row = $sth->fetchrow_array) {
169 $retval{$row[0]} = join(':', $row[1..$#row]);
171 &DEBUG("sqlSelectColHash: count => ".scalar(keys %retval) );
173 } elsif (defined $type and $type == 1) {
174 while (my @row = $sth->fetchrow_array) {
175 # reverse it to make it easier to count.
176 if (scalar @row == 2) {
177 $retval{$row[1]}{$row[0]} = 1;
178 } elsif (scalar @row == 3) {
179 $retval{$row[1]}{$row[0]} = 1;
181 # what to do if there's only one or more than 3?
185 while (my @row = $sth->fetchrow_array) {
186 $retval{$row[0]} = $row[1];
196 # Usage: &sqlSelectRowHash($table, $select, [$where_href]);
197 # Return: $hash{ col } = value;
198 # Note: useful for returning only one/first row of data.
199 sub sqlSelectRowHash {
200 my $sth = &sqlSelectMany(@_);
202 &WARN("sqlSelectRowHash failed.");
205 my $retval = $sth->fetchrow_hashref();
216 # End of SELECT functions.
220 # Usage: &sqlSet($table, $where_href, $data_href);
221 # Return: 1 for success, undef for failure.
223 my ($table, $where_href, $data_href) = @_;
225 if (!defined $table or $table =~ /^\s*$/) {
226 &WARN("sqlSet: table == NULL.");
230 if (!defined $data_href or ref($data_href) ne "HASH") {
231 &WARN("sqlSet: data_href == NULL.");
235 # any column can be NULL... so just get them all.
236 my $k = join(',', keys %{ $where_href } );
237 my $result = &sqlSelect($table, $k, $where_href);
238 # &DEBUG("result is not defined :(") if (!defined $result);
240 # this was hardwired to use sqlUpdate. sqlite does not do inserts on sqlUpdate.
241 if (defined $result) {
242 &sqlUpdate($table, $data_href, $where_href);
245 my %hash = %{ $where_href };
246 # add data_href values...
247 foreach (keys %{ $data_href }) {
248 $hash{ $_ } = ${ $data_href }{$_};
252 &sqlInsert($table, $data_href);
259 # Usage: &sqlUpdate($table, $data_href, $where_href);
261 my ($table, $data_href, $where_href) = @_;
263 if (!defined $data_href or ref($data_href) ne "HASH") {
264 &WARN("sqlSet: data_href == NULL.");
268 my $where = &hashref2where($where_href) if ($where_href);
269 my $update = &hashref2update($data_href) if ($data_href);
271 &sqlRaw("Update", "UPDATE $table SET $update WHERE $where");
277 # Usage: &sqlInsert($table, $data_href, $other);
279 my ($table, $data_href, $other) = @_;
280 # note: if $other == 1, add "DELAYED" to function instead.
282 if (!defined $data_href or ref($data_href) ne "HASH") {
283 &WARN("sqlInsert: data_href == NULL.");
287 my ($k_aref, $v_aref) = &hashref2array($data_href);
288 my @k = @{ $k_aref };
289 my @v = @{ $v_aref };
292 &WARN("sqlInsert: keys or vals is NULL.");
296 &sqlRaw("Insert($table)", sprintf(
297 "INSERT %s INTO %s (%s) VALUES (%s)",
298 ($other || ""), $table, join(',',@k), join(',',@v)
305 # Usage: &sqlReplace($table, $data_href);
307 my ($table, $data_href) = @_;
309 if (!defined $data_href or ref($data_href) ne "HASH") {
310 &WARN("sqlReplace: data_href == NULL.");
314 my ($k_aref, $v_aref) = &hashref2array($data_href);
315 my @k = @{ $k_aref };
316 my @v = @{ $v_aref };
319 &WARN("sqlReplace: keys or vals is NULL.");
323 &sqlRaw("Replace($table)", sprintf(
324 "REPLACE INTO %s (%s) VALUES (%s)",
325 $table, join(',',@k), join(',',@v)
332 # Usage: &sqlDelete($table, $where_href);
334 my ($table, $where_href) = @_;
336 if (!defined $where_href or ref($where_href) ne "HASH") {
337 &WARN("sqlDelete: where_href == NULL.");
341 my $where = &hashref2where($where_href);
343 &sqlRaw("Delete", "DELETE FROM $table WHERE $where");
349 # Usage: &sqlRaw($prefix, $query);
350 # Return: 1 for success, 0 for failure.
352 my ($prefix, $query) = @_;
355 if (!defined $query or $query =~ /^\s*$/) {
356 &WARN("sqlRaw: query == NULL.");
360 if (!($sth = $dbh->prepare($query))) {
361 &ERROR("Raw($prefix): !prepare => '$query'");
366 if (!$sth->execute) {
367 &ERROR("Raw($prefix): !execute => '$query'");
378 # Usage: &sqlRawReturn($query);
385 if (!defined $query or $query =~ /^\s*$/) {
386 &WARN("sqlRawReturn: query == NULL.");
390 if (!($sth = $dbh->prepare($query))) {
391 &ERROR("RawReturn: !prepare => '$query'");
396 if (!$sth->execute) {
397 &ERROR("RawReturn: !execute => '$query'");
402 while (my @row = $sth->fetchrow_array) {
403 push(@retval, $row[0]);
411 ####################################################################
412 ##### Misc DBI stuff...
418 if (!defined $href) {
419 &WARN("hashref2where: href == NULL.");
423 if (ref($href) ne "HASH") {
424 &WARN("hashref2where: href is not HASH ref (href => $href)");
428 my %hash = %{ $href };
429 foreach (keys %hash) {
432 if (s/^-//) { # as is.
434 delete $hash{'-'.$_};
436 $hash{$_} = &sqlQuote($v);
440 return join(' AND ', map { $_."=".$hash{$_} } keys %hash );
446 if (ref($href) ne "HASH") {
447 &WARN("hashref2update: href is not HASH ref.");
452 foreach (keys %{ $href }) {
454 my $v = ${ $href }{$_};
456 # is there a better way to do this?
457 if ($k =~ s/^-//) { # as is.
466 return join(', ', map { $_."=".$hash{$_} } sort keys %hash);
472 if (ref($href) ne "HASH") {
473 &WARN("hashref2update: href is not HASH ref.");
478 foreach (keys %{ $href }) {
480 my $v = ${ $href }{$_};
482 # is there a better way to do this?
483 if ($k =~ s/^-//) { # as is.
497 # Usage: &countKeys($table, [$col]);
499 my ($table, $col) = @_;
502 return (&sqlRawReturn("SELECT count($col) FROM $table"))[0];
506 # Usage: &sumKey($table, $col);
508 my ($table, $col) = @_;
510 return (&sqlRawReturn("SELECT sum($col) FROM $table"))[0];
514 # Usage: &randKey($table, $select);
516 my ($table, $select) = @_;
517 my $rand = int(rand(&countKeys($table)));
518 my $query = "SELECT $select FROM $table LIMIT 1 OFFSET $rand";
519 if ($param{DBType} =~ /^mysql$/i) {
520 # WARN: only newer MySQL supports "LIMIT limit OFFSET offset"
521 $query = "SELECT $select FROM $table LIMIT $rand,1";
523 my $sth = $dbh->prepare($query);
525 &WARN("randKey($query)") unless $sth->execute;
526 my @retval = $sth->fetchrow_array;
533 # Usage: &deleteTable($table);
535 &sqlRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
539 # Usage: &searchTable($table, $select, $key, $str);
540 # Note: searchTable does sqlQuote.
542 my($table, $select, $key, $str) = @_;
546 # allow two types of wildcards.
547 if ($str =~ /^\^(.*)\$$/) {
548 &FIXME("searchTable: can't do \"$str\"");
551 $str .= "%" if ($str =~ s/^\^//);
552 $str = "%".$str if ($str =~ s/\$$//);
553 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
557 $str =~ s/\?/_/g; # '.' should be supported, too.
561 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
563 my $sth = $dbh->prepare($query);
566 if (!$sth->execute) {
567 &WARN("Search($query)");
572 while (my @row = $sth->fetchrow_array) {
573 push(@results, $row[0]);
582 my(@path) = ($bot_data_dir, ".","..","../..");
587 my $file = "$_/setup/$table.sql";
588 next unless ( -f $file );
603 &sqlRaw("sqlCreateTable($table)", $data);
609 my $database_exists = 0;
612 if ($param{DBType} =~ /^mysql$/i) {
613 my $sql = "SHOW DATABASES";
614 foreach ( &sqlRawReturn($sql) ) {
615 $database_exists++ if ($_ eq $param{'DBName'});
618 unless ($database_exists) {
619 &status("Creating database $param{DBName}...");
620 my $query = "CREATE DATABASE $param{DBName}";
621 &sqlRaw("create(db $param{DBName})", $query);
624 # retrieve a list of db's from the server.
625 my @tables = map {s/^\`//; s/\`$//; $_;} $dbh->func('_ListTables');
627 @tables = $dbh->tables;
629 &status("Tables: ".join(',',@tables));
630 @db{@tables} = (1) x @tables;
632 } elsif ($param{DBType} =~ /^SQLite(2)?$/i) {
634 # retrieve a list of db's from the server.
635 foreach ( &sqlRawReturn("SELECT name FROM sqlite_master WHERE type='table'") ) {
639 # create database not needed for SQLite
642 foreach ( qw(botmail connections factoids rootwarn seen stats) ) {
643 if (exists $db{$_}) {
644 $cache{has_table}{$_} = 1;
648 &status("checkTables: creating new table $_...");
650 $cache{create_table}{$_} = 1;