Connection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\DBAL\Portability;
  20. use Doctrine\DBAL\Driver;
  21. use Doctrine\DBAL\Cache\QueryCacheProfile;
  22. /**
  23. * Portability wrapper for a Connection.
  24. *
  25. * @link www.doctrine-project.org
  26. * @since 2.0
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. */
  29. class Connection extends \Doctrine\DBAL\Connection
  30. {
  31. const PORTABILITY_ALL = 255;
  32. const PORTABILITY_NONE = 0;
  33. const PORTABILITY_RTRIM = 1;
  34. const PORTABILITY_EMPTY_TO_NULL = 4;
  35. const PORTABILITY_FIX_CASE = 8;
  36. const PORTABILITY_ORACLE = 9;
  37. const PORTABILITY_POSTGRESQL = 13;
  38. const PORTABILITY_SQLITE = 13;
  39. const PORTABILITY_OTHERVENDORS = 12;
  40. const PORTABILITY_DRIZZLE = 13;
  41. const PORTABILITY_SQLSRV = 13;
  42. /**
  43. * @var integer
  44. */
  45. private $portability = self::PORTABILITY_NONE;
  46. /**
  47. * @var integer
  48. */
  49. private $case;
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function connect()
  54. {
  55. $ret = parent::connect();
  56. if ($ret) {
  57. $params = $this->getParams();
  58. if (isset($params['portability'])) {
  59. if ($this->_platform->getName() === "oracle") {
  60. $params['portability'] = $params['portability'] & self::PORTABILITY_ORACLE;
  61. } else if ($this->_platform->getName() === "postgresql") {
  62. $params['portability'] = $params['portability'] & self::PORTABILITY_POSTGRESQL;
  63. } else if ($this->_platform->getName() === "sqlite") {
  64. $params['portability'] = $params['portability'] & self::PORTABILITY_SQLITE;
  65. } else if ($this->_platform->getName() === "drizzle") {
  66. $params['portability'] = self::PORTABILITY_DRIZZLE;
  67. } else if ($this->_platform->getName() === 'sqlsrv') {
  68. $params['portability'] = $params['portabililty'] & self::PORTABILITY_SQLSRV;
  69. } else {
  70. $params['portability'] = $params['portability'] & self::PORTABILITY_OTHERVENDORS;
  71. }
  72. $this->portability = $params['portability'];
  73. }
  74. if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) {
  75. if ($this->_conn instanceof \Doctrine\DBAL\Driver\PDOConnection) {
  76. // make use of c-level support for case handling
  77. $this->_conn->setAttribute(\PDO::ATTR_CASE, $params['fetch_case']);
  78. } else {
  79. $this->case = ($params['fetch_case'] == \PDO::CASE_LOWER) ? CASE_LOWER : CASE_UPPER;
  80. }
  81. }
  82. }
  83. return $ret;
  84. }
  85. /**
  86. * @return integer
  87. */
  88. public function getPortability()
  89. {
  90. return $this->portability;
  91. }
  92. /**
  93. * @return integer
  94. */
  95. public function getFetchCase()
  96. {
  97. return $this->case;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
  103. {
  104. $stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
  105. $stmt->setFetchMode($this->defaultFetchMode);
  106. return $stmt;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function prepare($statement)
  112. {
  113. $stmt = new Statement(parent::prepare($statement), $this);
  114. $stmt->setFetchMode($this->defaultFetchMode);
  115. return $stmt;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function query()
  121. {
  122. $this->connect();
  123. $stmt = call_user_func_array(array($this->_conn, 'query'), func_get_args());
  124. $stmt = new Statement($stmt, $this);
  125. $stmt->setFetchMode($this->defaultFetchMode);
  126. return $stmt;
  127. }
  128. }