AbstractSQLAnywhereDriver.php 5.1 KB

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