api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LazyPdo.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Database;
  3. class LazyPdo extends \PDO
  4. {
  5. private $dsn;
  6. private $user;
  7. private $password;
  8. private $options;
  9. private $commands;
  10. private $pdo = null;
  11. public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array())
  12. {
  13. $this->dsn = $dsn;
  14. $this->user = $user;
  15. $this->password = $password;
  16. $this->options = $options;
  17. $this->commands = array();
  18. // explicitly NOT calling super::__construct
  19. }
  20. public function addInitCommand(string $command)/*: void*/
  21. {
  22. $this->commands[] = $command;
  23. }
  24. private function pdo()
  25. {
  26. if (!$this->pdo) {
  27. $this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
  28. foreach ($this->commands as $command) {
  29. $this->pdo->query($command);
  30. }
  31. }
  32. return $this->pdo;
  33. }
  34. public function reconstruct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array()): bool
  35. {
  36. $this->dsn = $dsn;
  37. $this->user = $user;
  38. $this->password = $password;
  39. $this->options = $options;
  40. $this->commands = array();
  41. if ($this->pdo) {
  42. $this->pdo = null;
  43. return true;
  44. }
  45. return false;
  46. }
  47. public function inTransaction(): bool
  48. {
  49. // Do not call parent method if there is no pdo object
  50. return $this->pdo && parent::inTransaction();
  51. }
  52. public function setAttribute($attribute, $value): bool
  53. {
  54. if ($this->pdo) {
  55. return $this->pdo()->setAttribute($attribute, $value);
  56. }
  57. $this->options[$attribute] = $value;
  58. return true;
  59. }
  60. public function getAttribute($attribute): mixed
  61. {
  62. return $this->pdo()->getAttribute($attribute);
  63. }
  64. public function beginTransaction(): bool
  65. {
  66. return $this->pdo()->beginTransaction();
  67. }
  68. public function commit(): bool
  69. {
  70. return $this->pdo()->commit();
  71. }
  72. public function rollBack(): bool
  73. {
  74. return $this->pdo()->rollBack();
  75. }
  76. public function errorCode(): mixed
  77. {
  78. return $this->pdo()->errorCode();
  79. }
  80. public function errorInfo(): array
  81. {
  82. return $this->pdo()->errorInfo();
  83. }
  84. public function exec($query): int
  85. {
  86. return $this->pdo()->exec($query);
  87. }
  88. public function prepare($statement, $options = array())
  89. {
  90. return $this->pdo()->prepare($statement, $options);
  91. }
  92. public function quote($string, $parameter_type = null): string
  93. {
  94. return $this->pdo()->quote($string, $parameter_type);
  95. }
  96. public function lastInsertId(/* ?string */$name = null): string
  97. {
  98. return $this->pdo()->lastInsertId($name);
  99. }
  100. public function query(string $query, int $fetchMode = null, mixed ...$fetchModeArgs): \PDOStatement
  101. {
  102. return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
  103. }
  104. }