# Source for file Check.php

Documentation is available at Check.php

1. <?php
2. /*
3. * $Id: From.php 1080 2007-02-10 18:17:08Z romanb $
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. */
22. /**
23. * Doctrine_Query_Check
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: 1080 $
31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32. */
33. class Doctrine_Query_Check
34. {
35. /**
36. * @var Doctrine_Table $table Doctrine_Table object
37. */
38. protected $table;
39. /**
40. * @var string $sql database specific sql CHECK constraint definition
41. * parsed from the given dql CHECK definition
42. */
43. protected $sql;
44. /**
45. * @param Doctrine_Table|string$table Doctrine_Table object
46. */
47. public function __construct($table)
48. {
49. if ( ! ($table instanceof Doctrine_Table)) {
50. $table = Doctrine_Manager::getInstance()
51. ->getCurrentConnection()
52. ->getTable($table);
53. }
54. $this->table = $table;
55. }
56. /**
57. * getTable
58. * returns the table object associated with this object
59. *
60. * @return Doctrine_Connection
61. */
62. public function getTable()
63. {
64. return $this->table;
65. }
66. /**
67. * parse
68. *
69. * @param string $dql DQL CHECK constraint definition
70. * @return string
71. */
72. public function parse($dql)
73. {
74. $this->sql = $this->parseClause($dql);
75. }
76. /**
77. * parseClause
78. *
79. * @param string $alias component alias
80. * @param string $field the field name
81. * @param mixed $value the value of the field
82. * @return void
83. */
84. public function parseClause($dql)
85. {
86. $parts = Doctrine_Tokenizer::sqlExplode($dql, ' AND ');
88. if (count($parts) > 1) {
89. $ret = array();
90. foreach ($parts as $part) {
91. $ret[] = $this->parseSingle($part);
92. }
94. $r = implode(' AND ', $ret);
95. } else {
96. $parts = Doctrine_Tokenizer::quoteExplode($dql, ' OR ');
97. if (count($parts) > 1) {
98. $ret = array();
99. foreach ($parts as $part) {
100. $ret[] = $this->parseClause($part);
101. }
103. $r = implode(' OR ', $ret);
104. } else {
105. $ret = $this->parseSingle($dql);
106. return $ret;
107. }
108. }
109. return '(' . $r . ')';
110. }
111. public function parseSingle($part)
112. {
113. $e = explode(' ', $part);
115. $e[0] = $this->parseFunction($e[0]);
117. switch ($e[1]) {
118. case '>':
119. case '<':
120. case '=':
121. case '!=':
122. case '<>':
124. break;
125. default:
126. throw new Doctrine_Query_Exception('Unknown operator ' . $e[1]);
127. }
129. return implode(' ', $e);
130. }
131. public function parseFunction($dql)
132. {
133. if (($pos = strpos($dql, '(')) !== false) {
134. $func = substr($dql, 0, $pos);
135. $value = substr($dql, ($pos + 1), -1);
137. $expr = $this->table->getConnection()->expression;
139. if ( ! method_exists($expr, $func)) {
140. throw new Doctrine_Query_Exception('Unknown function ' . $func);
141. }
143. $func = $expr->$func($value);
144. }
145. return $func;
146. }
147. /**
148. * getSql
149. *
150. * returns database specific sql CHECK constraint definition
151. * parsed from the given dql CHECK definition
152. *
153. * @return string
154. */
155. public function getSql()
156. {
157. return $this->sql;
158. }
159. }