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'}.");
29 &WARN("closeDB: connection already closed?");
33 &status("Closed MySQL connection to $param{'SQLHost'}.");
39 # Usage: &dbQuote($str);
41 return $dbh->quote($_[0]);
45 # Usage: &dbGet($table, $primkey, $primval, $select);
47 my ($table, $primkey, $primval, $select) = @_;
48 my $query = "SELECT $select FROM $table WHERE $primkey=".
52 if (!($sth = $dbh->prepare($query))) {
53 &ERROR("Get: $DBI::errstr");
59 &ERROR("Get => '$query'");
60 &ERROR("Get => $DBI::errstr");
65 my @retval = $sth->fetchrow_array;
69 if (scalar @retval > 1) {
71 } elsif (scalar @retval == 1) {
79 # Usage: &dbGetCol($table, $primkey, $key, [$type]);
81 my ($table, $primkey, $key, $type) = @_;
82 my $query = "SELECT $primkey,$key FROM $table WHERE $key IS NOT NULL";
85 my $sth = $dbh->prepare($query);
88 &ERROR("GetCol => '$query'");
89 &ERROR("GetCol => $DBI::errstr");
94 if (defined $type and $type == 1) {
95 while (my @row = $sth->fetchrow_array) {
96 # reverse it to make it easier to count.
97 $retval{$row[1]}{$row[0]} = 1;
100 while (my @row = $sth->fetchrow_array) {
101 $retval{$row[0]} = $row[1];
111 # Usage: &dbGetRowInfo($table);
115 my $query = "SHOW COLUMNS from $table";
118 my $sth = $dbh->prepare($query);
120 if (!$sth->execute) {
121 &ERROR("GRI => '$query'");
122 &ERROR("GRI => $DBI::errstr");
128 while (my @row = $sth->fetchrow_array) {
129 push(@cols, $row[0]);
137 # Usage: &dbSet($table, $primkey, $primval, $key, $val);
139 my ($table, $primkey, $primval, $key, $val) = @_;
142 my $result = &dbGet($table,$primkey,$primval,$primkey);
143 if (defined $result) {
144 $query = "UPDATE $table SET $key=".&dbQuote($val).
145 " WHERE $primkey=".&dbQuote($primval);
147 $query = "INSERT INTO $table ($primkey,$key) VALUES (".
148 &dbQuote($primval).",".&dbQuote($val).")";
151 &dbRaw("Set", $query);
157 # Usage: &dbUpdate($table, $primkey, $primval, %hash);
159 my ($table, $primkey, $primval, %hash) = @_;
162 foreach (keys %hash) {
163 push(@array, "$_=".&dbQuote($hash{$_}) );
166 &dbRaw("Update", "UPDATE $table SET ".join(', ', @array).
167 " WHERE $primkey=".&dbQuote($primval)
174 # Usage: &dbInsert($table, $primkey, %hash);
176 my ($table, $primkey, %hash) = @_;
179 foreach (keys %hash) {
181 push(@vals, &dbQuote($hash{$_}));
184 &dbRaw("Insert($table)", "INSERT INTO $table (".join(',',@keys).
185 ") VALUES (".join(',',@vals).")"
192 # Usage: &dbSetRow($table, @values);
194 my ($table, @values) = @_;
200 return &dbRaw("SetRow", "INSERT INTO $table VALUES (".
201 join(",", @values) .")" );
205 # Usage: &dbDel($table, $primkey, $primval, [$key]);
207 my ($table, $primkey, $primval, $key) = @_;
209 &dbRaw("Del", "DELETE FROM $table WHERE $primkey=".
216 # Usage: &dbRaw($prefix,$rawquery);
218 my ($prefix,$query) = @_;
221 if (!($sth = $dbh->prepare($query))) {
222 &ERROR("Raw($prefix): $DBI::errstr");
227 if (!$sth->execute) {
228 &ERROR("Raw($prefix): => '$query'");
229 &ERROR("Raw($prefix): $DBI::errstr");
239 # Usage: &dbRawReturn($rawquery);
244 my $sth = $dbh->prepare($query);
246 &ERROR("RawReturn => '$query'.") unless $sth->execute;
247 while (my @row = $sth->fetchrow_array) {
248 push(@retval, $row[0]);
255 ####################################################################
256 ##### Misc DBI stuff...
260 # Usage: &countKeys($table);
264 return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
268 # Usage: &getKeys($table,$primkey);
270 my ($table,$primkey) = @_;
273 my $query = "SELECT $primkey FROM $table";
274 my $sth = $dbh->prepare($query);
277 &WARN("ERROR: getKeys($query)") unless $sth->execute;
279 while (my @row = $sth->fetchrow_array) {
280 push(@retval, $row[0]);
288 # Usage: &randKey($table, $select);
290 my ($table, $select) = @_;
291 my $rand = int(rand(&countKeys($table) - 1));
292 my $query = "SELECT $select FROM $table LIMIT $rand,1";
294 my $sth = $dbh->prepare($query);
296 &WARN("randKey($query)") unless $sth->execute;
297 my @retval = $sth->fetchrow_array;
304 # Usage: &deleteTable($table);
306 &dbRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
309 # Usage: &searchTable($table, $select, $key, $str);
311 my($table, $select, $key, $str) = @_;
315 # allow two types of wildcards.
316 if ($str =~ /^\^(.*)\$$/) {
317 &DEBUG("searchTable: should use dbGet(), heh.");
320 $str .= "%" if ($str =~ s/^\^//);
321 $str = "%".$str if ($str =~ s/\$$//);
322 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
326 $str =~ s/\?/\_/g; # '.' should be supported, too.
329 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
331 my $sth = $dbh->prepare($query);
333 &WARN("Search($query)") unless $sth->execute;
335 while (my @row = $sth->fetchrow_array) {
336 push(@results, $row[0]);
343 ####################################################################
344 ##### Factoid related stuff...
348 # Usage: &getFactInfo($faqtoid, type);
350 return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
354 # Usage: &getFactoid($faqtoid);
356 return &getFactInfo($_[0], "factoid_value");
360 # Usage: &delFactoid($faqtoid);
364 &dbDel("factoids", "factoid_key",$faqtoid);
365 &status("DELETED $faqtoid");
371 return unless (&IsParam("SQLDebug"));
373 return if (!fileno SQLDEBUG);
375 print SQLDEBUG $_[0]."\n";