Connection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Doctrine\DBAL\Portability;
  3. use Doctrine\DBAL\Cache\QueryCacheProfile;
  4. use Doctrine\DBAL\ColumnCase;
  5. use Doctrine\DBAL\Connection as BaseConnection;
  6. use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
  7. use Doctrine\DBAL\ForwardCompatibility;
  8. use PDO;
  9. use function func_get_args;
  10. use const CASE_LOWER;
  11. use const CASE_UPPER;
  12. /**
  13. * Portability wrapper for a Connection.
  14. */
  15. class Connection extends BaseConnection
  16. {
  17. public const PORTABILITY_ALL = 255;
  18. public const PORTABILITY_NONE = 0;
  19. public const PORTABILITY_RTRIM = 1;
  20. public const PORTABILITY_EMPTY_TO_NULL = 4;
  21. public const PORTABILITY_FIX_CASE = 8;
  22. /**#@+
  23. *
  24. * @deprecated Will be removed as internal implementation details.
  25. */
  26. public const PORTABILITY_DB2 = 13;
  27. public const PORTABILITY_ORACLE = 9;
  28. public const PORTABILITY_POSTGRESQL = 13;
  29. public const PORTABILITY_SQLITE = 13;
  30. public const PORTABILITY_OTHERVENDORS = 12;
  31. public const PORTABILITY_DRIZZLE = 13;
  32. public const PORTABILITY_SQLANYWHERE = 13;
  33. public const PORTABILITY_SQLSRV = 13;
  34. /**#@-*/
  35. /** @var int */
  36. private $portability = self::PORTABILITY_NONE;
  37. /** @var int|null */
  38. private $case;
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function connect()
  43. {
  44. $ret = parent::connect();
  45. if ($ret) {
  46. $params = $this->getParams();
  47. if (isset($params['portability'])) {
  48. $this->portability = $params['portability'] = (new OptimizeFlags())(
  49. $this->getDatabasePlatform(),
  50. $params['portability']
  51. );
  52. }
  53. if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) {
  54. if ($this->_conn instanceof PDOConnection) {
  55. // make use of c-level support for case handling
  56. $this->_conn->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
  57. } else {
  58. $this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
  59. }
  60. }
  61. }
  62. return $ret;
  63. }
  64. /**
  65. * @return int
  66. */
  67. public function getPortability()
  68. {
  69. return $this->portability;
  70. }
  71. /**
  72. * @return int|null
  73. */
  74. public function getFetchCase()
  75. {
  76. return $this->case;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null)
  82. {
  83. $stmt = new Statement(parent::executeQuery($sql, $params, $types, $qcp), $this);
  84. $stmt->setFetchMode($this->defaultFetchMode);
  85. return new ForwardCompatibility\Result($stmt);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. *
  90. * @param string $sql
  91. *
  92. * @return Statement
  93. */
  94. public function prepare($sql)
  95. {
  96. $stmt = new Statement(parent::prepare($sql), $this);
  97. $stmt->setFetchMode($this->defaultFetchMode);
  98. return $stmt;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function query()
  104. {
  105. $connection = $this->getWrappedConnection();
  106. $stmt = $connection->query(...func_get_args());
  107. $stmt = new Statement($stmt, $this);
  108. $stmt->setFetchMode($this->defaultFetchMode);
  109. return $stmt;
  110. }
  111. }