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");
61 &SQLDebug($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 &SQLDebug($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 &SQLDebug($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");
226 &SQLDebug($DBI::errstr);
235 # Usage: &dbRawReturn($rawquery);
240 my $sth = $dbh->prepare($query);
242 &ERROR("RawReturn => '$query'.") unless $sth->execute;
243 while (my @row = $sth->fetchrow_array) {
244 push(@retval, $row[0]);
251 ####################################################################
252 ##### Misc DBI stuff...
256 # Usage: &countKeys($table);
260 return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
264 # Usage: &getKeys($table,$primkey);
266 my ($table,$primkey) = @_;
269 my $query = "SELECT $primkey FROM $table";
270 my $sth = $dbh->prepare($query);
273 &WARN("ERROR: getKeys($query)") unless $sth->execute;
275 while (my @row = $sth->fetchrow_array) {
276 push(@retval, $row[0]);
284 # Usage: &randKey($table, $select);
286 my ($table, $select) = @_;
287 my $rand = int(rand(&countKeys($table) - 1));
288 my $query = "SELECT $select FROM $table LIMIT $rand,1";
290 my $sth = $dbh->prepare($query);
292 &WARN("randKey($query)") unless $sth->execute;
293 my @retval = $sth->fetchrow_array;
300 # Usage: &deleteTable($table);
302 &dbRaw("deleteTable($_[0])", "DELETE FROM $_[0]");
305 # Usage: &searchTable($table, $select, $key, $str);
307 my($table, $select, $key, $str) = @_;
311 # allow two types of wildcards.
312 if ($str =~ /^\^(.*)\$$/) {
313 &DEBUG("searchTable: should use dbGet(), heh.");
316 $str .= "%" if ($str =~ s/^\^//);
317 $str = "%".$str if ($str =~ s/\$$//);
318 $str = "%".$str."%" if ($str eq $origStr); # el-cheapo fix.
322 $str =~ s/\?/\_/g; # '.' should be supported, too.
325 my $query = "SELECT $select FROM $table WHERE $key LIKE ".
327 my $sth = $dbh->prepare($query);
329 &WARN("Search($query)") unless $sth->execute;
331 while (my @row = $sth->fetchrow_array) {
332 push(@results, $row[0]);
339 ####################################################################
340 ##### Factoid related stuff...
344 # Usage: &getFactInfo($faqtoid, type);
346 return &dbGet("factoids", "factoid_key", $_[0], $_[1]);
350 # Usage: &getFactoid($faqtoid);
352 return &getFactInfo($_[0], "factoid_value");
356 # Usage: &delFactoid($faqtoid);
360 &dbDel("factoids", "factoid_key",$faqtoid);
361 &status("DELETED $faqtoid");
367 return unless (&IsParam("SQLDebug"));
369 print SQLDEBUG $_[0]."\n";