/servers/urlcatcher.org/htdocs/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Condition.php

https://github.com/bigcalm/urlcatcher · PHP · 138 lines · 70 code · 14 blank · 54 comment · 28 complexity · 57cecbf805b1f6483d2c877196aceb6d MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: Condition.php 6366 2009-09-15 19:44:05Z 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.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Query_Condition
  23. *
  24. * @package Doctrine
  25. * @subpackage Query
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.phpdoctrine.org
  28. * @since 1.0
  29. * @version $Revision: 6366 $
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. */
  32. abstract class Doctrine_Query_Condition extends Doctrine_Query_Part
  33. {
  34. /**
  35. * DQL CONDITION PARSER
  36. * parses the join condition/where/having part of the query string
  37. *
  38. * @param string $str
  39. * @return string
  40. */
  41. public function parse($str)
  42. {
  43. $tmp = trim($str);
  44. $parts = $this->_tokenizer->bracketExplode($str, array(' OR '), '(', ')');
  45. if (count($parts) > 1) {
  46. $ret = array();
  47. foreach ($parts as $part) {
  48. $part = $this->_tokenizer->bracketTrim($part, '(', ')');
  49. $ret[] = $this->parse($part);
  50. }
  51. $r = implode(' OR ', $ret);
  52. } else {
  53. $parts = $this->_tokenizer->bracketExplode($str, array(' AND '), '(', ')');
  54. // Ticket #1388: We need to make sure we're not splitting a BETWEEN ... AND ... clause
  55. $tmp = array();
  56. for ($i = 0, $l = count($parts); $i < $l; $i++) {
  57. $test = $this->_tokenizer->sqlExplode($parts[$i]);
  58. if (count($test) == 3 && strtoupper($test[1]) == 'BETWEEN') {
  59. $tmp[] = $parts[$i] . ' AND ' . $parts[++$i];
  60. } else if (count($test) == 4 && strtoupper($test[1]) == 'NOT' && strtoupper($test[2]) == 'BETWEEN') {
  61. $tmp[] = $parts[$i] . ' AND ' . $parts[++$i];
  62. } else {
  63. $tmp[] = $parts[$i];
  64. }
  65. }
  66. $parts = $tmp;
  67. unset($tmp);
  68. if (count($parts) > 1) {
  69. $ret = array();
  70. foreach ($parts as $part) {
  71. $part = $this->_tokenizer->bracketTrim($part, '(', ')');
  72. $ret[] = $this->parse($part);
  73. }
  74. $r = implode(' AND ', $ret);
  75. } else {
  76. // Fix for #710
  77. if (substr($parts[0],0,1) == '(' && substr($parts[0], -1) == ')') {
  78. return $this->parse(substr($parts[0], 1, -1));
  79. } else {
  80. // Processing NOT here
  81. if (strtoupper(substr($parts[0], 0, 4)) === 'NOT ') {
  82. $r = 'NOT ('.$this->parse(substr($parts[0], 4)).')';
  83. } else {
  84. return $this->load($parts[0]);
  85. }
  86. }
  87. }
  88. }
  89. return '(' . $r . ')';
  90. }
  91. /**
  92. * parses a literal value and returns the parsed value
  93. *
  94. * boolean literals are parsed to integers
  95. * components are parsed to associated table aliases
  96. *
  97. * @param string $value literal value to be parsed
  98. * @return string
  99. */
  100. public function parseLiteralValue($value)
  101. {
  102. // check that value isn't a string
  103. if (strpos($value, '\'') === false) {
  104. // parse booleans
  105. $value = $this->query->getConnection()
  106. ->dataDict->parseBoolean($value);
  107. $a = explode('.', $value);
  108. if (count($a) > 1) {
  109. // either a float or a component..
  110. if ( ! is_numeric($a[0])) {
  111. // a component found
  112. $field = array_pop($a);
  113. $reference = implode('.', $a);
  114. $value = $this->query->getConnection()->quoteIdentifier(
  115. $this->query->getSqlTableAlias($reference). '.' . $field
  116. );
  117. }
  118. }
  119. } else {
  120. // string literal found
  121. }
  122. return $value;
  123. }
  124. }