Connection.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\DB\WPDB; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Database\ConnectionInterface; |
| 6 | use wpdb; |
| 7 | |
| 8 | /** |
| 9 | * Class Connection |
| 10 | * |
| 11 | * @package AmeliaBooking\Infrastructure\DB\WPDB |
| 12 | * @property \wpdb $wpdb |
| 13 | */ |
| 14 | class Connection implements ConnectionInterface |
| 15 | { |
| 16 | /** @var wpdb */ |
| 17 | private $wpdb; |
| 18 | |
| 19 | /** |
| 20 | * Connection constructor. |
| 21 | * |
| 22 | * @param wpdb $wpdb |
| 23 | */ |
| 24 | public function __construct($wpdb) |
| 25 | { |
| 26 | $this->wpdb = $wpdb; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param string $statement |
| 31 | * |
| 32 | * @return Statement |
| 33 | */ |
| 34 | public function query($statement) |
| 35 | { |
| 36 | $stmt = new Statement($this->wpdb, $statement); |
| 37 | $stmt->execute(); |
| 38 | |
| 39 | return $stmt; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string $statement |
| 44 | * |
| 45 | * @return Statement |
| 46 | */ |
| 47 | public function prepare($statement) |
| 48 | { |
| 49 | return new Statement($this->wpdb, $statement); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return int |
| 54 | */ |
| 55 | public function lastInsertId() |
| 56 | { |
| 57 | return $this->wpdb->insert_id; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return void |
| 62 | */ |
| 63 | public function beginTransaction() |
| 64 | { |
| 65 | $this->wpdb->query('START TRANSACTION'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return void |
| 70 | */ |
| 71 | public function commit() |
| 72 | { |
| 73 | $this->wpdb->query('COMMIT'); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return void |
| 78 | */ |
| 79 | public function rollBack() |
| 80 | { |
| 81 | $this->wpdb->query('ROLLBACK'); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Allow the connection wrapper to be used as a callable (historical usage in repositories `$connection()`). |
| 86 | * Returning $this preserves backward compatibility without altering repository constructors. |
| 87 | * |
| 88 | * @return $this |
| 89 | */ |
| 90 | public function __invoke() |
| 91 | { |
| 92 | return $this; |
| 93 | } |
| 94 | } |
| 95 |