| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Pgsql.php 7685 2010-08-24 16:43:34Z jwage $
|
|
|
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.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Expression_Pgsql
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Expression
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7685 $
|
|
|
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);
|
|
|
56 |
|
|
|
57 |
return 'MD5(' . $column . ')';
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Returns part of a string.
|
|
|
62 |
*
|
|
|
63 |
* Note: Not SQL92, but common functionality.
|
|
|
64 |
*
|
|
|
65 |
* @param string $value the target $value the string or the string column.
|
|
|
66 |
* @param int $from extract from this characeter.
|
|
|
67 |
* @param int $len extract this amount of characters.
|
|
|
68 |
* @return string sql that extracts part of a string.
|
|
|
69 |
*/
|
|
|
70 |
public function substring($value, $from, $len = null)
|
|
|
71 |
{
|
|
|
72 |
$value = $this->getIdentifier($value);
|
|
|
73 |
|
|
|
74 |
if ($len === null) {
|
|
|
75 |
$len = $this->getIdentifier($len);
|
|
|
76 |
return 'SUBSTR(' . $value . ', ' . $from . ')';
|
|
|
77 |
} else {
|
|
|
78 |
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Returns a series of strings concatinated
|
|
|
84 |
*
|
|
|
85 |
* concat() accepts an arbitrary number of parameters. Each parameter
|
|
|
86 |
* must contain an expression or an array with expressions.
|
|
|
87 |
*
|
|
|
88 |
* @param string|array(string) strings that will be concatinated.
|
|
|
89 |
* @return string
|
|
|
90 |
*/
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* PostgreSQLs AGE(<timestamp1> [, <timestamp2>]) function.
|
|
|
95 |
*
|
|
|
96 |
* @param string $timestamp1 timestamp to subtract from NOW()
|
|
|
97 |
* @param string $timestamp2 optional; if given: subtract arguments
|
|
|
98 |
* @return string
|
|
|
99 |
*/
|
|
|
100 |
public function age($timestamp1, $timestamp2 = null) {
|
|
|
101 |
if ( $timestamp2 == null ) {
|
|
|
102 |
return 'AGE(' . $timestamp1 . ')';
|
|
|
103 |
}
|
|
|
104 |
return 'AGE(' . $timestamp1 . ', ' . $timestamp2 . ')';
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* PostgreSQLs DATE_PART( <text>, <time> ) function.
|
|
|
109 |
*
|
|
|
110 |
* @param string $text what to extract
|
|
|
111 |
* @param string $time timestamp or interval to extract from
|
|
|
112 |
* @return string
|
|
|
113 |
*/
|
|
|
114 |
public function date_part($text, $time)
|
|
|
115 |
{
|
|
|
116 |
return 'DATE_PART(' . $text . ', ' . $time . ')';
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* PostgreSQLs TO_CHAR( <time>, <text> ) function.
|
|
|
121 |
*
|
|
|
122 |
* @param string $time timestamp or interval
|
|
|
123 |
* @param string $text how to the format the output
|
|
|
124 |
* @return string
|
|
|
125 |
*/
|
|
|
126 |
public function to_char($time, $text) {
|
|
|
127 |
return 'TO_CHAR(' . $time . ', ' . $text . ')';
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* PostgreSQLs CONCAT() function
|
|
|
132 |
*
|
|
|
133 |
* @param an array of values
|
|
|
134 |
* @return string
|
|
|
135 |
*/
|
|
|
136 |
public function concat()
|
|
|
137 |
{
|
|
|
138 |
$args = func_get_args();
|
|
|
139 |
|
|
|
140 |
return join(' || ' , $args);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Returns the SQL string to return the current system date and time.
|
|
|
145 |
*
|
|
|
146 |
* @return string
|
|
|
147 |
*/
|
|
|
148 |
public function now()
|
|
|
149 |
{
|
|
|
150 |
return 'LOCALTIMESTAMP(0)';
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* regexp
|
|
|
155 |
*
|
|
|
156 |
* @return string the regular expression operator
|
|
|
157 |
*/
|
|
|
158 |
public function regexp()
|
|
|
159 |
{
|
|
|
160 |
return 'SIMILAR TO';
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* return string to call a function to get random value inside an SQL statement
|
|
|
165 |
*
|
|
|
166 |
* @return return string to generate float between 0 and 1
|
|
|
167 |
* @access public
|
|
|
168 |
*/
|
|
|
169 |
public function random()
|
|
|
170 |
{
|
|
|
171 |
return 'RANDOM()';
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* build a pattern matching string
|
|
|
176 |
*
|
|
|
177 |
* EXPERIMENTAL
|
|
|
178 |
*
|
|
|
179 |
* WARNING: this function is experimental and may change signature at
|
|
|
180 |
* any time until labelled as non-experimental
|
|
|
181 |
*
|
|
|
182 |
* @access public
|
|
|
183 |
*
|
|
|
184 |
* @param array $pattern even keys are strings, odd are patterns (% and _)
|
|
|
185 |
* @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
|
|
|
186 |
* @param string $field optional field name that is being matched against
|
|
|
187 |
* (might be required when emulating ILIKE)
|
|
|
188 |
*
|
|
|
189 |
* @return string SQL pattern
|
|
|
190 |
*/
|
|
|
191 |
public function matchPattern($pattern, $operator = null, $field = null)
|
|
|
192 |
{
|
|
|
193 |
$match = '';
|
|
|
194 |
if ( ! is_null($operator)) {
|
|
|
195 |
$field = is_null($field) ? '' : $field.' ';
|
|
|
196 |
$operator = strtoupper($operator);
|
|
|
197 |
switch ($operator) {
|
|
|
198 |
// case insensitive
|
|
|
199 |
case 'ILIKE':
|
|
|
200 |
$match = $field.'ILIKE ';
|
|
|
201 |
break;
|
|
|
202 |
// case sensitive
|
|
|
203 |
case 'LIKE':
|
|
|
204 |
$match = $field.'LIKE ';
|
|
|
205 |
break;
|
|
|
206 |
default:
|
|
|
207 |
throw new Doctrine_Expression_Pgsql_Exception('not a supported operator type:'. $operator);
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
$match.= "'";
|
|
|
211 |
foreach ($pattern as $key => $value) {
|
|
|
212 |
if ($key % 2) {
|
|
|
213 |
$match.= $value;
|
|
|
214 |
} else {
|
|
|
215 |
$match.= $this->conn->escapePattern($this->conn->escape($value));
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
$match.= "'";
|
|
|
219 |
$match.= $this->patternEscapeString();
|
|
|
220 |
return $match;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
* return syntax for pgsql TRANSLATE() dbms function
|
|
|
225 |
*
|
|
|
226 |
* @return string $sql
|
|
|
227 |
*/
|
|
|
228 |
public function translate($string, $from, $to)
|
|
|
229 |
{
|
|
|
230 |
$translate = 'TRANSLATE(' . $string . ', ' . $from . ', ' . $to . ')';
|
|
|
231 |
return $translate;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* transform locate to position
|
|
|
236 |
*
|
|
|
237 |
* @param string $substr string to find
|
|
|
238 |
* @param string $str to find where
|
|
|
239 |
* @return string
|
|
|
240 |
*/
|
|
|
241 |
public function locate($substr, $str)
|
|
|
242 |
{
|
|
|
243 |
return $this->position($substr, $str);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* position
|
|
|
248 |
*
|
|
|
249 |
* @param string $substr string to find
|
|
|
250 |
* @param string $str to find where
|
|
|
251 |
* @return string
|
|
|
252 |
*/
|
|
|
253 |
public function position($substr, $str)
|
|
|
254 |
{
|
|
|
255 |
$substr = $this->getIdentifier($substr);
|
|
|
256 |
$str = $this->getIdentifier($str);
|
|
|
257 |
|
|
|
258 |
return sprintf('POSITION(%s IN %s)', $substr, $str);
|
|
|
259 |
}
|
|
|
260 |
}
|