# Source for file Pgsql.php

Documentation is available at Pgsql.php

1. <?php
2. /*
3. * $Id: Pgsql.php 2113 2007-07-31 05:50:41Z lukenukem $
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.com>.
20. */
21. Doctrine::autoload("Doctrine_Connection_Common");
22. /**
23. * Doctrine_Connection_Pgsql
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
28. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
29. * @version $Revision: 2113 $
30. * @category Object Relational Mapping
31. * @link www.phpdoctrine.com
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. * the constructor
42. *
43. * @param Doctrine_Manager $manager
44. * @param PDO $pdo database handle
45. */
46. public function __construct(Doctrine_Manager $manager, $adapter)
47. {
48. // initialize all driver options
49. $this->supported = array(
50. 'sequences' => true,
51. 'indexes' => true,
52. 'affected_rows' => true,
53. 'summary_functions' => true,
54. 'order_by_text' => true,
55. 'transactions' => true,
56. 'savepoints' => true,
57. 'current_id' => true,
58. 'limit_queries' => true,
59. 'LOBs' => true,
60. 'replace' => 'emulated',
61. 'sub_selects' => true,
62. 'auto_increment' => 'emulated',
63. 'primary_key' => true,
64. 'result_introspection' => true,
65. 'prepared_statements' => true,
66. 'identifier_quoting' => true,
67. 'pattern_escaping' => true,
68. );
70. $this->properties['string_quoting'] = array('start' => "'",
71. 'end' => "'",
72. 'escape' => "'",
73. 'escape_pattern' => '\\');
75. $this->properties['identifier_quoting'] = array('start' => '"',
76. 'end' => '"',
77. 'escape' => '"');
78. parent::__construct($manager, $adapter);
79. }
80. /**
81. * Set the charset on the current connection
82. *
83. * @param string charset
84. *
85. * @return void
86. */
87. public function setCharset($charset)
88. {
89. $query = 'SET NAMES '.$this->dbh->quote($charset);
90. $this->exec($query);
91. }
92. /**
93. * convertBoolean
94. * some drivers need the boolean values to be converted into integers
95. * when using DQL API
96. *
97. * This method takes care of that conversion
98. *
99. * @param array $item
100. * @return void
101. */
102. public function convertBooleans($item)
103. {
104. if (is_array($item)) {
105. foreach ($item as $key => $value) {
106. if (is_bool($value)) {
107. $item[$key] = ($value) ? 'true' : 'false';
108. }
109. }
110. } else {
111. if (is_bool($item)) {
112. $item = ($item) ? 'true' : 'false';
113. }
114. }
115. return $item;
116. }
117. /**
118. * Changes a query string for various DBMS specific reasons
119. *
120. * @param string $query query to modify
121. * @param integer $limit limit the number of rows
122. * @param integer $offset start reading from given offset
123. * @param boolean $isManip if the query is a DML query
124. * @return string modified query
125. */
126. public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false)
127. {
128. if ($limit > 0) {
129. $query = rtrim($query);
131. if (substr($query, -1) == ';') {
132. $query = substr($query, 0, -1);
133. }
135. if ($isManip) {
136. $manip = preg_replace('/^(DELETE FROM|UPDATE).*$/', '\\1', $query);
137. $from = $match[2];
138. $where = $match[3];
139. $query = $manip . ' ' . $from . ' WHERE ctid=(SELECT ctid FROM '
140. . $from . ' ' . $where . ' LIMIT ' . $limit . ')';
142. } else {
143. if ( ! empty($limit)) {
144. $query .= ' LIMIT ' . $limit;
145. }
146. if ( ! empty($offset)) {
147. $query .= ' OFFSET ' . $offset;
148. }
149. }
150. }
151. return $query;
152. }
153. /**
154. * return version information about the server
155. *
156. * @param string $native determines if the raw version string should be returned
157. * @return array|string an array or string with version information
158. */
159. public function getServerVersion($native = false)
160. {
161. $query = 'SHOW SERVER_VERSION';
163. $serverInfo = $this->fetchOne($query);
165. if ( ! $native) {
166. $tmp = explode('.', $serverInfo, 3);
168. if (empty($tmp[2]) && isset($tmp[1])
169. && preg_match('/(\d+)(.*)/', $tmp[1], $tmp2)
170. ) {
171. $serverInfo = array(
172. 'major' => $tmp[0],
173. 'minor' => $tmp2[1],
174. 'patch' => null,
175. 'extra' => $tmp2[2],
176. 'native' => $serverInfo,
177. );
178. } else {
179. $serverInfo = array(
180. 'major' => isset($tmp[0]) ? $tmp[0] : null,
181. 'minor' => isset($tmp[1]) ? $tmp[1] : null,
182. 'patch' => isset($tmp[2]) ? $tmp[2] : null,
183. 'extra' => null,
184. 'native' => $serverInfo,
185. );
186. }
187. }
188. return $serverInfo;
189. }
190. }