Hoa central
Pdo.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Core;
40 use Hoa\Database;
41 
50 class Pdo implements Database\IDal\Wrapper
51 {
57  protected $_connection = null;
58 
59 
60 
71  public function __construct(
72  $dsn,
73  $username,
74  $password,
75  Array $driverOptions = []
76  ) {
77  if (false === extension_loaded('pdo')) {
78  throw new Database\Exception(
79  'The module PDO is not enabled.',
80  0
81  );
82  }
83 
84  $connection = null;
85 
86  try {
87  $connection = new \PDO($dsn, $username, $password, $driverOptions);
88  } catch (\PDOException $e) {
89  throw new Database\Exception(
90  $e->getMessage(),
91  $e->getCode(),
92  null,
93  $e
94  );
95  }
96 
97  $this->setConnection($connection);
98 
99  return;
100  }
101 
108  protected function setConnection(\PDO $connection)
109  {
110  $old = $this->_connection;
111  $this->_connection = $connection;
112 
113  return $old;
114  }
115 
122  protected function getConnection()
123  {
124  if (null === $this->_connection) {
125  throw new Database\Exception(
126  'Cannot return a null connection.',
127  1
128  );
129  }
130 
131  return $this->_connection;
132  }
133 
140  public function beginTransaction()
141  {
142  return $this->getConnection()->beginTransaction();
143  }
144 
151  public function commit()
152  {
153  return $this->getConnection()->commit();
154  }
155 
162  public function rollBack()
163  {
164  return $this->getConnection()->rollBack();
165  }
166 
175  public function lastInsertId($name = null)
176  {
177  if (null === $name) {
178  return $this->getConnection()->lastInsertId();
179  }
180 
181  return $this->getConnection()->lastInsertId($name);
182  }
183 
194  public function prepare($statement, Array $options = [])
195  {
196  $handle = $this->getConnection()->prepare($statement);
197 
198  if (!($handle instanceof \PDOStatement)) {
199  throw new Database\Exception(
200  '%3$s (%1$s/%2$d).',
201  2,
202  $this->errorInfo()
203  );
204  }
205 
206  return new Statement($handle);
207  }
208 
218  public function quote($string = null, $type = -1)
219  {
220  if ($type < 0) {
221  return $this->getConnection()->quote($string);
222  }
223 
224  return $this->getConnection()->quote($string, $type);
225  }
226 
235  public function query($statement)
236  {
237  $handle = $this->getConnection()->query($statement);
238 
239  if (!($handle instanceof \PDOStatement)) {
240  throw new Database\Exception(
241  '%3$s (%1$s/%2$d).',
242  3,
243  $this->errorInfo()
244  );
245  }
246 
247  return new Statement($handle);
248  }
249 
257  public function errorCode()
258  {
259  return $this->getConnection()->errorCode();
260  }
261 
269  public function errorInfo()
270  {
271  return $this->getConnection()->errorInfo();
272  }
273 
280  public function getAvailableDrivers()
281  {
282  return $this->getConnection()->getAvailableDrivers();
283  }
284 
292  public function setAttributes(Array $attributes)
293  {
294  $out = true;
295 
296  foreach ($attributes as $attribute => $value) {
297  $out &= $this->setAttribute($attribute, $value);
298  }
299 
300  return (bool) $out;
301  }
302 
311  public function setAttribute($attribute, $value)
312  {
313  return $this->getConnection()->setAttribute($attribute, $value);
314  }
315 
322  public function getAttributes()
323  {
324  $out = [];
325  $attributes = [
326  0 => 'AUTOCOMMIT',
327  1 => 'CASE',
328  2 => 'CLIENT_VERSION',
329  3 => 'CONNECTION_STATUS',
330  4 => 'DRIVER_NAME',
331  5 => 'ERRMODE',
332  6 => 'ORACLE_NULLS',
333  7 => 'PERSISTENT',
334  8 => 'PREFETCH',
335  9 => 'SERVER_INFO',
336  10 => 'SERVER_VERSION',
337  11 => 'TIMEOUT'
338  ];
339 
340  foreach ($attributes as $attribute) {
341  $out[$attribute] = $this->getAttribute($attribute);
342  }
343 
344  return $out;
345  }
346 
354  public function getAttribute($attribute)
355  {
356  return
357  $this
358  ->getConnection()
359  ->getAttribute(constant('\PDO::ATTR_' . $attribute));
360  }
361 }
362 
366 Core\Consistency::flexEntity('Hoa\Database\Layer\Pdo\Pdo');
query($statement)
Definition: Pdo.php:235
getAttribute($attribute)
Definition: Pdo.php:354
setAttribute($attribute, $value)
Definition: Pdo.php:311
setAttributes(Array $attributes)
Definition: Pdo.php:292
quote($string=null, $type=-1)
Definition: Pdo.php:218
lastInsertId($name=null)
Definition: Pdo.php:175
setConnection(\PDO $connection)
Definition: Pdo.php:108
prepare($statement, Array $options=[])
Definition: Pdo.php:194
__construct($dsn, $username, $password, Array $driverOptions=[])
Definition: Pdo.php:71