X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Fdbi.pl;h=99d115e4d95d81a3f471faeaff568be27e2f4d39;hb=bfc0e61719a7f943557d6389d6654aa8bc9b8366;hp=6761b230051bb94155d8dee874df547199558e8a;hpb=42c917bf6e6ac756cc551be684106be0320296cd;p=infobot.git diff --git a/src/dbi.pl b/src/dbi.pl index 6761b23..99d115e 100644 --- a/src/dbi.pl +++ b/src/dbi.pl @@ -19,7 +19,7 @@ package main; sub sqlOpenDB { my ($db, $type, $user, $pass, $no_fail) = @_; # this is a mess. someone fix it, please. - if ($type =~ /^SQLite$/i) { + if ($type =~ /^SQLite(2)?$/i) { $db = "dbname=$db.sqlite"; } elsif ($type =~ /^pg/i) { $db = "dbname=$db"; @@ -514,12 +514,12 @@ sub sumKey { # Usage: &randKey($table, $select); sub randKey { my ($table, $select) = @_; - my $rand = int(rand(&countKeys($table) - 1)); - my $query = "SELECT $select FROM $table LIMIT $rand,1"; - if ($param{DBType} =~ /^pg/i) { - $query =~ s/$rand,1/1,$rand/; + my $rand = int(rand(&countKeys($table))); + my $query = "SELECT $select FROM $table LIMIT 1 OFFSET $rand"; + if ($param{DBType} =~ /^mysql$/i) { + # WARN: only newer MySQL supports "LIMIT limit OFFSET offset" + $query = "SELECT $select FROM $table LIMIT $rand,1"; } - my $sth = $dbh->prepare($query); &SQLDebug($query); &WARN("randKey($query)") unless $sth->execute; @@ -629,22 +629,17 @@ sub checkTables { &status("Tables: ".join(',',@tables)); @db{@tables} = (1) x @tables; - } elsif ($param{DBType} =~ /^SQLite$/i) { + } elsif ($param{DBType} =~ /^SQLite(2)?$/i) { # retrieve a list of db's from the server. foreach ( &sqlRawReturn("SELECT name FROM sqlite_master WHERE type='table'") ) { $db{$_} = 1; } - # create database. - if (!scalar keys %db) { - &status("Creating database $param{'DBName'}..."); - my $query = "CREATE DATABASE $param{'DBName'}"; - &sqlRaw("create(db $param{'DBName'})", $query); - } + # create database not needed for SQLite } - foreach ( qw(factoids rootwarn seen stats botmail) ) { + foreach ( qw(botmail connections factoids rootwarn seen stats) ) { if (exists $db{$_}) { $cache{has_table}{$_} = 1; next;