AbstractSQLServerDriver.php 2.4 KB

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