]> git.donarmstrong.com Git - infobot.git/blobdiff - src/db_mysql.pl
- moved scripts/setup_sql.pl to src/db_mysql as &createTables()
[infobot.git] / src / db_mysql.pl
index 3b659b7df41100806e5ee2b60c8a91b9b6d6f546..eaeb9cd0063eaa75f798a628a072070897d1fae3 100644 (file)
@@ -279,11 +279,19 @@ sub dbRawReturn {
 #####
 
 #####
-# Usage: &countKeys($table);
+# Usage: &countKeys($table, [$col]);
 sub countKeys {
-    my ($table) = @_;
+    my ($table, $col) = @_;
+    $col ||= "*";
+
+    return (&dbRawReturn("SELECT count($col) FROM $table"))[0];
+}
 
-    return (&dbRawReturn("SELECT count(*) FROM $table"))[0];
+# Usage: &sumKey($table, $col);
+sub sumKey {
+    my ($table, $col) = @_;
+
+    return (&dbRawReturn("SELECT sum($col) FROM $table"))[0];
 }
 
 ##### NOT USED.
@@ -397,4 +405,55 @@ sub SQLDebug {
     print SQLDEBUG $_[0]."\n";
 }
 
+sub dbCreateTable {
+    my($table) = @_;
+    my(@path)  = (".","..","../..");
+    my $found  = 0;
+    my $data;
+
+    foreach (@path) {
+       my $file = "$_/setup/$table.sql";
+       &DEBUG("dbCT: file => $file");
+       next unless ( -f $file );
+
+       &DEBUG("found!!!");
+
+       open(IN, $file);
+       $data = <IN>;
+
+       $found++;
+       last;
+    }
+
+    if (!$found) {
+       return 0;
+    } else {
+       &dbRaw("create($table)", $data);
+       return 1;
+    }
+}
+
+sub checkTables {
+    # retrieve a list of db's from the server.
+    my %db;
+    foreach ($dbh->func('_ListTables')) {
+       $db{$_} = 1;
+    }
+
+    # create database.
+    if (!scalar keys %db) {
+       &status("Creating database $param{'DBName'}...");
+       $query = "CREATE DATABASE $param{'DBName'}";
+       &dbRaw("create(db $param{'DBName'})", $query);
+    }
+
+    foreach ("factoids", "freshmeat", "karma", "rootwarn", "seen",
+    ) {
+       next if (exists $db{$_});
+       &status("  creating new table $_...");
+
+       &dbCreateTable($_);
+    }
+}
+
 1;