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.com>.
|
20 |
*/
|
21 |
Doctrine::autoload('Doctrine_Expression_Driver');
|
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.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 2963 $
|
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 |
if ($this->version > 7) {
|
58 |
return 'MD5(' . $column . ')';
|
59 |
} else {
|
60 |
return 'encode(digest(' . $column .', md5), hex)';
|
61 |
}
|
62 |
}
|
63 |
|
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);
|
77 |
|
78 |
if ($len === null) {
|
79 |
$len = $this->getIdentifier($len);
|
80 |
return 'SUBSTR(' . $value . ', ' . $from . ')';
|
81 |
} else {
|
82 |
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
|
83 |
}
|
84 |
}
|
85 |
|
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 |
*/
|
95 |
|
96 |
|
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 |
}
|
110 |
|
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 |
}
|
121 |
|
122 |
|
123 |
/**
|
124 |
* PostgreSQLs TO_CHAR( <time>, <text> ) function.
|
125 |
*
|
126 |
* @param string $time timestamp or interval
|
127 |
* @param string $text how to the format the output
|
128 |
* @return string
|
129 |
*/
|
130 |
public function to_char($time, $text) {
|
131 |
return 'TO_CHAR(' . $time . ', ' . $text . ')';
|
132 |
}
|
133 |
|
134 |
/**
|
135 |
* PostgreSQLs CONCAT() function
|
136 |
*
|
137 |
* @param an array of values
|
138 |
* @return string
|
139 |
*/
|
140 |
public function concat()
|
141 |
{
|
142 |
$args = func_get_args();
|
143 |
|
144 |
return join(' || ' , $args);
|
145 |
}
|
146 |
|
147 |
/**
|
148 |
* Returns the SQL string to return the current system date and time.
|
149 |
*
|
150 |
* @return string
|
151 |
*/
|
152 |
public function now()
|
153 |
{
|
154 |
return 'LOCALTIMESTAMP(0)';
|
155 |
}
|
156 |
|
157 |
/**
|
158 |
* regexp
|
159 |
*
|
160 |
* @return string the regular expression operator
|
161 |
*/
|
162 |
public function regexp()
|
163 |
{
|
164 |
return 'SIMILAR TO';
|
165 |
}
|
166 |
|
167 |
/**
|
168 |
* return string to call a function to get random value inside an SQL statement
|
169 |
*
|
170 |
* @return return string to generate float between 0 and 1
|
171 |
* @access public
|
172 |
*/
|
173 |
public function random()
|
174 |
{
|
175 |
return 'RANDOM()';
|
176 |
}
|
177 |
|
178 |
/**
|
179 |
* build a pattern matching string
|
180 |
*
|
181 |
* EXPERIMENTAL
|
182 |
*
|
183 |
* WARNING: this function is experimental and may change signature at
|
184 |
* any time until labelled as non-experimental
|
185 |
*
|
186 |
* @access public
|
187 |
*
|
188 |
* @param array $pattern even keys are strings, odd are patterns (% and _)
|
189 |
* @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
|
190 |
* @param string $field optional field name that is being matched against
|
191 |
* (might be required when emulating ILIKE)
|
192 |
*
|
193 |
* @return string SQL pattern
|
194 |
*/
|
195 |
public function matchPattern($pattern, $operator = null, $field = null)
|
196 |
{
|
197 |
$match = '';
|
198 |
if ( ! is_null($operator)) {
|
199 |
$field = is_null($field) ? '' : $field.' ';
|
200 |
$operator = strtoupper($operator);
|
201 |
switch ($operator) {
|
202 |
// case insensitive
|
203 |
case 'ILIKE':
|
204 |
$match = $field.'ILIKE ';
|
205 |
break;
|
206 |
// case sensitive
|
207 |
case 'LIKE':
|
208 |
$match = $field.'LIKE ';
|
209 |
break;
|
210 |
default:
|
211 |
throw new Doctrine_Expression_Pgsql_Exception('not a supported operator type:'. $operator);
|
212 |
}
|
213 |
}
|
214 |
$match.= "'";
|
215 |
foreach ($pattern as $key => $value) {
|
216 |
if ($key % 2) {
|
217 |
$match.= $value;
|
218 |
} else {
|
219 |
$match.= $this->conn->escapePattern($this->conn->escape($value));
|
220 |
}
|
221 |
}
|
222 |
$match.= "'";
|
223 |
$match.= $this->patternEscapeString();
|
224 |
return $match;
|
225 |
}
|
226 |
} |