# Source for file Mysql.php

Documentation is available at Mysql.php

1. <?php
2. /*
3. * $Id: Mysql.php 1917 2007-07-01 11:27:45Z zYne $
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_Mysql
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision: 1917 $
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. * return string to call a function to get random value inside an SQL statement
46. *
47. * @return string to generate float between 0 and 1
48. */
49. public function random()
50. {
51. return 'RAND()';
52. }
53. /**
54. * build a pattern matching string
55. *
56. * EXPERIMENTAL
57. *
58. * WARNING: this function is experimental and may change signature at
59. * any time until labelled as non-experimental
60. *
61. * @access public
62. *
63. * @param array $pattern even keys are strings, odd are patterns (% and _)
64. * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
65. * @param string $field optional field name that is being matched against
66. * (might be required when emulating ILIKE)
67. *
68. * @return string SQL pattern
69. */
70. public function matchPattern($pattern, $operator = null, $field = null)
71. {
72. $match = '';
73. if (!is_null($operator)) {
74. $field = is_null($field) ? '' : $field.' ';
75. $operator = strtoupper($operator);
76. switch ($operator) {
77. // case insensitive
78. case 'ILIKE':
79. $match = $field.'LIKE ';
80. break;
81. // case sensitive
82. case 'LIKE':
83. $match = $field.'LIKE BINARY ';
84. break;
85. default:
86. throw new Doctrine_Expression_Mysql_Exception('not a supported operator type:'. $operator);
87. }
88. }
89. $match.= "'";
90. foreach ($pattern as $key => $value) {
91. if ($key % 2) {
92. $match .= $value;
93. } else {
94. $match .= $this->conn->escapePattern($this->conn->escape($value));
95. }
96. }
97. $match.= "'";
98. $match.= $this->patternEscapeString();
99. return $match;
100. }
101. /**
102. * Returns global unique identifier
103. *
104. * @return string to get global unique identifier
105. */
106. public function guid()
107. {
108. return 'UUID()';
109. }
110. }