Connection.php
98 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 | * @param wpdb $wpdb |
| 21 | */ |
| 22 | public function __construct($wpdb) |
| 23 | { |
| 24 | $this->wpdb = $wpdb; |
| 25 | |
| 26 | // Enable SQL_BIG_SELECTS to handle complex JOINs on restrictive hosting |
| 27 | $this->wpdb->query('SET SESSION SQL_BIG_SELECTS=1'); |
| 28 | |
| 29 | $this->wpdb->query("SET NAMES " . (defined('DB_CHARSET') ? DB_CHARSET : 'utf8mb4')); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param string $statement |
| 34 | * |
| 35 | * @return Statement |
| 36 | */ |
| 37 | public function query($statement) |
| 38 | { |
| 39 | $stmt = new Statement($this->wpdb, $statement); |
| 40 | $stmt->execute(); |
| 41 | |
| 42 | return $stmt; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param string $statement |
| 47 | * |
| 48 | * @return Statement |
| 49 | */ |
| 50 | public function prepare($statement) |
| 51 | { |
| 52 | return new Statement($this->wpdb, $statement); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return int |
| 57 | */ |
| 58 | public function lastInsertId() |
| 59 | { |
| 60 | return $this->wpdb->insert_id; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return void |
| 65 | */ |
| 66 | public function beginTransaction() |
| 67 | { |
| 68 | $this->wpdb->query('START TRANSACTION'); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return void |
| 73 | */ |
| 74 | public function commit() |
| 75 | { |
| 76 | $this->wpdb->query('COMMIT'); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return void |
| 81 | */ |
| 82 | public function rollBack() |
| 83 | { |
| 84 | $this->wpdb->query('ROLLBACK'); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Allow the connection wrapper to be used as a callable (historical usage in repositories `$connection()`). |
| 89 | * Returning $this preserves backward compatibility without altering repository constructors. |
| 90 | * |
| 91 | * @return $this |
| 92 | */ |
| 93 | public function __invoke() |
| 94 | { |
| 95 | return $this; |
| 96 | } |
| 97 | } |
| 98 |