3 +-------------------------------------------------------------------------+
4 | Copyright 2010-2011, Davide Franco |
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. |
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 +-------------------------------------------------------------------------+
29 public function __construct( $dsn, $user, $password )
33 $this->password = $password;
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) ) );
40 public function makeConnection()
42 if( !isset( $this->connection ) )
43 $this->connection = new PDO( $this->dsn, $this->user, $this->password );
45 if( !is_a( $this->connection, 'PDO' ) )
46 throw new CDBError("Failed to make database connection");
51 private function setOptions()
53 foreach( $this->options as $option => $value )
54 $this->connection->setAttribute( $option, $value);
57 public function runQuery( $query)
59 $this->result =& $this->connection->prepare( $query );
62 throw new CDBError("Failed to execute query <br />$query");
64 if( !$this->result->execute() )
65 throw new CDBError("Failed to execute query <br />$query");
67 if( !PEAR::isError($this->result) )
71 public function getResult()
73 $result = $this->result->fetchAll();
74 $this->result_nb = count( $result );
78 public function countResult()
80 return $this->result_nb;
83 public function triggerError( $message, $db_error)
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 />';