- Bossa: refactor DB code, creating a new PHP class DbConn

representing a connection to a database.
    This is independent of Bossa,
    and we should rewrite the BOINC PHP code based on this.

svn path=/trunk/boinc/; revision=13967
This commit is contained in:
David Anderson 2007-10-26 17:04:46 +00:00
parent d0032ae0d8
commit c6c066bde1
3 changed files with 102 additions and 64 deletions

View File

@ -9975,3 +9975,14 @@ Charlie 26 Oct 2007
boinc.xcodeproj/ boinc.xcodeproj/
project.pbxproj project.pbxproj
David 26 Oct 2007
- Bossa: refactor DB code, creating a new PHP class DbConn
representing a connection to a database.
This is independent of Bossa,
and we should rewrite the BOINC PHP code based on this.
html/
inc/
bossa_db.inc
user/
bossa_example.php

View File

@ -1,34 +1,40 @@
<?php <?php
ini_set('display_errors', 'stdout');
error_reporting(E_ALL);
require_once("../inc/util.inc"); require_once("../inc/util.inc");
class BossaDb { class DbConn {
public static $db_conn; private $db_conn;
public static $db_name; private $db_name;
static function init_conn() { function init_conn($user_tag, $passwd_tag, $host_tag, $name_tag) {
if ($db_conn) return;
$config = get_config(); $config = get_config();
$user = parse_config($config, "<bossa_db_user>"); $user = parse_config($config, $user_tag);
$pass = parse_config($config, "<bossa_db_passwd>"); $pass = parse_config($config, $passwd_tag);
$host = parse_config($config, "<bossa_db_host>"); $host = parse_config($config, $host_tag);
if ($host == null) { if ($host == null) {
$host = "localhost"; $host = "localhost";
} }
self::$db_conn = mysql_pconnect($host, $user, $pass); $this->db_conn = mysql_pconnect($host, $user, $pass);
if (!self::$db_conn) { if (!$this->db_conn) {
error_page("can't connect to database"); error_page("can't connect to database");
} }
self::$db_name = parse_config($config, "<bossa_db_name>"); $this->db_name = parse_config($config, $name_tag);
} }
static function lookup_id($id, $table, $classname) { function do_query($query) {
self::init_conn(); $q = str_replace('DBNAME', $this->db_name, $query);
$query = "select * from ".self::$db_name.".$table where id=$id"; //echo $q;
$result = mysql_query($query , self::$db_conn); return mysql_query($q, $this->db_conn);
}
function lookup($table, $classname, $clause) {
$query = "select * from DBNAME.$table where $clause";
$result = $this->do_query($query);
if (!$result) { if (!$result) {
echo $query; echo $query;
print_r($db_conn);
echo mysql_error(); echo mysql_error();
return null; return null;
} }
@ -37,14 +43,18 @@ class BossaDb {
return $obj; return $obj;
} }
static function enum($table, $classname, $clause=null) { function lookup_id($id, $table, $classname) {
self::init_conn(); return $this->lookup($table, $classname, "id=$id");
}
function enum($table, $classname, $clause=null) {
$x = array(); $x = array();
if ($clause) { if ($clause) {
$result = mysql_query("select * from $db_name.$table where $clause", $db_conn); $query = "select * from DBNAME.$table where $clause";
} else { } else {
$result = mysql_query("select * from $db_name.$table", $db_conn); $query = "select * from DBNAME.$table";
} }
$result = $this->do_query($query);
if (!$result) return null; if (!$result) return null;
while ($obj = mysql_fetch_object($result, $classname)) { while ($obj = mysql_fetch_object($result, $classname)) {
$x[] = $obj; $x[] = $obj;
@ -52,91 +62,111 @@ class BossaDb {
mysql_free_result($result); mysql_free_result($result);
return $x; return $x;
} }
function update($table, $clause) { function update($obj, $table, $clause) {
self::init_conn(); $query = "update DBNAME.$table set $clause where id=$obj->id";
return mysql_query( return $this->do_query($query);
"update ".parent::$db_name.".$table set $clause where id=$this->id", parent::$db_conn }
); function insert_id() {
return mysql_insert_id($this->db_conn);
} }
} }
class BossaApp extends BossaDb { class BossaDb extends DbConn {
public static $instance;
static function get() {
if (!isset($instance)) {
$instance = new DbConn();
$instance->init_conn(
"<bossa_db_user>", "<bossa_db_passwd>",
"<bossa_db_host>", "<bossa_db_name>"
);
}
return $instance;
}
}
class BossaApp {
function insert() { function insert() {
parent::init_conn(); $db = BossaDb::get();
if (!$this->long_jobs) $this->long_jobs = 0; if (!isset($this->long_jobs)) $this->long_jobs = 0;
$now = time(); $now = time();
$query = "insert into ".parent::$db_name.".bossa_app (create_time, name, user_friendly_name, long_jobs, start_url) values ($now, '$this->name', '$this->user_friendly_name', $this->long_jobs, '$this->start_url')"; $query = "insert into DBNAME.bossa_app (create_time, name, user_friendly_name, long_jobs, start_url) values ($now, '$this->name', '$this->user_friendly_name', $this->long_jobs, '$this->start_url')";
$result = mysql_query($query, parent::$db_conn); $result = $db->do_query($query);
if (!$result) return false; if (!$result) return false;
$this->id = mysql_insert_id(); $this->id = $db->insert_id();
return true; return true;
} }
static function lookup_name($name) { static function lookup_name($name) {
parent::init_conn(); $db = BossaDb::get();
$result = mysql_query("select * from ".parent::$db_name.".bossa_app where name='$name'", parent::$db_conn); return $db->lookup('bossa_app', 'BossaApp', "name='$name'");
if (!$result) return null;
$app = mysql_fetch_object($result, 'BossaApp');
mysql_free_result($result);
return $app;
} }
static function lookup_id($id) { static function lookup_id($id) {
return parent::lookup_id($id, 'bossa_app', 'BossaApp'); $db = BossaDb::get();
return $db->lookup_id($id, 'bossa_app', 'BossaApp');
} }
static function enum() { static function enum() {
return parent::enum('bossa_app', 'BossaApp'); $db = BossaDb::get();
return $db->enum('bossa_app', 'BossaApp');
} }
} }
class BossaJob extends BossaDb { class BossaJob {
function insert() { function insert() {
parent::init_conn(); $db = BossaDb::get();
$now = time(); $now = time();
$query = "insert into ".parent::$db_name.".bossa_job (create_time, name, app_id, info, batch, time_estimate, time_limit, more_needed, npending, nsuccess, nsuccess_needed) values ($now, '$this->name', $this->app_id, '$this->info', $this->batch, $this->time_estimate, $this->time_limit, 1, 0, 0, $this->nsuccess_needed)"; $query = "insert into DBNAME.bossa_job (create_time, name, app_id, info, batch, time_estimate, time_limit, more_needed, npending, nsuccess, nsuccess_needed) values ($now, '$this->name', $this->app_id, '$this->info', $this->batch, $this->time_estimate, $this->time_limit, 1, 0, 0, $this->nsuccess_needed)";
$result = mysql_query($query, parent::$db_conn); $result = $db->do_query($query);
if (!$result) { if (!$result) {
echo "$query\n"; echo "$query\n";
return false; return false;
} }
$this->id = mysql_insert_id(); $this->id = $db->insert_id();
return true; return true;
} }
function update($clause) { function update($clause) {
return parent::update('bossa_job', $clause); $db = BossaDb::get();
return $db->update('bossa_job', $clause);
} }
static function lookup_id($id) { static function lookup_id($id) {
return parent::lookup_id($id, 'bossa_job', 'BossaJob'); $db = BossaDb::get();
return $db->lookup_id($id, 'bossa_job', 'BossaJob');
} }
static function enum($clause) { static function enum($clause) {
return parent::enum('bossa_job', 'BossaJob', $clause); $db = BossaDb::get();
return $db->enum('bossa_job', 'BossaJob', $clause);
} }
} }
class BossaJobInst extends BossaDb { class BossaJobInst {
function insert() { function insert() {
parent::init_conn(); $db = BossaDb::get();
$now = time(); $now = time();
$query = "insert into ".parent::$db_name.".bossa_job_inst (create_time, job_id, user_id) values ($now, $this->job_id, $this->user_id)"; $query = "insert into DBNAME.bossa_job_inst (create_time, job_id, user_id) values ($now, $this->job_id, $this->user_id)";
$result = mysql_query($query, parent::$db_conn); $result = $db->do_query($query);
if (!$result) { if (!$result) {
echo "$query\n"; echo "$query\n";
return false; return false;
} }
$this->id = mysql_insert_id(); $this->id = $db->insert_id();
return true; return true;
} }
static function lookup_id($id) { static function lookup_id($id) {
return parent::lookup_id($id, 'bossa_job_inst', 'BossaJobInst'); $db = BossaDb::get();
return $db->lookup_id($id, 'bossa_job_inst', 'BossaJobInst');
} }
static function enum($clause) { static function enum($clause) {
return parent::enum('bossa_job_inst', 'BossaJobInst', $clause); $db = BossaDb::get();
return $db->enum('bossa_job_inst', 'BossaJobInst', $clause);
} }
function update($clause) { function update($clause) {
return parent::update('bossa_job_inst', $clause); $db = BossaDb::get();
return $db->update($this, 'bossa_job_inst', $clause);
} }
// Assign a job from the given app to the given user. // Assign a job from the given app to the given user.
@ -146,17 +176,14 @@ class BossaJobInst extends BossaDb {
// this query skips jobs for which this user // this query skips jobs for which this user
// has already been assigned an instance // has already been assigned an instance
// //
parent::init_conn(); $db = BossaDb::get();
$query = "select bossa_job.* from ".parent::$db_name.".bossa_job left join ".parent::$db_name.".bossa_job_inst on bossa_job_inst.job_id = bossa_job.id where bossa_job.more_needed<>0 and bossa_job_inst.user_id is null limit 1"; $query = "select bossa_job.* from DBNAME.bossa_job left join DBNAME.bossa_job_inst on bossa_job_inst.job_id = bossa_job.id where bossa_job.more_needed<>0 and bossa_job_inst.user_id is null limit 1";
$result = mysql_query($query, parent::$db_conn); $result = $db->do_query($query);
if (!$result) return null; if (!$result) return null;
$job = mysql_fetch_object($result, 'BossaJob'); $job = mysql_fetch_object($result, 'BossaJob');
if (!$job) return null; if (!$job) return null;
mysql_free_result($result); mysql_free_result($result);
echo "<hr>";
print_r($job);
echo "<hr>";
$ji = new BossaJobInst(); $ji = new BossaJobInst();
$ji->user_id = $user->id; $ji->user_id = $user->id;
$ji->job_id = $job->id; $ji->job_id = $job->id;
@ -177,7 +204,7 @@ class BossaJobInst extends BossaDb {
$job->npending--; $job->npending--;
$job->nsuccess++; $job->nsuccess++;
$job->more_needed = ($job->npending+$job->nsuccess < $job->nsuccess_needed); $job->more_needed = ($job->npending+$job->nsuccess < $job->nsuccess_needed);
return BossaJob::update("npending=$job->npending, nsuccess=$job->nsuccess, more_needed=$job->more_needed"); return $job->update("npending=$job->npending, nsuccess=$job->nsuccess, more_needed=$job->more_needed");
} }
// The given job instance has timed out. // The given job instance has timed out.
@ -185,7 +212,7 @@ class BossaJobInst extends BossaDb {
function timed_out($job) { function timed_out($job) {
$job->npending--; $job->npending--;
$job->more_needed = ($job->npending+$job->nsuccess < $job->nsuccess_needed); $job->more_needed = ($job->npending+$job->nsuccess < $job->nsuccess_needed);
return BossaJob::update("npending=$job->npending, more_needed=$job->more_needed"); return $job->update("npending=$job->npending, more_needed=$job->more_needed");
} }
} }

View File

@ -38,7 +38,7 @@ function handle_job_completion($bj, $bji) {
Bossa::script_init($user, $bj, $bji); Bossa::script_init($user, $bj, $bji);
if ($_GET['submit']) { if (isset($_GET['submit'])) {
handle_job_completion($bj, $bji); handle_job_completion($bj, $bji);
} else { } else {
show_job($bj, $bji); show_job($bj, $bji);