QueryException.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query;
  20. use Doctrine\ORM\ORMException;
  21. use Doctrine\ORM\Query\AST\PathExpression;
  22. use Exception;
  23. /**
  24. * Description of QueryException.
  25. *
  26. * @link www.doctrine-project.org
  27. */
  28. class QueryException extends ORMException
  29. {
  30. /**
  31. * @param string $dql
  32. *
  33. * @return QueryException
  34. */
  35. public static function dqlError($dql)
  36. {
  37. return new self($dql);
  38. }
  39. /**
  40. * @param string $message
  41. * @param Exception|null $previous
  42. *
  43. * @return QueryException
  44. */
  45. public static function syntaxError($message, $previous = null)
  46. {
  47. return new self('[Syntax Error] ' . $message, 0, $previous);
  48. }
  49. /**
  50. * @param string $message
  51. * @param Exception|null $previous
  52. *
  53. * @return QueryException
  54. */
  55. public static function semanticalError($message, $previous = null)
  56. {
  57. return new self('[Semantical Error] ' . $message, 0, $previous);
  58. }
  59. /**
  60. * @return QueryException
  61. */
  62. public static function invalidLockMode()
  63. {
  64. return new self('Invalid lock mode hint provided.');
  65. }
  66. /**
  67. * @param string $expected
  68. * @param string $received
  69. *
  70. * @return QueryException
  71. */
  72. public static function invalidParameterType($expected, $received)
  73. {
  74. return new self('Invalid parameter type, ' . $received . ' given, but ' . $expected . ' expected.');
  75. }
  76. /**
  77. * @param string $pos
  78. *
  79. * @return QueryException
  80. */
  81. public static function invalidParameterPosition($pos)
  82. {
  83. return new self('Invalid parameter position: ' . $pos);
  84. }
  85. /**
  86. * @param int $expected
  87. * @param int $received
  88. *
  89. * @return QueryException
  90. */
  91. public static function tooManyParameters($expected, $received)
  92. {
  93. return new self('Too many parameters: the query defines ' . $expected . ' parameters and you bound ' . $received);
  94. }
  95. /**
  96. * @param int $expected
  97. * @param int $received
  98. *
  99. * @return QueryException
  100. */
  101. public static function tooFewParameters($expected, $received)
  102. {
  103. return new self('Too few parameters: the query defines ' . $expected . ' parameters but you only bound ' . $received);
  104. }
  105. /**
  106. * @param string $value
  107. *
  108. * @return QueryException
  109. */
  110. public static function invalidParameterFormat($value)
  111. {
  112. return new self('Invalid parameter format, ' . $value . ' given, but :<name> or ?<num> expected.');
  113. }
  114. /**
  115. * @param string $key
  116. *
  117. * @return QueryException
  118. */
  119. public static function unknownParameter($key)
  120. {
  121. return new self('Invalid parameter: token ' . $key . ' is not defined in the query.');
  122. }
  123. /**
  124. * @return QueryException
  125. */
  126. public static function parameterTypeMismatch()
  127. {
  128. return new self('DQL Query parameter and type numbers mismatch, but have to be exactly equal.');
  129. }
  130. /**
  131. * @param object $pathExpr
  132. *
  133. * @return QueryException
  134. */
  135. public static function invalidPathExpression($pathExpr)
  136. {
  137. return new self(
  138. "Invalid PathExpression '" . $pathExpr->identificationVariable . '.' . $pathExpr->field . "'."
  139. );
  140. }
  141. /**
  142. * @param string $literal
  143. *
  144. * @return QueryException
  145. */
  146. public static function invalidLiteral($literal)
  147. {
  148. return new self("Invalid literal '" . $literal . "'");
  149. }
  150. /**
  151. * @return QueryException
  152. *
  153. * @psalm-param array<string, string> $assoc
  154. */
  155. public static function iterateWithFetchJoinCollectionNotAllowed($assoc)
  156. {
  157. return new self(
  158. 'Invalid query operation: Not allowed to iterate over fetch join collections ' .
  159. 'in class ' . $assoc['sourceEntity'] . ' association ' . $assoc['fieldName']
  160. );
  161. }
  162. /**
  163. * @return QueryException
  164. */
  165. public static function partialObjectsAreDangerous()
  166. {
  167. return new self(
  168. 'Loading partial objects is dangerous. Fetch full objects or consider ' .
  169. 'using a different fetch mode. If you really want partial objects, ' .
  170. 'set the doctrine.forcePartialLoad query hint to TRUE.'
  171. );
  172. }
  173. /**
  174. * @return QueryException
  175. *
  176. * @psalm-param array<string, string> $assoc
  177. */
  178. public static function overwritingJoinConditionsNotYetSupported($assoc)
  179. {
  180. return new self(
  181. 'Unsupported query operation: It is not yet possible to overwrite the join ' .
  182. 'conditions in class ' . $assoc['sourceEntityName'] . ' association ' . $assoc['fieldName'] . '. ' .
  183. 'Use WITH to append additional join conditions to the association.'
  184. );
  185. }
  186. /**
  187. * @return QueryException
  188. */
  189. public static function associationPathInverseSideNotSupported(PathExpression $pathExpr)
  190. {
  191. return new self(
  192. 'A single-valued association path expression to an inverse side is not supported in DQL queries. ' .
  193. 'Instead of "' . $pathExpr->identificationVariable . '.' . $pathExpr->field . '" use an explicit join.'
  194. );
  195. }
  196. /**
  197. * @return QueryException
  198. *
  199. * @psalm-param array<string, string> $assoc
  200. */
  201. public static function iterateWithFetchJoinNotAllowed($assoc)
  202. {
  203. return new self(
  204. 'Iterate with fetch join in class ' . $assoc['sourceEntity'] .
  205. ' using association ' . $assoc['fieldName'] . ' not allowed.'
  206. );
  207. }
  208. public static function iterateWithMixedResultNotAllowed(): QueryException
  209. {
  210. return new self('Iterating a query with mixed results (using scalars) is not supported.');
  211. }
  212. /**
  213. * @return QueryException
  214. */
  215. public static function associationPathCompositeKeyNotSupported()
  216. {
  217. return new self(
  218. 'A single-valued association path expression to an entity with a composite primary ' .
  219. 'key is not supported. Explicitly name the components of the composite primary key ' .
  220. 'in the query.'
  221. );
  222. }
  223. /**
  224. * @param string $className
  225. * @param string $rootClass
  226. *
  227. * @return QueryException
  228. */
  229. public static function instanceOfUnrelatedClass($className, $rootClass)
  230. {
  231. return new self("Cannot check if a child of '" . $rootClass . "' is instanceof '" . $className . "', " .
  232. 'inheritance hierarchy does not exists between these two classes.');
  233. }
  234. /**
  235. * @param string $dqlAlias
  236. *
  237. * @return QueryException
  238. */
  239. public static function invalidQueryComponent($dqlAlias)
  240. {
  241. return new self(
  242. "Invalid query component given for DQL alias '" . $dqlAlias . "', " .
  243. "requires 'metadata', 'parent', 'relation', 'map', 'nestingLevel' and 'token' keys."
  244. );
  245. }
  246. }