NativeQuery.php 2.5KB

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