PDO.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\mssql;
  8. /**
  9. * This is an extension of the default PDO class of MSSQL and DBLIB drivers.
  10. * It provides workarounds for improperly implemented functionalities of the MSSQL and DBLIB drivers.
  11. *
  12. * @author Timur Ruziev <resurtm@gmail.com>
  13. * @since 2.0
  14. */
  15. class PDO extends \PDO
  16. {
  17. /**
  18. * Returns value of the last inserted ID.
  19. * @param string|null $sequence the sequence name. Defaults to null.
  20. * @return int last inserted ID value.
  21. */
  22. public function lastInsertId($sequence = null)
  23. {
  24. return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn();
  25. }
  26. /**
  27. * Starts a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not
  28. * natively support transactions.
  29. * @return bool the result of a transaction start.
  30. */
  31. public function beginTransaction()
  32. {
  33. $this->exec('BEGIN TRANSACTION');
  34. return true;
  35. }
  36. /**
  37. * Commits a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not
  38. * natively support transactions.
  39. * @return bool the result of a transaction commit.
  40. */
  41. public function commit()
  42. {
  43. $this->exec('COMMIT TRANSACTION');
  44. return true;
  45. }
  46. /**
  47. * Rollbacks a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not
  48. * natively support transactions.
  49. * @return bool the result of a transaction roll back.
  50. */
  51. public function rollBack()
  52. {
  53. $this->exec('ROLLBACK TRANSACTION');
  54. return true;
  55. }
  56. /**
  57. * Retrieve a database connection attribute.
  58. *
  59. * It is necessary to override PDO's method as some MSSQL PDO driver (e.g. dblib) does not
  60. * support getting attributes.
  61. * @param int $attribute One of the PDO::ATTR_* constants.
  62. * @return mixed A successful call returns the value of the requested PDO attribute.
  63. * An unsuccessful call returns null.
  64. */
  65. public function getAttribute($attribute)
  66. {
  67. try {
  68. return parent::getAttribute($attribute);
  69. } catch (\PDOException $e) {
  70. switch ($attribute) {
  71. case self::ATTR_SERVER_VERSION:
  72. return $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)")->fetchColumn();
  73. default:
  74. throw $e;
  75. }
  76. }
  77. }
  78. }