| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfPDODatabase extends sfDatabase |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
* Connects to the database. |
|---|
| 27 |
* |
|---|
| 28 |
* @throws <b>sfDatabaseException</b> If a connection could not be created |
|---|
| 29 |
*/ |
|---|
| 30 |
public function connect() |
|---|
| 31 |
{ |
|---|
| 32 |
|
|---|
| 33 |
$method = $this->getParameter('method', 'dsn'); |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
switch ($method) |
|---|
| 37 |
{ |
|---|
| 38 |
case 'dsn': |
|---|
| 39 |
|
|---|
| 40 |
$dsn = $this->getParameter('dsn'); |
|---|
| 41 |
|
|---|
| 42 |
if ($dsn == null) |
|---|
| 43 |
{ |
|---|
| 44 |
|
|---|
| 45 |
throw new sfDatabaseException('Database configuration specifies method "dsn", but is missing dsn parameter.'); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
break; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
try |
|---|
| 52 |
{ |
|---|
| 53 |
$pdo_class = $this->getParameter('class', 'PDO'); |
|---|
| 54 |
$username = $this->getParameter('username'); |
|---|
| 55 |
$password = $this->getParameter('password'); |
|---|
| 56 |
$persistent = $this->getParameter('persistent'); |
|---|
| 57 |
|
|---|
| 58 |
$options = ($persistent) ? array(PDO::ATTR_PERSISTENT => true) : array(); |
|---|
| 59 |
|
|---|
| 60 |
$this->connection = new $pdo_class($dsn, $username, $password, $options); |
|---|
| 61 |
|
|---|
| 62 |
} |
|---|
| 63 |
catch (PDOException $e) |
|---|
| 64 |
{ |
|---|
| 65 |
throw new sfDatabaseException($e->getMessage()); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
if (sfConfig::get('sf_debug')) |
|---|
| 70 |
{ |
|---|
| 71 |
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|---|
| 72 |
} |
|---|
| 73 |
else |
|---|
| 74 |
{ |
|---|
| 75 |
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$compatability = $this->getParameter('compat'); |
|---|
| 80 |
if ($compatability) |
|---|
| 81 |
{ |
|---|
| 82 |
$this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
$nulls = $this->getParameter('nulls'); |
|---|
| 87 |
if ($nulls) |
|---|
| 88 |
{ |
|---|
| 89 |
$this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
$autocommit = $this->getParameter('autocommit'); |
|---|
| 94 |
if ($autocommit) |
|---|
| 95 |
{ |
|---|
| 96 |
$this->connection->setAttribute(PDO::ATTR_AUTOCOMMIT, true); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
$this->resource = $this->connection; |
|---|
| 100 |
|
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
* Execute the shutdown procedure. |
|---|
| 105 |
* |
|---|
| 106 |
* @return void |
|---|
| 107 |
*/ |
|---|
| 108 |
public function shutdown () |
|---|
| 109 |
{ |
|---|
| 110 |
if ($this->connection !== null) |
|---|
| 111 |
{ |
|---|
| 112 |
@$this->connection = null; |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
* Magic method for calling PDO directly via sfPDODatabase |
|---|
| 118 |
* |
|---|
| 119 |
* @param string $method |
|---|
| 120 |
* @param array $arguments |
|---|
| 121 |
* @return mixed |
|---|
| 122 |
*/ |
|---|
| 123 |
public function __call($method, $arguments) |
|---|
| 124 |
{ |
|---|
| 125 |
return $this->getConnection()->$method($arguments); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|