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: &dbSetRow($table, @values);
191 my ($table, @values) = @_;
197 return &dbRaw("SetRow", "INSERT INTO $table VALUES (".
198 join(",", @values) .")" );
202 # Usage: &dbDel($table, $primkey, $primval, [$key]);
204 my ($table, $primkey, $primval, $key) = @_;
206 &dbRaw("Del", "DELETE FROM $table WHERE $primkey=".
213 # Usage: &dbRaw($prefix,$rawquery);
215 my ($prefix,$query) = @_;
218 if (!($sth = $dbh->prepare($query))) {
219 &ERROR("Raw($prefix): $DBI::errstr");
224 if (!$sth->execute) {
225 &ERROR("Raw($prefix): => '$query'");
226 &ERROR("Raw($prefix): $DBI::errstr");
236 # Usage: &dbRawReturn($rawquery);
241 my $sth = $dbh->prepare($query);
243 &ERROR("RawReturn => '$query'.") unless $sth->execute;
244 while (my @row = $sth->fetchrow_array) {
245 push(@retval, $row[0]);
252 ####################################################################
253 ##### Misc DBI stuff...
257 # Usage: &countKeys($table);
261 return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
265 # Usage: &getKeys($table,$primkey);
267 my ($table,$primkey) = @_;
270 my $query = "SELECT $primkey FROM $table";
271 my $sth = $dbh->prepare($query);
274 &WARN("ERROR: getKeys($query)") unless $sth->execute;
276 while (my @row = $sth->fetchrow_array) {
277 push(@retval, $row[0]);
285 # Usage: &randKey($table, $select);
287 my ($table, $select) = @_;
288 my $rand = int(rand(&countKeys($table) - 1));
289 my $query = "SELECT $select FROM $table LIMIT $rand,1";
291 my $sth = $dbh->prepare($query);
293 &WARN("randKey($query)") unless $sth->execute;
294 my @retval = $sth->fetchrow_array;
301 # Usage: &deleteTable($table);
303 &dbRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
306 # Usage: &searchTable($table, $select, $key, $str);
308 my($table, $select, $key, $str) = @_;
312 # allow two types of wildcards.
313 if ($str =~ /^\^(.*)\$$/) {
314 &DEBUG("searchTable: should use dbGet(), heh.");
317 $str .= "%" if ($str =~ s/^\^//);
318 $str = "%".$str if ($str =~ s/\$$//);
319 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
323 $str =~ s/\?/\_/g; # '.' should be supported, too.
326 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
328 my $sth = $dbh->prepare($query);
330 &WARN("Search($query)") unless $sth->execute;
332 while (my @row = $sth->fetchrow_array) {
333 push(@results, $row[0]);
340 ####################################################################
341 ##### Factoid related stuff...
345 # Usage: &getFactInfo($faqtoid, type);
347 return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
351 # Usage: &getFactoid($faqtoid);
353 return &getFactInfo($_[0], "factoid_value");
357 # Usage: &delFactoid($faqtoid);
361 &dbDel("factoids", "factoid_key",$faqtoid);
362 &status("DELETED '$faqtoid'");
368 return unless (&IsParam("SQLDebug"));
370 return if (!fileno SQLDEBUG);
372 print SQLDEBUG $_[0]."\n";