AbstractSQLServerDriver.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Driver;
  5. use Doctrine\DBAL\Driver\DriverException as TheDriverException;
  6. use Doctrine\DBAL\Exception;
  7. use Doctrine\DBAL\Exception\DriverException;
  8. use Doctrine\DBAL\Platforms\SQLServer2005Platform;
  9. use Doctrine\DBAL\Platforms\SQLServer2008Platform;
  10. use Doctrine\DBAL\Platforms\SQLServer2012Platform;
  11. use Doctrine\DBAL\Platforms\SQLServerPlatform;
  12. use Doctrine\DBAL\Schema\SQLServerSchemaManager;
  13. use Doctrine\DBAL\VersionAwarePlatformDriver;
  14. use function assert;
  15. use function preg_match;
  16. use function version_compare;
  17. /**
  18. * Abstract base implementation of the {@link Driver} interface for Microsoft SQL Server based drivers.
  19. */
  20. abstract class AbstractSQLServerDriver implements Driver, VersionAwarePlatformDriver
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function createDatabasePlatformForVersion($version)
  26. {
  27. if (
  28. ! preg_match(
  29. '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
  30. $version,
  31. $versionParts
  32. )
  33. ) {
  34. throw Exception::invalidPlatformVersionSpecified(
  35. $version,
  36. '<major_version>.<minor_version>.<patch_version>.<build_version>'
  37. );
  38. }
  39. $majorVersion = $versionParts['major'];
  40. $minorVersion = $versionParts['minor'] ?? 0;
  41. $patchVersion = $versionParts['patch'] ?? 0;
  42. $buildVersion = $versionParts['build'] ?? 0;
  43. $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
  44. switch (true) {
  45. case version_compare($version, '11.00.2100', '>='):
  46. return new SQLServer2012Platform();
  47. case version_compare($version, '10.00.1600', '>='):
  48. return new SQLServer2008Platform();
  49. case version_compare($version, '9.00.1399', '>='):
  50. return new SQLServer2005Platform();
  51. default:
  52. return new SQLServerPlatform();
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @deprecated Use Connection::getDatabase() instead.
  59. */
  60. public function getDatabase(Connection $conn)
  61. {
  62. $params = $conn->getParams();
  63. if (isset($params['dbname'])) {
  64. return $params['dbname'];
  65. }
  66. $database = $conn->query('SELECT DB_NAME()')->fetchColumn();
  67. assert($database !== false);
  68. return $database;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getDatabasePlatform()
  74. {
  75. return new SQLServer2008Platform();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getSchemaManager(Connection $conn)
  81. {
  82. return new SQLServerSchemaManager($conn);
  83. }
  84. /**
  85. * @param string $message
  86. *
  87. * @return DriverException
  88. */
  89. public function convertException($message, TheDriverException $exception)
  90. {
  91. return new DriverException($message, $exception);
  92. }
  93. }