| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
class sfPostgreSQLDatabase extends sfDatabase |
|---|
| 41 |
{ |
|---|
| 42 |
|
|---|
| 43 |
* Connects to the database. |
|---|
| 44 |
* |
|---|
| 45 |
* @throws <b>sfDatabaseException</b> If a connection could not be created |
|---|
| 46 |
*/ |
|---|
| 47 |
public function connect() |
|---|
| 48 |
{ |
|---|
| 49 |
|
|---|
| 50 |
$method = $this->getParameter('method', 'normal'); |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
switch ($method) |
|---|
| 54 |
{ |
|---|
| 55 |
case 'normal': |
|---|
| 56 |
|
|---|
| 57 |
$database = $this->getParameter('database'); |
|---|
| 58 |
$host = $this->getParameter('host'); |
|---|
| 59 |
$password = $this->getParameter('password'); |
|---|
| 60 |
$port = $this->getParameter('port'); |
|---|
| 61 |
$username = $this->getParameter('username'); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
$string = ($database != null ? (' dbname=' .$database) : ''). |
|---|
| 65 |
($host != null ? (' host=' .$host) : ''). |
|---|
| 66 |
($password != null ? (' password=' .$password) : ''). |
|---|
| 67 |
($port != null ? (' port=' .$port) : ''). |
|---|
| 68 |
($username != null ? (' user=' .$username) : ''); |
|---|
| 69 |
|
|---|
| 70 |
break; |
|---|
| 71 |
|
|---|
| 72 |
case 'server': |
|---|
| 73 |
|
|---|
| 74 |
$string = $this->loadParameters($_SERVER); |
|---|
| 75 |
|
|---|
| 76 |
break; |
|---|
| 77 |
|
|---|
| 78 |
case 'env': |
|---|
| 79 |
|
|---|
| 80 |
$string = $this->loadParameters($_ENV); |
|---|
| 81 |
|
|---|
| 82 |
break; |
|---|
| 83 |
|
|---|
| 84 |
default: |
|---|
| 85 |
|
|---|
| 86 |
throw new sfDatabaseException(sprintf('Invalid PostgreSQLDatabase parameter retrieval method "%s".', $method)); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
$persistent = $this->getParameter('persistent', false); |
|---|
| 91 |
$connect = $persistent ? 'pg_pconnect' : 'pg_connect'; |
|---|
| 92 |
|
|---|
| 93 |
$this->connection = @$connect($string); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
if ($this->connection === false) |
|---|
| 97 |
{ |
|---|
| 98 |
|
|---|
| 99 |
throw new sfDatabaseException('Failed to create a PostgreSQLDatabase connection.'); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
// to the resource |
|---|
| 104 |
$this->resource = $this->connection; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
* Loads connection parameters from an existing array. |
|---|
| 109 |
* |
|---|
| 110 |
* @return string A connection string |
|---|
| 111 |
*/ |
|---|
| 112 |
protected function loadParameters(&$array) |
|---|
| 113 |
{ |
|---|
| 114 |
$database = $this->getParameter('database'); |
|---|
| 115 |
$host = $this->getParameter('host'); |
|---|
| 116 |
$password = $this->getParameter('password'); |
|---|
| 117 |
$port = $this->getParameter('port'); |
|---|
| 118 |
$username = $this->getParameter('username'); |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
$string = ($database != null ? (' dbname=' .$array[$database]) : ''). |
|---|
| 122 |
($host != null ? (' host=' .$array[$host]) : ''). |
|---|
| 123 |
($password != null ? (' password='.$array[$password]) : ''). |
|---|
| 124 |
($port != null ? (' port=' .$array[$port]) : ''). |
|---|
| 125 |
($username != null ? (' user=' .$array[$username]) : ''); |
|---|
| 126 |
|
|---|
| 127 |
return $string; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
* Executes the shutdown procedure. |
|---|
| 132 |
* |
|---|
| 133 |
* @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database |
|---|
| 134 |
*/ |
|---|
| 135 |
public function shutdown() |
|---|
| 136 |
{ |
|---|
| 137 |
if ($this->connection != null) |
|---|
| 138 |
{ |
|---|
| 139 |
@pg_close($this->connection); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|