]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/includes/db/cdb.class.php
bacula-web: New functions implementation in CDB class
[bacula/bacula] / gui / bacula-web / includes / db / cdb.class.php
1 <?php
2 /* 
3 +-------------------------------------------------------------------------+
4 | Copyright 2010-2011, Davide Franco                                              |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+ 
16 */
17
18 class CDB
19 {       
20         private $username;
21         private $password;
22         private $dsn;
23         private $connection;
24         private $options;
25         
26         private $result;
27         private $result_nb;
28         
29         public function __construct( $dsn, $user, $password )
30         {
31                 $this->dsn      = $dsn;
32                 $this->user     = $user;
33                 $this->password = $password;
34                 
35                 $this->options  = array(        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
36                                                                         PDO::ATTR_CASE => PDO::CASE_LOWER,
37                                                                         PDO::ATTR_STATEMENT_CLASS => array('CDBResult', array($this) ) );
38         }
39         
40         public function makeConnection()
41         {
42                 if( !isset( $this->connection ) )
43                         $this->connection = new PDO( $this->dsn, $this->user, $this->password );
44                 
45                 if( !is_a( $this->connection, 'PDO' ) )
46                         throw new CDBError("Failed to make database connection");
47                 
48                 $this->setOptions();            
49         }
50         
51         private function setOptions()
52         {
53                 foreach( $this->options as $option => $value )
54                         $this->connection->setAttribute( $option, $value);
55         }
56         
57         public function runQuery( $query) 
58         {
59                 $this->result =& $this->connection->prepare( $query );
60                                 
61                 if(!$this->result)
62                         throw new CDBError("Failed to execute query <br />$query");
63                 
64                 if( !$this->result->execute() )
65                         throw new CDBError("Failed to execute query <br />$query");
66                         
67                 if( !PEAR::isError($this->result) )             
68                         return true;
69         }
70         
71         public function getResult()
72         {
73                 $result = $this->result->fetchAll();
74                 $this->result_nb = count( $result );
75                 return $result;
76         }
77         
78         public function countResult()
79         {
80                 return $this->result_nb;
81         }
82         
83         public function triggerError( $message, $db_error)
84         {
85                 echo 'Error: ' . $message . '<br />';
86                 echo 'Standard Message: ' . $db_error->getMessage() . '<br />';
87                 echo 'Standard Code: ' . $db_error->getCode() . '<br />';
88                 echo 'DBMS/User Message: ' . $db_error->getUserInfo() . '<br />';
89                 echo 'DBMS/Debug Message: ' . $db_error->getDebugInfo() . '<br />';
90         }
91 }
92 ?>