--- FreeBSD/tinderbox/webui/core/TinderboxDS.php 2007/11/09 22:47:22 1.1.1.1.2.1 +++ FreeBSD/tinderbox/webui/core/TinderboxDS.php 2007/11/15 15:31:35 1.1.1.1.2.3 @@ -24,16 +24,15 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # -# $Paefchen$ +# $Paefchen: FreeBSD/tinderbox/webui/core/TinderboxDS.php,v 1.1.1.1.2.2 2007/11/13 07:52:29 as Exp $ # $MCom: portstools/tinderbox/webui/core/TinderboxDS.php,v 1.36 2007/10/13 02:28:47 ade Exp $ # require_once 'DB.php'; -require_once 'inc_ds.php'; -require_once 'inc_tinderbox.php'; class TinderboxDS { - private $this->objectMap = array( + static private $instance = false; + private $objectMap = array( 'Build' => 'builds', 'BuildPortsQueue' => 'build_ports_queue', 'Config' => 'config', @@ -42,11 +41,11 @@ class TinderboxDS { 'PortsTree' => 'ports_trees', 'PortFailReason' => 'port_fail_reasons', 'User' => 'users'); - public $db; + private $db; private $error = null; - public $packageSuffixCache; /* in use by getPackageSuffix() */ + private $packageSuffixCache; /* in use by getPackageSuffix() */ - public function __construct() { + private function __construct() { global $DB_HOST, $DB_DRIVER, $DB_NAME, $DB_USER, $DB_PASS; # XXX: backwards compatibility @@ -58,15 +57,21 @@ class TinderboxDS { $this->db = DB::connect($dsn); if (DB::isError($this->db)) - die (sprintf("Tinderbox DS: Unable to initialize datastore: %s\n", - $this->db->getMessage())); + die (sprintf("Error => %s: Unable to initialize datastore: %s\n", + __CLASS__, $this->db->getMessage())); $this->db->setFetchMode(DB_FETCHMODE_ASSOC); $this->db->setOption('persistent', true); } - private function __destruct() { - $this->db->disconnect(); + static public function instance() { + if (! self::$instance) + self::$instance = new TinderboxDS; + return self::$instance; + } + + public function __destruct() { + @$this->db->disconnect(); $this->error = null; } @@ -120,7 +125,7 @@ class TinderboxDS { AND bp.port_id = %d', $portid); - return $this->_doQueryHashRef($query, $results, array())) + return $this->_doQueryHashRef($query, $results, array()) ? $this->_newFromArray('Port', $results) : null; } @@ -460,8 +465,8 @@ class TinderboxDS { $query .= ' ORDER BY bp.last_built DESC'; return $this->_doQueryHashRef($query, $results, array()) - : $this->_newFromArray('Port', $results) - ? null + ? $this->_newFromArray('Port', $results) + : null; } public function getBuildStatsWithStatus($build_id) { @@ -471,7 +476,7 @@ class TinderboxDS { WHERE build_id = ? GROUP BY last_status'; - return $this->_doQueryHashRef($query, $results, $build_id)) + return $this->_doQueryHashRef($query, $results, $build_id) ? $results : null; } @@ -484,7 +489,7 @@ class TinderboxDS { WHERE last_status = \'FAIL\' AND build_id = ?'; - return $this->_doQueryHashRef($query, $results, $build_id); + return $this->_doQueryHashRef($query, $results, $build_id) ? $results[0] : null; } @@ -547,7 +552,7 @@ class TinderboxDS { $condition = implode(' OR ', $conds); - $query = empty($condition)) + $query = empty($condition) ? "SELECT * FROM $table" : "SELECT * FROM $table WHERE $condition"; @@ -701,7 +706,7 @@ class TinderboxDS { } private function _doQueryHashRef($query, &$results, $params = array()) { - if (! $this->_doQuery($query, $params, $res)) + if (! $this->_doQuery($query, $params, $res)) { $results = null; return 0; } @@ -723,7 +728,7 @@ class TinderboxDS { return 0; } - $_res = count($params)) + $_res = count($params) ? $this->db->execute($sth, $params) : $this->db->execute($sth);