From: Davide Franco Date: Fri, 15 Jul 2011 13:59:21 +0000 (+0200) Subject: bacula-web: New functions implementation in CDB class X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=448dcf69c5f92d65e865eb1e38f7cb4b6c95eea8;p=bacula%2Fbacula bacula-web: New functions implementation in CDB class --- diff --git a/gui/bacula-web/includes/db/cdb.class.php b/gui/bacula-web/includes/db/cdb.class.php index 922f492762..0cfb0203a2 100644 --- a/gui/bacula-web/includes/db/cdb.class.php +++ b/gui/bacula-web/includes/db/cdb.class.php @@ -17,14 +17,76 @@ class CDB { - public function __construct() + private $username; + private $password; + private $dsn; + private $connection; + private $options; + + private $result; + private $result_nb; + + public function __construct( $dsn, $user, $password ) + { + $this->dsn = $dsn; + $this->user = $user; + $this->password = $password; + + $this->options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_CASE => PDO::CASE_LOWER, + PDO::ATTR_STATEMENT_CLASS => array('CDBResult', array($this) ) ); + } + + public function makeConnection() { + if( !isset( $this->connection ) ) + $this->connection = new PDO( $this->dsn, $this->user, $this->password ); + + if( !is_a( $this->connection, 'PDO' ) ) + throw new CDBError("Failed to make database connection"); + + $this->setOptions(); + } + private function setOptions() + { + foreach( $this->options as $option => $value ) + $this->connection->setAttribute( $option, $value); } - public function __destruct() + public function runQuery( $query) { + $this->result =& $this->connection->prepare( $query ); + + if(!$this->result) + throw new CDBError("Failed to execute query
$query"); + + if( !$this->result->execute() ) + throw new CDBError("Failed to execute query
$query"); + + if( !PEAR::isError($this->result) ) + return true; + } + public function getResult() + { + $result = $this->result->fetchAll(); + $this->result_nb = count( $result ); + return $result; + } + + public function countResult() + { + return $this->result_nb; + } + + public function triggerError( $message, $db_error) + { + echo 'Error: ' . $message . '
'; + echo 'Standard Message: ' . $db_error->getMessage() . '
'; + echo 'Standard Code: ' . $db_error->getCode() . '
'; + echo 'DBMS/User Message: ' . $db_error->getUserInfo() . '
'; + echo 'DBMS/Debug Message: ' . $db_error->getDebugInfo() . '
'; } } ?>