# Coverage for Doctrine_Expression_Mysql

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Mysql.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.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Expression_Driver');
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.phpdoctrine.org
29 | * @since 1.0
30 | * @version $Revision: 2963 $
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 | }