Guest User

Untitled

a guest
May 21st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 KB | None | 0 0
  1. Index: lib/Doctrine/Query.php
  2. ===================================================================
  3. --- lib/Doctrine/Query.php (revision 4844)
  4. +++ lib/Doctrine/Query.php (working copy)
  5. @@ -1102,6 +1102,10 @@
  6.  
  7. // apply inheritance to WHERE part
  8. if ( ! empty($string)) {
  9. + if (count($this->_sqlParts['where']) > 0) {
  10. + $this->_sqlParts['where'][] = 'AND';
  11. + }
  12. +
  13. if (substr($string, 0, 1) === '(' && substr($string, -1) === ')') {
  14. $this->_sqlParts['where'][] = $string;
  15. } else {
  16. @@ -1132,13 +1136,17 @@
  17.  
  18. // only append the subquery if it actually contains something
  19. if ($subquery !== '') {
  20. + if (count($this->_sqlParts['where']) > 0) {
  21. + array_unshift($this->_sqlParts['where'], 'AND');
  22. + }
  23. +
  24. array_unshift($this->_sqlParts['where'], $this->_conn->quoteIdentifier($field) . ' IN (' . $subquery . ')');
  25. }
  26.  
  27. $modifyLimit = false;
  28. }
  29.  
  30. - $q .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : '';
  31. + $q .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' ', $this->_sqlParts['where']) : '';
  32. $q .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
  33. $q .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']): '';
  34. $q .= ( ! empty($this->_sqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_sqlParts['orderby']) : '';
  35. @@ -1151,11 +1159,23 @@
  36.  
  37. // return to the previous state
  38. if ( ! empty($string)) {
  39. + // We need to double pop if > 2
  40. + if (count($this->_sqlParts['where']) > 2) {
  41. + array_pop($this->_sqlParts['where']);
  42. + }
  43. +
  44. array_pop($this->_sqlParts['where']);
  45. }
  46. +
  47. if ($needsSubQuery) {
  48. + // We need to double shift if > 2
  49. + if (count($this->_sqlParts['where']) > 2) {
  50. + array_shift($this->_sqlParts['where']);
  51. + }
  52. +
  53. array_shift($this->_sqlParts['where']);
  54. }
  55. +
  56. $this->_sql = $q;
  57.  
  58. return $q;
  59. @@ -1240,7 +1260,7 @@
  60. }
  61.  
  62. // all conditions must be preserved in subquery
  63. - $subquery .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_sqlParts['where']) : '';
  64. + $subquery .= ( ! empty($this->_sqlParts['where']))? ' WHERE ' . implode(' ', $this->_sqlParts['where']) : '';
  65. $subquery .= ( ! empty($this->_sqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_sqlParts['groupby']) : '';
  66. $subquery .= ( ! empty($this->_sqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_sqlParts['having']) : '';
  67. $subquery .= ( ! empty($this->_sqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_sqlParts['orderby']) : '';
  68. @@ -1797,11 +1817,15 @@
  69. $string = $this->getInheritanceCondition($this->getRootAlias());
  70.  
  71. if ( ! empty($string)) {
  72. + if (count($where) > 0) {
  73. + $where[] = 'AND';
  74. + }
  75. +
  76. $where[] = $string;
  77. }
  78.  
  79. // append conditions
  80. - $q .= ( ! empty($where)) ? ' WHERE ' . implode(' AND ', $where) : '';
  81. + $q .= ( ! empty($where)) ? ' WHERE ' . implode(' ', $where) : '';
  82.  
  83. if ( ! empty($groupby)) {
  84. // Maintain existing groupby
  85. Index: lib/Doctrine/Query/Abstract.php
  86. ===================================================================
  87. --- lib/Doctrine/Query/Abstract.php (revision 4847)
  88. +++ lib/Doctrine/Query/Abstract.php (working copy)
  89. @@ -338,7 +338,7 @@
  90. $q = '';
  91. $q .= ( ! empty($this->_dqlParts['select']))? 'SELECT ' . implode(', ', $this->_dqlParts['select']) : '';
  92. $q .= ( ! empty($this->_dqlParts['from']))? ' FROM ' . implode(' ', $this->_dqlParts['from']) : '';
  93. - $q .= ( ! empty($this->_dqlParts['where']))? ' WHERE ' . implode(' AND ', $this->_dqlParts['where']) : '';
  94. + $q .= ( ! empty($this->_dqlParts['where']))? ' WHERE ' . implode(' ', $this->_dqlParts['where']) : '';
  95. $q .= ( ! empty($this->_dqlParts['groupby']))? ' GROUP BY ' . implode(', ', $this->_dqlParts['groupby']) : '';
  96. $q .= ( ! empty($this->_dqlParts['having']))? ' HAVING ' . implode(' AND ', $this->_dqlParts['having']) : '';
  97. $q .= ( ! empty($this->_dqlParts['orderby']))? ' ORDER BY ' . implode(', ', $this->_dqlParts['orderby']) : '';
  98. @@ -1190,14 +1190,42 @@
  99. */
  100. public function addWhere($where, $params = array())
  101. {
  102. + return $this->andWhere($where, $params);
  103. + }
  104. +
  105. +
  106. + public function andWhere($where, $params = array())
  107. + {
  108. if (is_array($params)) {
  109. $this->_params['where'] = array_merge($this->_params['where'], $params);
  110. } else {
  111. $this->_params['where'][] = $params;
  112. }
  113. +
  114. + if ($this->_hasDqlQueryPart('where')) {
  115. + $this->_addDqlQueryPart('where', 'AND', true);
  116. + }
  117. +
  118. return $this->_addDqlQueryPart('where', $where, true);
  119. }
  120. +
  121. +
  122. + public function orWhere($where, $params = array())
  123. + {
  124. + if (is_array($params)) {
  125. + $this->_params['where'] = array_merge($this->_params['where'], $params);
  126. + } else {
  127. + $this->_params['where'][] = $params;
  128. + }
  129. +
  130. + if ($this->_hasDqlQueryPart('where')) {
  131. + $this->_addDqlQueryPart('where', 'OR', true);
  132. + }
  133.  
  134. + return $this->_addDqlQueryPart('where', $where, true);
  135. + }
  136. +
  137. +
  138. /**
  139. * whereIn
  140. * adds IN condition to the query WHERE part
  141. @@ -1209,6 +1237,51 @@
  142. */
  143. public function whereIn($expr, $params = array(), $not = false)
  144. {
  145. + return $this->andWhereIn($expr, $params, $not);
  146. + }
  147. +
  148. +
  149. + /**
  150. + * Adds IN condition to the query WHERE part
  151. + *
  152. + * @param string $expr The operand of the IN
  153. + * @param mixed $params An array of parameters or a simple scalar
  154. + * @param boolean $not Whether or not to use NOT in front of IN
  155. + * @return Doctrine_Query
  156. + */
  157. + public function andWhereIn($expr, $params = array(), $not = false)
  158. + {
  159. + if ($this->_hasDqlQueryPart('where')) {
  160. + $this->_addDqlQueryPart('where', 'AND', true);
  161. + }
  162. +
  163. + return $this->_addDqlQueryPart('where', $this->_processWhereIn($expr, $params, $not), true);
  164. + }
  165. +
  166. +
  167. + /**
  168. + * Adds IN condition to the query WHERE part
  169. + *
  170. + * @param string $expr The operand of the IN
  171. + * @param mixed $params An array of parameters or a simple scalar
  172. + * @param boolean $not Whether or not to use NOT in front of IN
  173. + * @return Doctrine_Query
  174. + */
  175. + public function orWhereIn($expr, $params = array(), $not = false)
  176. + {
  177. + if ($this->_hasDqlQueryPart('where')) {
  178. + $this->_addDqlQueryPart('where', 'OR', true);
  179. + }
  180. +
  181. + return $this->_addDqlQueryPart('where', $this->_processWhereIn($expr, $params, $not), true);
  182. + }
  183. +
  184. +
  185. + /**
  186. + * @nodoc
  187. + */
  188. + protected function _processWhereIn($expr, $params = array(), $not = false)
  189. + {
  190. $params = (array) $params;
  191.  
  192. // if there's no params, return (else we'll get a WHERE IN (), invalid SQL)
  193. @@ -1229,11 +1302,10 @@
  194.  
  195. $this->_params['where'] = array_merge($this->_params['where'], $params);
  196.  
  197. - $where = $expr . ($not === true ? ' NOT ':'') . ' IN (' . implode(', ', $a) . ')';
  198. -
  199. - return $this->_addDqlQueryPart('where', $where, true);
  200. + return $expr . ($not === true ? ' NOT ':'') . ' IN (' . implode(', ', $a) . ')';
  201. }
  202.  
  203. +
  204. /**
  205. * whereNotIn
  206. * adds NOT IN condition to the query WHERE part
  207. @@ -1246,8 +1318,34 @@
  208. {
  209. return $this->whereIn($expr, $params, true);
  210. }
  211. +
  212.  
  213. /**
  214. + * Adds NOT IN condition to the query WHERE part
  215. + *
  216. + * @param string $expr The operand of the NOT IN
  217. + * @param mixed $params An array of parameters or a simple scalar
  218. + * @return Doctrine_Query
  219. + */
  220. + public function andWhereNotIn($expr, $params = array())
  221. + {
  222. + return $this->andWhereIn($expr, $params, true);
  223. + }
  224. +
  225. +
  226. + /**
  227. + * Adds NOT IN condition to the query WHERE part
  228. + *
  229. + * @param string $expr The operand of the NOT IN
  230. + * @param mixed $params An array of parameters or a simple scalar
  231. + * @return Doctrine_Query
  232. + */
  233. + public function orWhereNotIn($expr, $params = array())
  234. + {
  235. + return $this->orWhereIn($expr, $params, true);
  236. + }
  237. +
  238. + /**
  239. * addGroupBy
  240. * adds fields to the GROUP BY part of the query
  241. *
  242. @@ -1450,6 +1548,7 @@
  243. public function where($where, $params = array())
  244. {
  245. $this->_params['where'] = array();
  246. +
  247. if (is_array($params)) {
  248. $this->_params['where'] = $params;
  249. } else {
  250. @@ -1813,7 +1912,19 @@
  251. {
  252. return $this->_conn;
  253. }
  254. +
  255. + /**
  256. + * Checks if there's at least one DQL part defined to the internal parts collection.
  257. + *
  258. + * @param string $queryPartName The name of the query part.
  259. + * @return boolean
  260. + */
  261. + protected function _hasDqlQueryPart($queryPartName)
  262. + {
  263. + return count($this->_dqlParts[$queryPartName]) > 0;
  264. + }
  265.  
  266. +
  267. /**
  268. * Adds a DQL part to the internal parts collection.
  269. *
  270. Index: tests/QueryTestCase.php
  271. ===================================================================
  272. --- tests/QueryTestCase.php (revision 4844)
  273. +++ tests/QueryTestCase.php (working copy)
  274. @@ -175,6 +175,18 @@
  275. $this->pass();
  276. }
  277. }
  278. +
  279. +
  280. + public function testOrQuerySupport()
  281. + {
  282. + $q = Doctrine_Query::create()
  283. + ->from('User u')
  284. + ->leftJoin('u.Phonenumber p')
  285. + ->where('u.name = ?')
  286. + ->orWhere('u.loginname = ?');
  287. +
  288. + $this->assertEqual($q->getSqlQuery(), '');
  289. + }
  290. }
  291.  
  292. class MyQuery extends Doctrine_Query
Add Comment
Please, Sign In to add comment