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, $primval, %hash);
191 my ($table, $primkey, $primval, %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).") WHERE $primkey=".
209 # Usage: &dbSetRow($table, @values);
211 my ($table, @values) = @_;
217 return &dbRaw("SetRow", "INSERT INTO $table VALUES (".
218 join(",", @values) .")" );
222 # Usage: &dbDel($table, $primkey, $primval, [$key]);
224 my ($table, $primkey, $primval, $key) = @_;
226 &dbRaw("Del", "DELETE FROM $table WHERE $primkey=".
233 # Usage: &dbRaw($prefix,$rawquery);
235 my ($prefix,$query) = @_;
238 if (!($sth = $dbh->prepare($query))) {
239 &ERROR("Raw($prefix): $DBI::errstr");
244 if (!$sth->execute) {
245 &ERROR("Raw($prefix): => '$query'");
246 # &ERROR("Raw($prefix): $DBI::errstr");
256 # Usage: &dbRawReturn($rawquery);
261 my $sth = $dbh->prepare($query);
263 &ERROR("RawReturn => '$query'.") unless $sth->execute;
264 while (my @row = $sth->fetchrow_array) {
265 push(@retval, $row[0]);
272 ####################################################################
273 ##### Misc DBI stuff...
277 # Usage: &countKeys($table);
281 return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
285 # Usage: &getKeys($table,$primkey);
287 my ($table,$primkey) = @_;
290 my $query = "SELECT $primkey FROM $table";
291 my $sth = $dbh->prepare($query);
294 &WARN("ERROR: getKeys($query)") unless $sth->execute;
296 while (my @row = $sth->fetchrow_array) {
297 push(@retval, $row[0]);
305 # Usage: &randKey($table, $select);
307 my ($table, $select) = @_;
308 my $rand = int(rand(&countKeys($table) - 1));
309 my $query = "SELECT $select FROM $table LIMIT $rand,1";
311 my $sth = $dbh->prepare($query);
313 &WARN("randKey($query)") unless $sth->execute;
314 my @retval = $sth->fetchrow_array;
321 # Usage: &deleteTable($table);
323 &dbRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
326 # Usage: &searchTable($table, $select, $key, $str);
328 my($table, $select, $key, $str) = @_;
332 # allow two types of wildcards.
333 if ($str =~ /^\^(.*)\$$/) {
334 &DEBUG("searchTable: should use dbGet(), heh.");
337 $str .= "%" if ($str =~ s/^\^//);
338 $str = "%".$str if ($str =~ s/\$$//);
339 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
343 $str =~ s/\?/\_/g; # '.' should be supported, too.
346 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
348 my $sth = $dbh->prepare($query);
350 &WARN("Search($query)") unless $sth->execute;
352 while (my @row = $sth->fetchrow_array) {
353 push(@results, $row[0]);
360 ####################################################################
361 ##### Factoid related stuff...
365 # Usage: &getFactInfo($faqtoid, type);
367 return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
371 # Usage: &getFactoid($faqtoid);
373 return &getFactInfo($_[0], "factoid_value");
377 # Usage: &delFactoid($faqtoid);
381 &dbDel("factoids", "factoid_key",$faqtoid);
382 &status("DELETED '$faqtoid'");
388 return unless (&IsParam("SQLDebug"));
390 return if (!fileno SQLDEBUG);
392 print SQLDEBUG $_[0]."\n";