NativeQuery.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 LGPL. 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. $stmt = $this->_em->getConnection()->prepare($this->_sql);
  56. $params = $this->_params;
  57. foreach ($params as $key => $value) {
  58. if (isset($this->_paramTypes[$key])) {
  59. $stmt->bindValue($key, $value, $this->_paramTypes[$key]);
  60. } else {
  61. $stmt->bindValue($key, $value);
  62. }
  63. }
  64. $stmt->execute();
  65. return $stmt;
  66. }
  67. }