#
# db_mysql.pl: MySQL database frontend.
-# Author: xk <xk@leguin.openprojects.net>
+# Author: dms
# Version: v0.2c (19991224)
# Created: 19991203
#
if (&IsParam("useStrict")) { use strict; }
sub openDB {
- my $dsn = "DBI:mysql:$param{'DBName'}:$param{'SQLHost'}";
- $dbh = DBI->connect($dsn, $param{'SQLUser'}, $param{'SQLPass'});
+ my ($db, $user, $pass) = @_;
+ my $dsn = "DBI:mysql:$db:$param{'SQLHost'}";
+ $dbh = DBI->connect($dsn, $user, $pass);
if ($dbh) {
&status("Opened MySQL connection to $param{'SQLHost'}");
return;
}
+ &SQLDebug($query);
if (!$sth->execute) {
&ERROR("Get => '$query'");
&ERROR("Get => $DBI::errstr");
- return;
+ $sth->finish;
+ return 0;
}
my @retval = $sth->fetchrow_array;
my %retval;
my $sth = $dbh->prepare($query);
- &ERROR("GetCol => '$query'") unless $sth->execute;
+ &SQLDebug($query);
+ if (!$sth->execute) {
+ &ERROR("GetCol => '$query'");
+ &ERROR("GetCol => $DBI::errstr");
+ $sth->finish;
+ return;
+ }
if (defined $type and $type == 1) {
while (my @row = $sth->fetchrow_array) {
####
# Usage: &dbGetRowInfo($table);
sub dbGetRowInfo {
- &DEBUG("STUB: dbGetRowInfo().");
+ my ($table) = @_;
+
+ my $query = "SHOW COLUMNS from $table";
+ my %retval;
+
+ my $sth = $dbh->prepare($query);
+ &SQLDebug($query);
+ if (!$sth->execute) {
+ &ERROR("GRI => '$query'");
+ &ERROR("GRI => $DBI::errstr");
+ $sth->finish;
+ return;
+ }
+
+ my @cols;
+ while (my @row = $sth->fetchrow_array) {
+ push(@cols, $row[0]);
+ }
+ $sth->finish;
+
+ return @cols;
}
#####
return 0;
}
+ &SQLDebug($query);
if (!$sth->execute) {
&ERROR("Raw($prefix): => '$query'");
&ERROR("Raw($prefix): $DBI::errstr");
+ $sth->finish;
return 0;
}
my @retval;
my $sth = $dbh->prepare($query);
+ &SQLDebug($query);
&ERROR("RawReturn => '$query'.") unless $sth->execute;
while (my @row = $sth->fetchrow_array) {
push(@retval, $row[0]);
my $query = "SELECT $primkey FROM $table";
my $sth = $dbh->prepare($query);
- $sth->execute;
+ &SQLDebug($query);
+ &WARN("ERROR: getKeys($query)") unless $sth->execute;
+
while (my @row = $sth->fetchrow_array) {
push(@retval, $row[0]);
}
my $query = "SELECT $select FROM $table LIMIT $rand,1";
my $sth = $dbh->prepare($query);
- $sth->execute;
+ &SQLDebug($query);
+ &WARN("randKey($query)") unless $sth->execute;
my @retval = $sth->fetchrow_array;
$sth->finish;
my $query = "SELECT $select FROM $table WHERE $key LIKE ".
&dbQuote($str);
my $sth = $dbh->prepare($query);
- $sth->execute;
+ &SQLDebug($query);
+ &WARN("Search($query)") unless $sth->execute;
while (my @row = $sth->fetchrow_array) {
push(@results, $row[0]);
return 1;
}
+sub SQLDebug {
+ return unless (&IsParam("SQLDebug"));
+
+ return if (!fileno SQLDEBUG);
+
+ print SQLDEBUG $_[0]."\n";
+}
+
1;