DBLibPDO.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 DBLIB drivers.
  10. * It provides workarounds for improperly implemented functionalities of the DBLIB drivers.
  11. *
  12. * @author Bert Brunekreeft <bbrunekreeft@gmail.com>
  13. * @since 2.0.41
  14. */
  15. class DBLibPDO extends \PDO
  16. {
  17. /**
  18. * Returns value of the last inserted ID.
  19. * @param string|null $name the sequence name. Defaults to null.
  20. * @return int last inserted ID value.
  21. */
  22. public function lastInsertId($name = null)
  23. {
  24. return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn();
  25. }
  26. /**
  27. * Retrieve a database connection attribute.
  28. *
  29. * It is necessary to override PDO's method as some MSSQL PDO driver (e.g. dblib) does not
  30. * support getting attributes.
  31. * @param int $attribute One of the PDO::ATTR_* constants.
  32. * @return mixed A successful call returns the value of the requested PDO attribute.
  33. * An unsuccessful call returns null.
  34. */
  35. public function getAttribute($attribute)
  36. {
  37. try {
  38. return parent::getAttribute($attribute);
  39. } catch (\PDOException $e) {
  40. switch ($attribute) {
  41. case self::ATTR_SERVER_VERSION:
  42. return $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)")->fetchColumn();
  43. default:
  44. throw $e;
  45. }
  46. }
  47. }
  48. }