PaperConnection.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace PhpDevCommunity\PaperORM;
  3. use Exception;
  4. use LogicException;
  5. use PDO;
  6. use PDOStatement;
  7. use PhpDevCommunity\PaperORM\Driver\DriverInterface;
  8. use PhpDevCommunity\PaperORM\Pdo\PaperPDO;
  9. use Psr\Log\LoggerInterface;
  10. final class PaperConnection
  11. {
  12. private ?PaperPDO $pdo = null;
  13. private array $params;
  14. private DriverInterface $driver;
  15. private bool $debug;
  16. private ?LoggerInterface $logger = null;
  17. public function __construct(DriverInterface $driver, array $params)
  18. {
  19. $this->params = $params;
  20. $this->driver = $driver;
  21. $extra = $params['extra'] ?? [];
  22. $this->debug = (bool)($extra['debug'] ?? false);
  23. }
  24. public function executeStatement(string $query, array $params = []): int
  25. {
  26. $db = $this->executeQuery($query, $params);
  27. return $db->rowCount();
  28. }
  29. public function executeQuery(string $query, array $params = []): PDOStatement
  30. {
  31. $db = $this->getPdo()->prepare($query);
  32. if ($db === false) {
  33. throw new Exception($this->getPdo()->errorInfo()[2]);
  34. }
  35. foreach ($params as $key => $value) {
  36. if (is_string($key)) {
  37. $db->bindValue(':' . $key, $value);
  38. } else {
  39. $db->bindValue($key + 1, $value);
  40. }
  41. }
  42. $db->execute();
  43. return $db;
  44. }
  45. public function fetch(string $query, array $params = []): ?array
  46. {
  47. $db = $this->executeQuery($query, $params);
  48. $data = $db->fetch(PDO::FETCH_ASSOC);
  49. return $data === false ? null : $data;
  50. }
  51. public function fetchAll(string $query, array $params = []): array
  52. {
  53. $db = $this->executeQuery($query, $params);
  54. return $db->fetchAll(PDO::FETCH_ASSOC);
  55. }
  56. public function getParams(): array
  57. {
  58. return $this->params;
  59. }
  60. public function getDriver(): DriverInterface
  61. {
  62. return $this->driver;
  63. }
  64. public function getPdo(): PaperPDO
  65. {
  66. $this->connect();
  67. return $this->pdo;
  68. }
  69. public function connect(): bool
  70. {
  71. if (!$this->isConnected()) {
  72. $params = $this->params;
  73. unset($params['extra']);
  74. $this->pdo = $this->driver->connect($this->params);
  75. if ($this->debug) {
  76. $this->pdo->enableSqlDebugger($this->logger);
  77. }
  78. return true;
  79. }
  80. return false;
  81. }
  82. public function isConnected(): bool
  83. {
  84. return $this->pdo !== null;
  85. }
  86. public function close(): void
  87. {
  88. $this->pdo = null;
  89. }
  90. public function cloneConnectionWithoutDbname(): self
  91. {
  92. $params = $this->params;
  93. unset($params['path']);
  94. return new self($this->driver, $params);
  95. }
  96. public function withLogger(LoggerInterface $logger): self
  97. {
  98. $this->logger = $logger;
  99. return $this;
  100. }
  101. }