AbstractSQLAnywhereDriver.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Doctrine\DBAL\Driver;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Driver;
  5. use Doctrine\DBAL\Driver\DriverException as DeprecatedDriverException;
  6. use Doctrine\DBAL\Exception;
  7. use Doctrine\DBAL\Exception\ConnectionException;
  8. use Doctrine\DBAL\Exception\DeadlockException;
  9. use Doctrine\DBAL\Exception\DriverException;
  10. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  11. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  12. use Doctrine\DBAL\Exception\LockWaitTimeoutException;
  13. use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
  14. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  15. use Doctrine\DBAL\Exception\SyntaxErrorException;
  16. use Doctrine\DBAL\Exception\TableExistsException;
  17. use Doctrine\DBAL\Exception\TableNotFoundException;
  18. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  19. use Doctrine\DBAL\Platforms\SQLAnywhere11Platform;
  20. use Doctrine\DBAL\Platforms\SQLAnywhere12Platform;
  21. use Doctrine\DBAL\Platforms\SQLAnywhere16Platform;
  22. use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
  23. use Doctrine\DBAL\Schema\SQLAnywhereSchemaManager;
  24. use Doctrine\DBAL\VersionAwarePlatformDriver;
  25. use function assert;
  26. use function preg_match;
  27. use function version_compare;
  28. /**
  29. * Abstract base implementation of the {@link Driver} interface for SAP Sybase SQL Anywhere based drivers.
  30. */
  31. abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver
  32. {
  33. /**
  34. * {@inheritdoc}
  35. *
  36. * @deprecated
  37. *
  38. * @link http://dcx.sybase.com/index.html#sa160/en/saerrors/sqlerror.html
  39. */
  40. public function convertException($message, DeprecatedDriverException $exception)
  41. {
  42. switch ($exception->getErrorCode()) {
  43. case '-306':
  44. case '-307':
  45. case '-684':
  46. return new DeadlockException($message, $exception);
  47. case '-210':
  48. case '-1175':
  49. case '-1281':
  50. return new LockWaitTimeoutException($message, $exception);
  51. case '-100':
  52. case '-103':
  53. case '-832':
  54. return new ConnectionException($message, $exception);
  55. case '-143':
  56. return new InvalidFieldNameException($message, $exception);
  57. case '-193':
  58. case '-196':
  59. return new UniqueConstraintViolationException($message, $exception);
  60. case '-194':
  61. case '-198':
  62. return new ForeignKeyConstraintViolationException($message, $exception);
  63. case '-144':
  64. return new NonUniqueFieldNameException($message, $exception);
  65. case '-184':
  66. case '-195':
  67. return new NotNullConstraintViolationException($message, $exception);
  68. case '-131':
  69. return new SyntaxErrorException($message, $exception);
  70. case '-110':
  71. return new TableExistsException($message, $exception);
  72. case '-141':
  73. case '-1041':
  74. return new TableNotFoundException($message, $exception);
  75. }
  76. return new DriverException($message, $exception);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function createDatabasePlatformForVersion($version)
  82. {
  83. if (
  84. ! preg_match(
  85. '/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+)(?:\.(?P<build>\d+))?)?)?/',
  86. $version,
  87. $versionParts
  88. )
  89. ) {
  90. throw Exception::invalidPlatformVersionSpecified(
  91. $version,
  92. '<major_version>.<minor_version>.<patch_version>.<build_version>'
  93. );
  94. }
  95. $majorVersion = $versionParts['major'];
  96. $minorVersion = $versionParts['minor'] ?? 0;
  97. $patchVersion = $versionParts['patch'] ?? 0;
  98. $buildVersion = $versionParts['build'] ?? 0;
  99. $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion . '.' . $buildVersion;
  100. switch (true) {
  101. case version_compare($version, '16', '>='):
  102. return new SQLAnywhere16Platform();
  103. case version_compare($version, '12', '>='):
  104. return new SQLAnywhere12Platform();
  105. case version_compare($version, '11', '>='):
  106. return new SQLAnywhere11Platform();
  107. default:
  108. return new SQLAnywherePlatform();
  109. }
  110. }
  111. /**
  112. * {@inheritdoc}
  113. *
  114. * @deprecated Use Connection::getDatabase() instead.
  115. */
  116. public function getDatabase(Connection $conn)
  117. {
  118. $params = $conn->getParams();
  119. if (isset($params['dbname'])) {
  120. return $params['dbname'];
  121. }
  122. $database = $conn->query('SELECT DB_NAME()')->fetchColumn();
  123. assert($database !== false);
  124. return $database;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getDatabasePlatform()
  130. {
  131. return new SQLAnywhere12Platform();
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getSchemaManager(Connection $conn)
  137. {
  138. return new SQLAnywhereSchemaManager($conn);
  139. }
  140. }