| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Mysql.php 7490 2010-03-29 19:53:27Z 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_Mysql
|
|
|
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: 7490 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Expression_Mysql extends Doctrine_Expression_Driver
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* returns the regular expression operator
|
|
|
37 |
*
|
|
|
38 |
* @return string
|
|
|
39 |
*/
|
|
|
40 |
public function regexp()
|
|
|
41 |
{
|
|
|
42 |
return 'RLIKE';
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* return string to call a function to get random value inside an SQL statement
|
|
|
47 |
*
|
|
|
48 |
* @return string to generate float between 0 and 1
|
|
|
49 |
*/
|
|
|
50 |
public function random()
|
|
|
51 |
{
|
|
|
52 |
return 'RAND()';
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* build a pattern matching string
|
|
|
57 |
*
|
|
|
58 |
* EXPERIMENTAL
|
|
|
59 |
*
|
|
|
60 |
* WARNING: this function is experimental and may change signature at
|
|
|
61 |
* any time until labelled as non-experimental
|
|
|
62 |
*
|
|
|
63 |
* @access public
|
|
|
64 |
*
|
|
|
65 |
* @param array $pattern even keys are strings, odd are patterns (% and _)
|
|
|
66 |
* @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
|
|
|
67 |
* @param string $field optional field name that is being matched against
|
|
|
68 |
* (might be required when emulating ILIKE)
|
|
|
69 |
*
|
|
|
70 |
* @return string SQL pattern
|
|
|
71 |
*/
|
|
|
72 |
public function matchPattern($pattern, $operator = null, $field = null)
|
|
|
73 |
{
|
|
|
74 |
$match = '';
|
|
|
75 |
if ( ! is_null($operator)) {
|
|
|
76 |
$field = is_null($field) ? '' : $field.' ';
|
|
|
77 |
$operator = strtoupper($operator);
|
|
|
78 |
switch ($operator) {
|
|
|
79 |
// case insensitive
|
|
|
80 |
case 'ILIKE':
|
|
|
81 |
$match = $field.'LIKE ';
|
|
|
82 |
break;
|
|
|
83 |
// case sensitive
|
|
|
84 |
case 'LIKE':
|
|
|
85 |
$match = $field.'LIKE BINARY ';
|
|
|
86 |
break;
|
|
|
87 |
default:
|
|
|
88 |
throw new Doctrine_Expression_Mysql_Exception('not a supported operator type:'. $operator);
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
$match.= "'";
|
|
|
92 |
foreach ($pattern as $key => $value) {
|
|
|
93 |
if ($key % 2) {
|
|
|
94 |
$match .= $value;
|
|
|
95 |
} else {
|
|
|
96 |
$match .= $this->conn->escapePattern($this->conn->escape($value));
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
$match.= "'";
|
|
|
100 |
$match.= $this->patternEscapeString();
|
|
|
101 |
return $match;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Returns global unique identifier
|
|
|
106 |
*
|
|
|
107 |
* @return string to get global unique identifier
|
|
|
108 |
*/
|
|
|
109 |
public function guid()
|
|
|
110 |
{
|
|
|
111 |
return 'UUID()';
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Returns the year from dbms
|
|
|
116 |
*
|
|
|
117 |
* @param string $column
|
|
|
118 |
* @return string to get year from dbms
|
|
|
119 |
*/
|
|
|
120 |
public function year($column)
|
|
|
121 |
{
|
|
|
122 |
$column = $this->getIdentifier($column);
|
|
|
123 |
return 'YEAR(' . $column . ')';
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Returns the month from dbms
|
|
|
128 |
*
|
|
|
129 |
* @param string $column
|
|
|
130 |
* @return string to get month from dbms
|
|
|
131 |
*/
|
|
|
132 |
public function month($column)
|
|
|
133 |
{
|
|
|
134 |
$column = $this->getIdentifier($column);
|
|
|
135 |
return 'MONTH(' . $column . ')';
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Returns day from dbms
|
|
|
140 |
*
|
|
|
141 |
* @param string $column
|
|
|
142 |
* @return string to get day from dbms
|
|
|
143 |
*/
|
|
|
144 |
public function day($column)
|
|
|
145 |
{
|
|
|
146 |
$column = $this->getIdentifier($column);
|
|
|
147 |
return 'DAY(' . $column . ')';
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Returns soundex from dbms
|
|
|
152 |
*
|
|
|
153 |
* @param string $column
|
|
|
154 |
* @return string to get soundex from dbms
|
|
|
155 |
*/
|
|
|
156 |
public function soundex($column)
|
|
|
157 |
{
|
|
|
158 |
return 'SOUNDEX(' . $column . ')';
|
|
|
159 |
}
|
|
|
160 |
}
|