NativeQuery.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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;
  20. use function array_values;
  21. use function is_int;
  22. use function key;
  23. use function ksort;
  24. /**
  25. * Represents a native SQL query.
  26. */
  27. final class NativeQuery extends AbstractQuery
  28. {
  29. /** @var string */
  30. private $sql;
  31. /**
  32. * Sets the SQL of the query.
  33. *
  34. * @param string $sql
  35. *
  36. * @return self This query instance.
  37. */
  38. public function setSQL($sql): self
  39. {
  40. $this->sql = $sql;
  41. return $this;
  42. }
  43. /**
  44. * Gets the SQL query.
  45. *
  46. * @return mixed The built SQL query or an array of all SQL queries.
  47. *
  48. * @override
  49. */
  50. public function getSQL()
  51. {
  52. return $this->sql;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function _doExecute()
  58. {
  59. $parameters = [];
  60. $types = [];
  61. foreach ($this->getParameters() as $parameter) {
  62. $name = $parameter->getName();
  63. $value = $this->processParameterValue($parameter->getValue());
  64. $type = $parameter->getValue() === $value
  65. ? $parameter->getType()
  66. : Query\ParameterTypeInferer::inferType($value);
  67. $parameters[$name] = $value;
  68. $types[$name] = $type;
  69. }
  70. if ($parameters && is_int(key($parameters))) {
  71. ksort($parameters);
  72. ksort($types);
  73. $parameters = array_values($parameters);
  74. $types = array_values($types);
  75. }
  76. return $this->_em->getConnection()->executeQuery(
  77. $this->sql,
  78. $parameters,
  79. $types,
  80. $this->_queryCacheProfile
  81. );
  82. }
  83. }