# Source for file Having.php

Documentation is available at Having.php

1. <?php
2. /*
3. * $Id: Having.php 1881 2007-06-27 18:42:47Z 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_Query_Condition');
22. /**
23. * Doctrine_Query_Having
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: 1881 $
31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32. */
33. class Doctrine_Query_Having extends Doctrine_Query_Condition
34. {
35. /**
36. * DQL Aggregate Function parser
37. *
38. * @param string $func
39. * @return mixed
40. */
41. private function parseAggregateFunction($func)
42. {
43. $pos = strpos($func, '(');
45. if ($pos !== false) {
46. $funcs = array();
48. $name = substr($func, 0, $pos);
49. $func = substr($func, ($pos + 1), -1);
50. $params = Doctrine_Tokenizer::bracketExplode($func, ',', '(', ')');
52. foreach ($params as $k => $param) {
53. $params[$k] = $this->parseAggregateFunction($param);
54. }
56. $funcs = $name . '(' . implode(', ', $params) . ')';
58. return $funcs;
60. } else {
61. if ( ! is_numeric($func)) {
62. $a = explode('.', $func);
64. if (count($a) > 1) {
65. $field = array_pop($a);
66. $reference = implode('.', $a);
67. $map = $this->query->load($reference, false);
68. $field = $map['table']->getColumnName($field);
69. $func = $this->query->getTableAlias($reference) . '.' . $field;
70. } else {
71. $field = end($a);
72. $func = $this->query->getAggregateAlias($field);
73. }
74. return $func;
75. } else {
76. return $func;
77. }
78. }
79. }
80. /**
81. * load
82. * returns the parsed query part
83. *
84. * @param string $having
85. * @return string
86. */
87. final public function load($having)
88. {
89. $e = Doctrine_Tokenizer::bracketExplode($having, ' ', '(', ')');
91. $r = array_shift($e);
92. $t = explode('(', $r);
94. $count = count($t);
95. $r = $this->parseAggregateFunction($r);
96. $operator = array_shift($e);
97. $value = implode(' ', $e);
98. $r .= ' ' . $operator . ' ' . $value;
100. return $r;
101. }
102. }