Statement.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\DB\MySQLi; |
| 4 | |
| 5 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 6 | use mysqli; |
| 7 | |
| 8 | /** |
| 9 | * Class Statement |
| 10 | * |
| 11 | * @package AmeliaBooking\Infrastructure\DB\MySQLi |
| 12 | */ |
| 13 | class Statement |
| 14 | { |
| 15 | /** @var mysqli $mysqli */ |
| 16 | private $mysqli; |
| 17 | |
| 18 | /** @var Query $query */ |
| 19 | private $query; |
| 20 | |
| 21 | /** @var Result $result */ |
| 22 | private $result; |
| 23 | |
| 24 | /** @var array $params */ |
| 25 | private $params = []; |
| 26 | |
| 27 | /** |
| 28 | * @param mysqli $mysqli |
| 29 | * @param Result $result |
| 30 | * @param Query $query |
| 31 | */ |
| 32 | public function __construct($mysqli, $result, $query) |
| 33 | { |
| 34 | $this->mysqli = $mysqli; |
| 35 | $this->result = $result; |
| 36 | $this->query = $query; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @return mixed |
| 42 | */ |
| 43 | public function fetch() |
| 44 | { |
| 45 | return $this->result->getValue()->fetch_assoc(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @return mixed |
| 51 | */ |
| 52 | public function fetchAll() |
| 53 | { |
| 54 | $rows = []; |
| 55 | |
| 56 | while ($row = $this->result->getValue()->fetch_assoc()) { |
| 57 | $rows[] = $row; |
| 58 | } |
| 59 | |
| 60 | return $rows; |
| 61 | } |
| 62 | /** |
| 63 | * |
| 64 | * @return mixed |
| 65 | */ |
| 66 | public function rowCount() |
| 67 | { |
| 68 | return $this->result->getValue()->num_rows; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * @param array $params |
| 74 | * |
| 75 | * @return mixed |
| 76 | */ |
| 77 | public function execute($params = []) |
| 78 | { |
| 79 | $this->params = array_merge($this->params, $params); |
| 80 | |
| 81 | $paramsKeys = []; |
| 82 | $paramsValues = []; |
| 83 | $paramsTypes = []; |
| 84 | |
| 85 | foreach ($this->params as $key => $value) { |
| 86 | $index = strpos($this->query->getValue(), $key); |
| 87 | |
| 88 | $paramsKeys[$index] = $key; |
| 89 | $paramsValues[$index] = $value; |
| 90 | $paramsTypes[$index] = gettype($this->params[$key])[0]; |
| 91 | } |
| 92 | |
| 93 | usort($paramsKeys, function ($a, $b) { |
| 94 | return strlen($b) - strlen($a); |
| 95 | }); |
| 96 | |
| 97 | ksort($paramsValues); |
| 98 | ksort($paramsTypes); |
| 99 | |
| 100 | $referencedQueryParams = []; |
| 101 | |
| 102 | foreach ($paramsValues as $key => &$value) { |
| 103 | $referencedQueryParams[$key] = &$value; |
| 104 | } |
| 105 | |
| 106 | $parsedQuery = str_replace($paramsKeys, '?', $this->query->getValue()); |
| 107 | |
| 108 | if ($stmt = $this->mysqli->prepare($parsedQuery)) { |
| 109 | if ($referencedQueryParams) { |
| 110 | call_user_func_array( |
| 111 | array($stmt, 'bind_param'), |
| 112 | array_merge([str_replace('N', 'i', implode('', $paramsTypes))], $referencedQueryParams) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | $success = $stmt->execute(); |
| 117 | |
| 118 | $this->result->setValue($stmt->get_result()); |
| 119 | } else { |
| 120 | throw new \Exception(); |
| 121 | } |
| 122 | |
| 123 | $this->params = []; |
| 124 | |
| 125 | return $stmt && $success; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param string $key |
| 130 | * @param string $value |
| 131 | * |
| 132 | * @return mixed |
| 133 | */ |
| 134 | public function bindParam($key, $value) |
| 135 | { |
| 136 | $this->params[$key] = $value; |
| 137 | } |
| 138 | } |
| 139 |