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");
64 my @retval = $sth->fetchrow_array;
68 if (scalar @retval > 1) {
70 } elsif (scalar @retval == 1) {
78 # Usage: &dbGetCol($table, $primkey, $key, [$type]);
80 my ($table, $primkey, $key, $type) = @_;
81 my $query = "SELECT $primkey,$key FROM $table WHERE $key IS NOT NULL";
84 my $sth = $dbh->prepare($query);
87 &ERROR("GetCol => '$query'");
88 &ERROR("GetCol => $DBI::errstr");
92 if (defined $type and $type == 1) {
93 while (my @row = $sth->fetchrow_array) {
94 # reverse it to make it easier to count.
95 $retval{$row[1]}{$row[0]} = 1;
98 while (my @row = $sth->fetchrow_array) {
99 $retval{$row[0]} = $row[1];
109 # Usage: &dbGetRowInfo($table);
113 my $query = "SHOW COLUMNS from $table";
116 my $sth = $dbh->prepare($query);
118 if (!$sth->execute) {
119 &ERROR("GRI => '$query'");
120 &ERROR("GRI => $DBI::errstr");
124 while (my @row = $sth->fetchrow_array) {
125 push(@cols, $row[0]);
133 # Usage: &dbSet($table, $primkey, $primval, $key, $val);
135 my ($table, $primkey, $primval, $key, $val) = @_;
138 my $result = &dbGet($table,$primkey,$primval,$primkey);
139 if (defined $result) {
140 $query = "UPDATE $table SET $key=".&dbQuote($val).
141 " WHERE $primkey=".&dbQuote($primval);
143 $query = "INSERT INTO $table ($primkey,$key) VALUES (".
144 &dbQuote($primval).",".&dbQuote($val).")";
147 &dbRaw("Set", $query);
153 # Usage: &dbUpdate($table, $primkey, $primval, %hash);
155 my ($table, $primkey, $primval, %hash) = @_;
158 foreach (keys %hash) {
159 push(@array, "$_=".&dbQuote($hash{$_}) );
162 &dbRaw("Update", "UPDATE $table SET ".join(', ', @array).
163 " WHERE $primkey=".&dbQuote($primval)
170 # Usage: &dbInsert($table, $primkey, %hash);
172 my ($table, $primkey, %hash) = @_;
175 foreach (keys %hash) {
177 push(@vals, &dbQuote($hash{$_}));
180 &dbRaw("Insert($table)", "INSERT INTO $table (".join(',',@keys).
181 ") VALUES (".join(',',@vals).")"
188 # Usage: &dbSetRow($table, @values);
190 my ($table, @values) = @_;
196 return &dbRaw("SetRow", "INSERT INTO $table VALUES (".
197 join(",", @values) .")" );
201 # Usage: &dbDel($table, $primkey, $primval, [$key]);
203 my ($table, $primkey, $primval, $key) = @_;
205 &dbRaw("Del", "DELETE FROM $table WHERE $primkey=".
212 # Usage: &dbRaw($prefix,$rawquery);
214 my ($prefix,$query) = @_;
217 if (!($sth = $dbh->prepare($query))) {
218 &ERROR("Raw($prefix): $DBI::errstr");
223 if (!$sth->execute) {
224 &ERROR("Raw($prefix): => '$query'");
225 &ERROR("Raw($prefix): $DBI::errstr");
234 # Usage: &dbRawReturn($rawquery);
239 my $sth = $dbh->prepare($query);
241 &ERROR("RawReturn => '$query'.") unless $sth->execute;
242 while (my @row = $sth->fetchrow_array) {
243 push(@retval, $row[0]);
250 ####################################################################
251 ##### Misc DBI stuff...
255 # Usage: &countKeys($table);
259 return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
263 # Usage: &getKeys($table,$primkey);
265 my ($table,$primkey) = @_;
268 my $query = "SELECT $primkey FROM $table";
269 my $sth = $dbh->prepare($query);
272 &WARN("ERROR: getKeys($query)") unless $sth->execute;
274 while (my @row = $sth->fetchrow_array) {
275 push(@retval, $row[0]);
283 # Usage: &randKey($table, $select);
285 my ($table, $select) = @_;
286 my $rand = int(rand(&countKeys($table) - 1));
287 my $query = "SELECT $select FROM $table LIMIT $rand,1";
289 my $sth = $dbh->prepare($query);
291 &WARN("randKey($query)") unless $sth->execute;
292 my @retval = $sth->fetchrow_array;
299 # Usage: &deleteTable($table);
301 &dbRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
304 # Usage: &searchTable($table, $select, $key, $str);
306 my($table, $select, $key, $str) = @_;
310 # allow two types of wildcards.
311 if ($str =~ /^\^(.*)\$$/) {
312 &DEBUG("searchTable: should use dbGet(), heh.");
315 $str .= "%" if ($str =~ s/^\^//);
316 $str = "%".$str if ($str =~ s/\$$//);
317 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
321 $str =~ s/\?/\_/g; # '.' should be supported, too.
324 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
326 my $sth = $dbh->prepare($query);
328 &WARN("Search($query)") unless $sth->execute;
330 while (my @row = $sth->fetchrow_array) {
331 push(@results, $row[0]);
338 ####################################################################
339 ##### Factoid related stuff...
343 # Usage: &getFactInfo($faqtoid, type);
345 return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
349 # Usage: &getFactoid($faqtoid);
351 return &getFactInfo($_[0], "factoid_value");
355 # Usage: &delFactoid($faqtoid);
359 &dbDel("factoids", "factoid_key",$faqtoid);
360 &status("DELETED $faqtoid");
366 return unless (&IsParam("SQLDebug"));
368 return if (!fileno SQLDEBUG);
370 print SQLDEBUG $_[0]."\n";