MySqlConnection.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database;
  12. use Doctrine\DBAL\Driver\PDO\MySQL\Driver;
  13. use Exception;
  14. use Hyperf\Database\DBAL\MySqlDriver;
  15. use Hyperf\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
  16. use Hyperf\Database\Query\Processors\MySqlProcessor;
  17. use Hyperf\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
  18. use Hyperf\Database\Schema\MySqlBuilder;
  19. use PDOStatement;
  20. class MySqlConnection extends Connection
  21. {
  22. /**
  23. * Get a schema builder instance for the connection.
  24. */
  25. public function getSchemaBuilder(): MySqlBuilder
  26. {
  27. if (is_null($this->schemaGrammar)) {
  28. $this->useDefaultSchemaGrammar();
  29. }
  30. return new MySqlBuilder($this);
  31. }
  32. /**
  33. * Bind values to their parameters in the given statement.
  34. */
  35. public function bindValues(PDOStatement $statement, array $bindings): void
  36. {
  37. foreach ($bindings as $key => $value) {
  38. $statement->bindValue(
  39. is_string($key) ? $key : $key + 1,
  40. $value
  41. );
  42. }
  43. }
  44. /**
  45. * Determine if the given database exception was caused by a unique constraint violation.
  46. *
  47. * @return bool
  48. */
  49. protected function isUniqueConstraintError(Exception $exception)
  50. {
  51. return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage()));
  52. }
  53. /**
  54. * Get the default query grammar instance.
  55. */
  56. protected function getDefaultQueryGrammar(): QueryGrammar
  57. {
  58. return $this->withTablePrefix(new QueryGrammar());
  59. }
  60. /**
  61. * Get the default schema grammar instance.
  62. */
  63. protected function getDefaultSchemaGrammar(): SchemaGrammar
  64. {
  65. return $this->withTablePrefix(new SchemaGrammar());
  66. }
  67. /**
  68. * Get the default post processor instance.
  69. */
  70. protected function getDefaultPostProcessor(): MySqlProcessor
  71. {
  72. return new MySqlProcessor();
  73. }
  74. /**
  75. * Get the Doctrine DBAL driver.
  76. *
  77. * @return Driver
  78. */
  79. protected function getDoctrineDriver()
  80. {
  81. return new MySqlDriver();
  82. }
  83. }