# Source for file Pgsql.php

Documentation is available at Pgsql.php

1. <?php
2. /*
3. * $Id: Pgsql.php 2276 2007-08-25 08:06:17Z 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_Expression_Driver');
22. /**
23. * Doctrine_Expression_Pgsql
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision: 2276 $
31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32. */
33. class Doctrine_Expression_Pgsql extends Doctrine_Expression_Driver
34. {
35. /**
36. * Returns the md5 sum of a field.
37. *
38. * Note: Not SQL92, but common functionality
39. *
40. * md5() works with the default PostgreSQL 8 versions.
41. *
42. * If you are using PostgreSQL 7.x or older you need
43. * to make sure that the digest procedure is installed.
44. * If you use RPMS (Redhat and Mandrake) install the postgresql-contrib
45. * package. You must then install the procedure by running this shell command:
46. * <code>
47. * psql [dbname] < /usr/share/pgsql/contrib/pgcrypto.sql
48. * </code>
49. * You should make sure you run this as the postgres user.
50. *
51. * @return string
52. */
53. public function md5($column)
54. {
55. $column = $this->getIdentifier($column);
57. if ($this->version > 7) {
58. return 'MD5(' . $column . ')';
59. } else {
60. return 'encode(digest(' . $column .', md5), hex)';
61. }
62. }
64. /**
65. * Returns part of a string.
66. *
67. * Note: Not SQL92, but common functionality.
68. *
69. * @param string $value the target $value the string or the string column.
70. * @param int $from extract from this characeter.
71. * @param int $len extract this amount of characters.
72. * @return string sql that extracts part of a string.
73. */
74. public function substring($value, $from, $len = null)
75. {
76. $value = $this->getIdentifier($value);
78. if ($len === null) {
79. $len = $this->getIdentifier($len);
80. return 'SUBSTR(' . $value . ', ' . $from . ')';
81. } else {
82. return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
83. }
84. }
86. /**
87. * Returns a series of strings concatinated
88. *
89. * concat() accepts an arbitrary number of parameters. Each parameter
90. * must contain an expression or an array with expressions.
91. *
92. * @param string|array(string)strings that will be concatinated.
93. * @return string
94. */
97. /**
98. * PostgreSQLs AGE(<timestamp1> [, <timestamp2>]) function.
99. *
100. * @param string $timestamp1 timestamp to subtract from NOW()
101. * @param string $timestamp2 optional; if given: subtract arguments
102. * @return string
103. */
104. public function age($timestamp1, $timestamp2 = null) {
105. if( $timestamp2 == null ) {
106. return 'AGE(' . $timestamp1 . ')';
107. }
108. return 'AGE(' . $timestamp1 . ', ' . $timestamp2 . ')';
109. }
111. /**
112. * PostgreSQLs DATE_PART( <text>, <time> ) function.
113. *
114. * @param string $text what to extract
115. * @param string $time timestamp or interval to extract from
116. * @return string
117. */
118. public function date_part($text, $time) {
119. return 'DATE_PART(' . $text . ', ' . $time . ')';
120. }
123. public function concat()
124. {
125. $args = func_get_args();
127. return join(' || ' , $args);
128. }
129. /**
130. * Returns the SQL string to return the current system date and time.
131. *
132. * @return string
133. */
134. public function now()
135. {
136. return 'LOCALTIMESTAMP(0)';
137. }
138. /**
139. * regexp
140. *
141. * @return string the regular expression operator
142. */
143. public function regexp()
144. {
145. return 'SIMILAR TO';
146. }
147. /**
148. * return string to call a function to get random value inside an SQL statement
149. *
150. * @return return string to generate float between 0 and 1
151. * @access public
152. */
153. public function random()
154. {
155. return 'RANDOM()';
156. }
157. /**
158. * build a pattern matching string
159. *
160. * EXPERIMENTAL
161. *
162. * WARNING: this function is experimental and may change signature at
163. * any time until labelled as non-experimental
164. *
165. * @access public
166. *
167. * @param array $pattern even keys are strings, odd are patterns (% and _)
168. * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
169. * @param string $field optional field name that is being matched against
170. * (might be required when emulating ILIKE)
171. *
172. * @return string SQL pattern
173. */
174. public function matchPattern($pattern, $operator = null, $field = null)
175. {
176. $match = '';
177. if (!is_null($operator)) {
178. $field = is_null($field) ? '' : $field.' ';
179. $operator = strtoupper($operator);
180. switch ($operator) {
181. // case insensitive
182. case 'ILIKE':
183. $match = $field.'ILIKE ';
184. break;
185. // case sensitive
186. case 'LIKE':
187. $match = $field.'LIKE ';
188. break;
189. default:
190. throw new Doctrine_Expression_Pgsql_Exception('not a supported operator type:'. $operator);
191. }
192. }
193. $match.= "'";
194. foreach ($pattern as $key => $value) {
195. if ($key % 2) {
196. $match.= $value;
197. } else {
198. $match.= $this->conn->escapePattern($this->conn->escape($value));
199. }
200. }
201. $match.= "'";
202. $match.= $this->patternEscapeString();
203. return $match;
204. }
205. }