AbstractSQLAnywhereDriver.php 4.2 KB

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