2 # db_mysql.pl: MySQL database frontend.
4 # Version: v0.2c (19991224)
10 if (&IsParam("useStrict")) { use strict; }
13 my ($db, $user, $pass) = @_;
14 my $dsn = "DBI:mysql:$db:$param{'SQLHost'}";
15 $dbh = DBI->connect($dsn, $user, $pass);
18 &status("Opened MySQL connection to $param{'SQLHost'}");
20 &ERROR("cannot connect to $param{'SQLHost'}.");
28 return 0 unless ($dbh);
30 &status("Closed MySQL connection to $param{'SQLHost'}.");
36 # Usage: &dbQuote($str);
38 return $dbh->quote($_[0]);
42 # Usage: &dbGet($table, $primkey, $primval, $select);
44 my ($table, $primkey, $primval, $select) = @_;
45 my $query = "SELECT $select FROM $table WHERE $primkey=".
49 if (!($sth = $dbh->prepare($query))) {
50 &ERROR("Get: $DBI::errstr");
56 &ERROR("Get => '$query'");
57 &ERROR("Get => $DBI::errstr");
62 my @retval = $sth->fetchrow_array;
66 if (scalar @retval > 1) {
68 } elsif (scalar @retval == 1) {
76 # Usage: &dbGetCol($table, $primkey, $key, [$type]);
78 my ($table, $primkey, $key, $type) = @_;
79 my $query = "SELECT $primkey,$key FROM $table WHERE $key IS NOT NULL";
82 my $sth = $dbh->prepare($query);
85 &ERROR("GetCol => '$query'");
86 &ERROR("GetCol => $DBI::errstr");
91 if (defined $type and $type == 1) {
92 while (my @row = $sth->fetchrow_array) {
93 # reverse it to make it easier to count.
94 $retval{$row[1]}{$row[0]} = 1;
97 while (my @row = $sth->fetchrow_array) {
98 $retval{$row[0]} = $row[1];
108 # Usage: &dbGetRowInfo($table);
112 my $query = "SHOW COLUMNS from $table";
115 my $sth = $dbh->prepare($query);
117 if (!$sth->execute) {
118 &ERROR("GRI => '$query'");
119 &ERROR("GRI => $DBI::errstr");
125 while (my @row = $sth->fetchrow_array) {
126 push(@cols, $row[0]);
134 # Usage: &dbSet($table, $primkey, $primval, $key, $val);
136 my ($table, $primkey, $primval, $key, $val) = @_;
139 my $result = &dbGet($table,$primkey,$primval,$primkey);
140 if (defined $result) {
141 $query = "UPDATE $table SET $key=".&dbQuote($val).
142 " WHERE $primkey=".&dbQuote($primval);
144 $query = "INSERT INTO $table ($primkey,$key) VALUES (".
145 &dbQuote($primval).",".&dbQuote($val).")";
148 &dbRaw("Set", $query);
154 # Usage: &dbUpdate($table, $primkey, $primval, %hash);
156 my ($table, $primkey, $primval, %hash) = @_;
159 foreach (keys %hash) {
160 push(@array, "$_=".&dbQuote($hash{$_}) );
163 &dbRaw("Update", "UPDATE $table SET ".join(', ', @array).
164 " WHERE $primkey=".&dbQuote($primval)
171 # Usage: &dbInsert($table, $primkey, %hash);
173 my ($table, $primkey, %hash) = @_;
176 foreach (keys %hash) {
178 push(@vals, &dbQuote($hash{$_}));
181 &dbRaw("Insert($table)", "INSERT INTO $table (".join(',',@keys).
182 ") VALUES (".join(',',@vals).")"
189 # Usage: &dbReplace($table, $primkey, %hash);
191 my ($table, $primkey, %hash) = @_;
194 foreach (keys %hash) {
195 &DEBUG("hash{$_} => $hash{$_}");
197 push(@vals, &dbQuote($hash{$_}));
200 &dbRaw("Replace($table)", "REPLACE INTO $table (".join(',',@keys).
201 ") VALUES (".join(',',@vals).")"
208 # Usage: &dbSetRow($table, @values);
210 my ($table, @values) = @_;
216 return &dbRaw("SetRow", "INSERT INTO $table VALUES (".
217 join(",", @values) .")" );
221 # Usage: &dbDel($table, $primkey, $primval, [$key]);
223 my ($table, $primkey, $primval, $key) = @_;
225 &dbRaw("Del", "DELETE FROM $table WHERE $primkey=".
232 # Usage: &dbRaw($prefix,$rawquery);
234 my ($prefix,$query) = @_;
237 if (!($sth = $dbh->prepare($query))) {
238 &ERROR("Raw($prefix): $DBI::errstr");
243 if (!$sth->execute) {
244 &ERROR("Raw($prefix): => '$query'");
245 &ERROR("Raw($prefix): $DBI::errstr");
255 # Usage: &dbRawReturn($rawquery);
260 my $sth = $dbh->prepare($query);
262 &ERROR("RawReturn => '$query'.") unless $sth->execute;
263 while (my @row = $sth->fetchrow_array) {
264 push(@retval, $row[0]);
271 ####################################################################
272 ##### Misc DBI stuff...
276 # Usage: &countKeys($table);
280 return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
284 # Usage: &getKeys($table,$primkey);
286 my ($table,$primkey) = @_;
289 my $query = "SELECT $primkey FROM $table";
290 my $sth = $dbh->prepare($query);
293 &WARN("ERROR: getKeys($query)") unless $sth->execute;
295 while (my @row = $sth->fetchrow_array) {
296 push(@retval, $row[0]);
304 # Usage: &randKey($table, $select);
306 my ($table, $select) = @_;
307 my $rand = int(rand(&countKeys($table) - 1));
308 my $query = "SELECT $select FROM $table LIMIT $rand,1";
310 my $sth = $dbh->prepare($query);
312 &WARN("randKey($query)") unless $sth->execute;
313 my @retval = $sth->fetchrow_array;
320 # Usage: &deleteTable($table);
322 &dbRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
325 # Usage: &searchTable($table, $select, $key, $str);
327 my($table, $select, $key, $str) = @_;
331 # allow two types of wildcards.
332 if ($str =~ /^\^(.*)\$$/) {
333 &DEBUG("searchTable: should use dbGet(), heh.");
336 $str .= "%" if ($str =~ s/^\^//);
337 $str = "%".$str if ($str =~ s/\$$//);
338 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
342 $str =~ s/\?/\_/g; # '.' should be supported, too.
345 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
347 my $sth = $dbh->prepare($query);
349 &WARN("Search($query)") unless $sth->execute;
351 while (my @row = $sth->fetchrow_array) {
352 push(@results, $row[0]);
359 ####################################################################
360 ##### Factoid related stuff...
364 # Usage: &getFactInfo($faqtoid, type);
366 return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
370 # Usage: &getFactoid($faqtoid);
372 return &getFactInfo($_[0], "factoid_value");
376 # Usage: &delFactoid($faqtoid);
380 &dbDel("factoids", "factoid_key",$faqtoid);
381 &status("DELETED '$faqtoid'");
387 return unless (&IsParam("SQLDebug"));
389 return if (!fileno SQLDEBUG);
391 print SQLDEBUG $_[0]."\n";