NativeQuery.php 2.6 KB

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