1 |
<?php
|
2 |
/*
|
3 |
* $Id: Pgsql.php 2702 2007-10-03 21:43:22Z 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: 2702 $
|
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 |
* Returns the SQL string to return the current system date and time.
|
148 |
*
|
149 |
* @return string
|
150 |
*/
|
151 |
public function now()
|
152 |
{
|
153 |
return 'LOCALTIMESTAMP(0)';
|
154 |
}
|
155 |
|
156 |
/**
|
157 |
* regexp
|
158 |
*
|
159 |
* @return string the regular expression operator
|
160 |
*/
|
161 |
public function regexp()
|
162 |
{
|
163 |
return 'SIMILAR TO';
|
164 |
}
|
165 |
|
166 |
/**
|
167 |
* return string to call a function to get random value inside an SQL statement
|
168 |
*
|
169 |
* @return return string to generate float between 0 and 1
|
170 |
* @access public
|
171 |
*/
|
172 |
public function random()
|
173 |
{
|
174 |
return 'RANDOM()';
|
175 |
}
|
176 |
/**
|
177 |
* build a pattern matching string
|
178 |
*
|
179 |
* EXPERIMENTAL
|
180 |
*
|
181 |
* WARNING: this function is experimental and may change signature at
|
182 |
* any time until labelled as non-experimental
|
183 |
*
|
184 |
* @access public
|
185 |
*
|
186 |
* @param array $pattern even keys are strings, odd are patterns (% and _)
|
187 |
* @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
|
188 |
* @param string $field optional field name that is being matched against
|
189 |
* (might be required when emulating ILIKE)
|
190 |
*
|
191 |
* @return string SQL pattern
|
192 |
*/
|
193 |
public function matchPattern($pattern, $operator = null, $field = null)
|
194 |
{
|
195 |
$match = '';
|
196 |
if ( ! is_null($operator)) {
|
197 |
$field = is_null($field) ? '' : $field.' ';
|
198 |
$operator = strtoupper($operator);
|
199 |
switch ($operator) {
|
200 |
// case insensitive
|
201 |
case 'ILIKE':
|
202 |
$match = $field.'ILIKE ';
|
203 |
break;
|
204 |
// case sensitive
|
205 |
case 'LIKE':
|
206 |
$match = $field.'LIKE ';
|
207 |
break;
|
208 |
default:
|
209 |
throw new Doctrine_Expression_Pgsql_Exception('not a supported operator type:'. $operator);
|
210 |
}
|
211 |
}
|
212 |
$match.= "'";
|
213 |
foreach ($pattern as $key => $value) {
|
214 |
if ($key % 2) {
|
215 |
$match.= $value;
|
216 |
} else {
|
217 |
$match.= $this->conn->escapePattern($this->conn->escape($value));
|
218 |
}
|
219 |
}
|
220 |
$match.= "'";
|
221 |
$match.= $this->patternEscapeString();
|
222 |
return $match;
|
223 |
}
|
224 |
} |