Connection.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Doctrine\DBAL\Driver\PDOSqlsrv;
  3. use Doctrine\DBAL\Driver\PDOConnection;
  4. use Doctrine\DBAL\ParameterType;
  5. use PDO;
  6. use function strpos;
  7. use function substr;
  8. /**
  9. * Sqlsrv Connection implementation.
  10. */
  11. class Connection extends PDOConnection
  12. {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function __construct($dsn, $user = null, $password = null, ?array $options = null)
  17. {
  18. parent::__construct($dsn, $user, $password, $options);
  19. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class, []]);
  20. }
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function lastInsertId($name = null)
  25. {
  26. if ($name === null) {
  27. return parent::lastInsertId($name);
  28. }
  29. $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
  30. $stmt->execute([$name]);
  31. return $stmt->fetchColumn();
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function quote($value, $type = ParameterType::STRING)
  37. {
  38. $val = parent::quote($value, $type);
  39. // Fix for a driver version terminating all values with null byte
  40. if (strpos($val, "\0") !== false) {
  41. $val = substr($val, 0, -1);
  42. }
  43. return $val;
  44. }
  45. }