BAOBAB
CO_PDO Class Reference

This class provides a genericized interface to the PHP PDO toolkit. More...

Collaboration diagram for CO_PDO:

Public Member Functions

 __construct ( $driver, $host, $database, $user=NULL, $password=NULL, $charset=NULL)
 Initializes connection param class members. More...
 
 preparedExec ( $sql, $params=array())
 Wrapper for preparing and executing a PDOStatement that does not return a resultset e.g. INSERT or UPDATE SQL statements. More...
 
 preparedQuery ( $sql, $params=array(), $fetchKeyPair=false)
 Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statements. More...
 

Public Attributes

 $driver_type = NULL
 The type of PDO driver we are configured for. More...
 
 $class_description = NULL
 A simple description of this class. More...
 
 $last_insert = NULL
 This holds the integer ID of the last AUTO_INCREMENT insert. More...
 
 $owner_instance = NULL
 This is the instance of A_CO_DB that "owns" this instance. We can use this for auditing. More...
 

Private Attributes

 $_pdo = NULL
 Internal PDO object. More...
 
 $fetchMode = PDO::FETCH_ASSOC
 Default fetch mode for internal PDOStatements. More...
 

Detailed Description

This class provides a genericized interface to the PHP PDO toolkit.

Definition at line 32 of file co_pdo.class.php.

Constructor & Destructor Documentation

◆ __construct()

CO_PDO::__construct (   $driver,
  $host,
  $database,
  $user = NULL,
  $password = NULL,
  $charset = NULL 
)

Initializes connection param class members.

Must be called BEFORE any attempts to connect to or query a database.

Will destroy previous connection (if one exists).

Parameters
$driverdatabase server type (ex: 'mysql')
$hostdatabase server host
$databasedatabase name
$useruser, optional
$passwordpassword, optional
$charsetconnection charset, optional

Definition at line 56 of file co_pdo.class.php.

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  }
preparedExec( $sql, $params=array())
Wrapper for preparing and executing a PDOStatement that does not return a resultset e....

Member Function Documentation

◆ preparedExec()

CO_PDO::preparedExec (   $sql,
  $params = array() 
)

Wrapper for preparing and executing a PDOStatement that does not return a resultset e.g. INSERT or UPDATE SQL statements.

See PDO documentation about prepared queries.

If there isn't already a database connection, it will "lazy load" the connection.

Exceptions
Exceptionthrown if internal PDO exception is thrown
Returns
true if execution is successful.
Parameters
$sqlsame as kind provided to PDO::prepare()
$paramssame as kind provided to PDO::prepare()

Definition at line 96 of file co_pdo.class.php.

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  }
preparedQuery( $sql, $params=array(), $fetchKeyPair=false)
Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statement...

◆ preparedQuery()

CO_PDO::preparedQuery (   $sql,
  $params = array(),
  $fetchKeyPair = false 
)

Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statements.

Returns a multidimensional array depending on internal fetch mode setting ($this->fetchMode) See PDO documentation about prepared queries.

Fetching key pairs- when $fetchKeyPair is set to true, it will force the returned array to be a one-dimensional array indexed on the first column in the query. Note- query may contain only two columns or an exception/error is thrown. See PDO::PDO::FETCH_KEY_PAIR for more details

Returns
associative array of results.
Exceptions
Exceptionthrown if internal PDO exception is thrown
Parameters
$sqlsame as kind provided to PDO::prepare()
$paramssame as kind provided to PDO::prepare()
$fetchKeyPairSee description in method documentation

Definition at line 149 of file co_pdo.class.php.

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  }

Member Data Documentation

◆ $_pdo

CO_PDO::$_pdo = NULL
private

Internal PDO object.

Definition at line 34 of file co_pdo.class.php.

◆ $class_description

CO_PDO::$class_description = NULL

A simple description of this class.

Definition at line 38 of file co_pdo.class.php.

◆ $driver_type

CO_PDO::$driver_type = NULL

The type of PDO driver we are configured for.

Definition at line 36 of file co_pdo.class.php.

◆ $fetchMode

CO_PDO::$fetchMode = PDO::FETCH_ASSOC
private

Default fetch mode for internal PDOStatements.

Definition at line 45 of file co_pdo.class.php.

◆ $last_insert

CO_PDO::$last_insert = NULL

This holds the integer ID of the last AUTO_INCREMENT insert.

Definition at line 40 of file co_pdo.class.php.

◆ $owner_instance

CO_PDO::$owner_instance = NULL

This is the instance of A_CO_DB that "owns" this instance. We can use this for auditing.

Definition at line 42 of file co_pdo.class.php.