BAOBAB
co_pdo.class.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************************************************************/
26 defined( 'LGV_DB_CATCHER' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
27 
28 /***************************************************************************************************************************/
32 class CO_PDO {
34  private $_pdo = NULL;
36  var $driver_type = NULL;
38  var $class_description = NULL;
40  var $last_insert = NULL;
42  var $owner_instance = NULL;
43 
45  private $fetchMode = PDO::FETCH_ASSOC;
46 
47  /***********************************************************************************************************************/
48  /***********************/
56  public function __construct( $driver,
57  $host,
58  $database,
59  $user = NULL,
60  $password = NULL,
61  $charset = NULL
62  ) {
63 
64  $this->class_description = 'A class for managing PDO access to the databases.';
65 
66  $this->_pdo = NULL;
67  $this->driver_type = $driver;
68  $this->last_insert = NULL;
69 
70  $dsn = $driver . ':host=' . $host . ';dbname=' . $database ;
71  try {
72  $this->_pdo = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
73  $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
74  $this->_pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
75  $this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
76  if (strlen($charset) > 0) {
77  self::preparedExec('SET NAMES :charset', array(':charset' => $charset), false);
78  }
79  } catch (PDOException $exception) {
80  throw new Exception(__METHOD__ . '() ' . $exception->getMessage());
81  }
82  }
83 
84  /***********************/
96  public function preparedExec( $sql,
97  $params = array()
98  )
99  {
100  $this->last_insert = NULL;
101  try {
102  if ('pgsql' == $this->driver_type) {
103  if (strpos($sql, 'RETURNING id;')) {
104  $response = $this->preparedQuery($sql, $params);
105  $this->last_insert = intval($response[0]['id']);
106  return true;
107  }
108  }
109 
110  $sql = str_replace(' RETURNING id', '', $sql);
111  // This represents a potential MASSIVE security, performnce and legal issue. This should ONLY be used for debugging!
112  if (method_exists('CO_Config', 'call_low_level_log_handler_function')) {
113  CO_Config::call_low_level_log_handler_function(isset($this->owner_instance) ? $this->owner_instance->access_object->get_login_id() : 0, $sql, $params);
114  }
115  $this->_pdo->beginTransaction();
116  $stmt = $this->_pdo->prepare($sql);
117  $stmt->execute($params);
118  if ('pgsql' != $this->driver_type) {
119  $this->last_insert = $this->_pdo->lastInsertId();
120  }
121  $this->_pdo->commit();
122 
123  return true;
124  } catch (PDOException $exception) {
125  $this->last_insert = NULL;
126  $this->_pdo->rollback();
127  throw new Exception(__METHOD__ . '() ' . $exception->getMessage());
128  }
129 
130  return false;
131  }
132 
133  /***********************/
149  public function preparedQuery( $sql,
150  $params = array(),
151  $fetchKeyPair = false
152  ) {
153  $this->last_insert = NULL;
154  try {
155  // This represents a potential MASSIVE security, performnce and legal issue. This should ONLY be used for debugging!
156  if (method_exists('CO_Config', 'call_low_level_log_handler_function')) {
157  CO_Config::call_low_level_log_handler_function(isset($this->owner_instance) ? $this->owner_instance->access_object->get_login_id() : 0, $sql, $params);
158  }
159  $this->_pdo->beginTransaction();
160  $stmt = $this->_pdo->prepare($sql);
161  $stmt->setFetchMode($this->fetchMode);
162  $stmt->execute($params);
163  $this->_pdo->commit();
164 
165  $ret = NULL;
166 
167  if ($fetchKeyPair) {
168  $ret = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
169  } else {
170  $ret = $stmt->fetchAll();
171  }
172 
173  return $ret;
174  } catch (PDOException $exception) {
175  $this->last_insert = NULL;
176  $this->_pdo->rollback();
177  throw new Exception(__METHOD__ . '() ' . $exception->getMessage());
178  }
179 
180  return false;
181  }
182 };
This class provides a genericized interface to the PHP PDO toolkit.
preparedQuery( $sql, $params=array(), $fetchKeyPair=false)
Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statement...
preparedExec( $sql, $params=array())
Wrapper for preparing and executing a PDOStatement that does not return a resultset e....
__construct( $driver, $host, $database, $user=NULL, $password=NULL, $charset=NULL)
Initializes connection param class members.