Skip to content

Commit

Permalink
A couple of additions to the BoincDb class:
Browse files Browse the repository at this point in the history
- a close() method, closes the connection
- a $dbnum member: which replica you're connected to (0 if main DB)

Also, BoincDb doesn't inherit DbConn.
  • Loading branch information
davidpanderson committed Aug 1, 2024
1 parent 2638531 commit 4b4ba9d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
50 changes: 33 additions & 17 deletions html/inc/boinc_db.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
// https://boinc.berkeley.edu
// Copyright (C) 2024 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
Expand All @@ -16,6 +16,19 @@
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.

// A project can have one or more BOINC databases:
// DB 0:
// the main DB; read/write
// identified in config file by db_host, db_name, db_user, db_passwd
// db_host defaults to localhost
// DB 1:
// read-only replica; identified by
// replica_db_host/name/user/passwd (must include all)
// DB 2:
// read-only replica; identified by
// replica2_db_host/name/user/passwd (must include all)
// ... and potentially more

function incs() {
$d = dirname(__FILE__);
require_once("$d/db_conn.inc");
Expand All @@ -24,21 +37,15 @@ function incs() {

incs();

class BoincDb extends DbConn {
static $instance;

// A project can have one or more databases:
// DB 0:
// the main DB; read/write
// identified in config file by db_host, db_name, db_user, db_passwd
// db_host defaults to localhost
// DB 1:
// read-only replica; identified by
// replica_db_host/name/user/passwd (must include all)
// DB 2:
// read-only replica; identified by
// replica2_db_host/name/user/passwd (must include all)
// ... and potentially more
// class BoincDb represents a connection to a BOINC database.
// All its members are static, so there's only 1 connection at a time.
// get(n) establishes a connection to DB n,
// or DB 0 if that fails or doesn't exit.
// close() closes the connection.

class BoincDb {
static $instance; // a DbConn object, or null
static $dbnum; // which replica we're connected to

// connect to DB $dbnum (0, 1, ...)
// If the requested DB doesn't exist or connection fails, connect to DB 0.
Expand All @@ -59,6 +66,7 @@ class BoincDb extends DbConn {
if ($retval) {
//error_log("BoincDb::get_aux(): connected to replica DB $dbnum");
self::$instance = $instance;
self::$dbnum = $dbnum;
return;
}
}
Expand All @@ -77,6 +85,7 @@ class BoincDb extends DbConn {
if ($retval) {
//error_log("BoincDb::get_aux(): connected to DB $dbnum");
self::$instance = $instance;
self::$dbnum = 0;
return;
}
error_log("BoincDb::get_aux(): Couldn't connect to DB $dbnum");
Expand Down Expand Up @@ -116,6 +125,13 @@ class BoincDb extends DbConn {
}
}

static function close() {
if (isset(self::$instance)) {
self::$instance->close();
self::$instance = null;
}
}

static function escape_string($string) {
if (!$string) return '';
$db = self::get();
Expand Down
10 changes: 8 additions & 2 deletions html/inc/db_conn.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ require_once("../inc/db.inc");
// Intended to be subclassed (e.g., BoincDb, BossaDb)
//
class DbConn {
var $db_conn;
var $db_name;
var $db_conn; // a mysqli object
var $db_name; // the DB name

function init_conn($user, $passwd, $host, $name) {
$x = explode(":", $host);
Expand Down Expand Up @@ -54,6 +54,12 @@ class DbConn {
return true;
}

function close() {
if ($this->db_conn) {
$this->db_conn->close();
}
}

// in keeping with PHP/MySQL convention, return true (nonzero) on success.
// (This is the opposite of the BOINC convention)
//
Expand Down

0 comments on commit 4b4ba9d

Please sign in to comment.