| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Pgsql.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | Doctrine::autoload("Doctrine_Connection_Common"); |
| 22 | /** |
| 23 | * Doctrine_Connection_Pgsql |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Connection |
| 27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 28 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 29 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
| 30 | * @version $Revision: 2963 $ |
| 31 | * @link www.phpdoctrine.org |
| 32 | * @since 1.0 |
| 33 | */ |
| 34 | class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common |
| 35 | { |
| 36 | /** |
| 37 | * @var string $driverName the name of this connection driver |
| 38 | */ |
| 39 | protected $driverName = 'Pgsql'; |
| 40 | |
| 41 | /** |
| 42 | * the constructor |
| 43 | * |
| 44 | * @param Doctrine_Manager $manager |
| 45 | * @param PDO $pdo database handle |
| 46 | */ |
| 47 | public function __construct(Doctrine_Manager $manager, $adapter) |
| 48 | { |
| 49 | // initialize all driver options |
| 50 | $this->supported = array( |
| 51 | 'sequences' => true, |
| 52 | 'indexes' => true, |
| 53 | 'affected_rows' => true, |
| 54 | 'summary_functions' => true, |
| 55 | 'order_by_text' => true, |
| 56 | 'transactions' => true, |
| 57 | 'savepoints' => true, |
| 58 | 'current_id' => true, |
| 59 | 'limit_queries' => true, |
| 60 | 'LOBs' => true, |
| 61 | 'replace' => 'emulated', |
| 62 | 'sub_selects' => true, |
| 63 | 'auto_increment' => 'emulated', |
| 64 | 'primary_key' => true, |
| 65 | 'result_introspection' => true, |
| 66 | 'prepared_statements' => true, |
| 67 | 'identifier_quoting' => true, |
| 68 | 'pattern_escaping' => true, |
| 69 | ); |
| 70 | |
| 71 | $this->properties['string_quoting'] = array('start' => "'", |
| 72 | 'end' => "'", |
| 73 | 'escape' => "'", |
| 74 | 'escape_pattern' => '\\'); |
| 75 | |
| 76 | $this->properties['identifier_quoting'] = array('start' => '"', |
| 77 | 'end' => '"', |
| 78 | 'escape' => '"'); |
| 79 | parent::__construct($manager, $adapter); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Set the charset on the current connection |
| 84 | * |
| 85 | * @param string charset |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public function setCharset($charset) |
| 90 | { |
| 91 | $query = 'SET NAMES '.$this->dbh->quote($charset); |
| 92 | $this->exec($query); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * convertBoolean |
| 97 | * some drivers need the boolean values to be converted into integers |
| 98 | * when using DQL API |
| 99 | * |
| 100 | * This method takes care of that conversion |
| 101 | * |
| 102 | * @param array $item |
| 103 | * @return void |
| 104 | */ |
| 105 | public function convertBooleans($item) |
| 106 | { |
| 107 | if (is_array($item)) { |
| 108 | foreach ($item as $key => $value) { |
| 109 | if (is_bool($value)) { |
| 110 | $item[$key] = ($value) ? 'true' : 'false'; |
| 111 | } |
| 112 | } |
| 113 | } else { |
| 114 | if (is_bool($item)) { |
| 115 | $item = ($item) ? 'true' : 'false'; |
| 116 | } |
| 117 | } |
| 118 | return $item; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Changes a query string for various DBMS specific reasons |
| 123 | * |
| 124 | * @param string $query query to modify |
| 125 | * @param integer $limit limit the number of rows |
| 126 | * @param integer $offset start reading from given offset |
| 127 | * @param boolean $isManip if the query is a DML query |
| 128 | * @return string modified query |
| 129 | */ |
| 130 | public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false) |
| 131 | { |
| 132 | if ($limit > 0) { |
| 133 | $query = rtrim($query); |
| 134 | |
| 135 | if (substr($query, -1) == ';') { |
| 136 | $query = substr($query, 0, -1); |
| 137 | } |
| 138 | |
| 139 | if ($isManip) { |
| 140 | $manip = preg_replace('/^(DELETE FROM|UPDATE).*$/', '\\1', $query); |
| 141 | $from = $match[2]; |
| 142 | $where = $match[3]; |
| 143 | $query = $manip . ' ' . $from . ' WHERE ctid=(SELECT ctid FROM ' |
| 144 | . $from . ' ' . $where . ' LIMIT ' . $limit . ')'; |
| 145 | |
| 146 | } else { |
| 147 | if ( ! empty($limit)) { |
| 148 | $query .= ' LIMIT ' . $limit; |
| 149 | } |
| 150 | if ( ! empty($offset)) { |
| 151 | $query .= ' OFFSET ' . $offset; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | return $query; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * return version information about the server |
| 160 | * |
| 161 | * @param string $native determines if the raw version string should be returned |
| 162 | * @return array|string an array or string with version information |
| 163 | */ |
| 164 | public function getServerVersion($native = false) |
| 165 | { |
| 166 | $query = 'SHOW SERVER_VERSION'; |
| 167 | |
| 168 | $serverInfo = $this->fetchOne($query); |
| 169 | |
| 170 | if ( ! $native) { |
| 171 | $tmp = explode('.', $serverInfo, 3); |
| 172 | |
| 173 | if (empty($tmp[2]) && isset($tmp[1]) |
| 174 | && preg_match('/(\d+)(.*)/', $tmp[1], $tmp2) |
| 175 | ) { |
| 176 | $serverInfo = array( |
| 177 | 'major' => $tmp[0], |
| 178 | 'minor' => $tmp2[1], |
| 179 | 'patch' => null, |
| 180 | 'extra' => $tmp2[2], |
| 181 | 'native' => $serverInfo, |
| 182 | ); |
| 183 | } else { |
| 184 | $serverInfo = array( |
| 185 | 'major' => isset($tmp[0]) ? $tmp[0] : null, |
| 186 | 'minor' => isset($tmp[1]) ? $tmp[1] : null, |
| 187 | 'patch' => isset($tmp[2]) ? $tmp[2] : null, |
| 188 | 'extra' => null, |
| 189 | 'native' => $serverInfo, |
| 190 | ); |
| 191 | } |
| 192 | } |
| 193 | return $serverInfo; |
| 194 | } |
| 195 | } |