MySqlConnection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Illuminate\Database;
  3. use Exception;
  4. use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
  5. use Illuminate\Database\Query\Processors\MySqlProcessor;
  6. use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
  7. use Illuminate\Database\Schema\MySqlBuilder;
  8. use Illuminate\Database\Schema\MySqlSchemaState;
  9. use Illuminate\Filesystem\Filesystem;
  10. use Illuminate\Support\Str;
  11. use PDO;
  12. class MySqlConnection extends Connection
  13. {
  14. /**
  15. * Escape a binary value for safe SQL embedding.
  16. *
  17. * @param string $value
  18. * @return string
  19. */
  20. protected function escapeBinary($value)
  21. {
  22. $hex = bin2hex($value);
  23. return "x'{$hex}'";
  24. }
  25. /**
  26. * Determine if the given database exception was caused by a unique constraint violation.
  27. *
  28. * @param \Exception $exception
  29. * @return bool
  30. */
  31. protected function isUniqueConstraintError(Exception $exception)
  32. {
  33. return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage()));
  34. }
  35. /**
  36. * Determine if the connected database is a MariaDB database.
  37. *
  38. * @return bool
  39. */
  40. public function isMaria()
  41. {
  42. return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB');
  43. }
  44. /**
  45. * Get the server version for the connection.
  46. *
  47. * @return string
  48. */
  49. public function getServerVersion(): string
  50. {
  51. return str_contains($version = parent::getServerVersion(), 'MariaDB')
  52. ? Str::between($version, '5.5.5-', '-MariaDB')
  53. : $version;
  54. }
  55. /**
  56. * Get the default query grammar instance.
  57. *
  58. * @return \Illuminate\Database\Query\Grammars\MySqlGrammar
  59. */
  60. protected function getDefaultQueryGrammar()
  61. {
  62. ($grammar = new QueryGrammar)->setConnection($this);
  63. return $this->withTablePrefix($grammar);
  64. }
  65. /**
  66. * Get a schema builder instance for the connection.
  67. *
  68. * @return \Illuminate\Database\Schema\MySqlBuilder
  69. */
  70. public function getSchemaBuilder()
  71. {
  72. if (is_null($this->schemaGrammar)) {
  73. $this->useDefaultSchemaGrammar();
  74. }
  75. return new MySqlBuilder($this);
  76. }
  77. /**
  78. * Get the default schema grammar instance.
  79. *
  80. * @return \Illuminate\Database\Schema\Grammars\MySqlGrammar
  81. */
  82. protected function getDefaultSchemaGrammar()
  83. {
  84. ($grammar = new SchemaGrammar)->setConnection($this);
  85. return $this->withTablePrefix($grammar);
  86. }
  87. /**
  88. * Get the schema state for the connection.
  89. *
  90. * @param \Illuminate\Filesystem\Filesystem|null $files
  91. * @param callable|null $processFactory
  92. * @return \Illuminate\Database\Schema\MySqlSchemaState
  93. */
  94. public function getSchemaState(?Filesystem $files = null, ?callable $processFactory = null)
  95. {
  96. return new MySqlSchemaState($this, $files, $processFactory);
  97. }
  98. /**
  99. * Get the default post processor instance.
  100. *
  101. * @return \Illuminate\Database\Query\Processors\MySqlProcessor
  102. */
  103. protected function getDefaultPostProcessor()
  104. {
  105. return new MySqlProcessor;
  106. }
  107. }