-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLibsqlStatement.php
More file actions
234 lines (184 loc) · 6.58 KB
/
LibsqlStatement.php
File metadata and controls
234 lines (184 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
declare(strict_types=1);
namespace Libsql\Laravel\Database;
use Illuminate\Support\Carbon;
use Libsql\Statement;
use Libsql\Blob;
class LibsqlStatement
{
protected int $affectedRows = 0;
protected int $mode = \PDO::FETCH_OBJ;
protected array $bindings = [];
protected array|object $response = [];
protected array $lastInsertIds = [];
public function __construct(
private Statement $statement,
protected string $query
) {
}
public function setFetchMode(int $mode, mixed ...$args): bool
{
$this->mode = $mode;
return true;
}
public function bindValue($parameter, $value = null, $type = \PDO::PARAM_STR): self
{
if (is_int($parameter)) {
$this->bindings[$parameter] = $value;
} elseif (is_string($parameter)) {
$this->bindings[$parameter] = $value;
} else {
throw new \InvalidArgumentException('Parameter must be an integer or string.');
}
$this->bindings = $this->parameterCasting($this->bindings);
return $this;
}
public function prepare(string $query): self
{
return new self($this->statement, $query);
}
public function query(array $parameters = []): mixed
{
if (empty($parameters)) {
$parameters = $this->parameterCasting($this->bindings);
foreach ($parameters as $key => $value) {
$this->statement->bind([$key => $value]);
}
$results = $this->statement->query()->fetchArray();
$rows = decode($results);
$rowValues = array_values($rows);
return match ($this->mode) {
\PDO::FETCH_BOTH => array_merge($rows, $rowValues),
\PDO::FETCH_ASSOC, \PDO::FETCH_NAMED => $rows,
\PDO::FETCH_NUM => $rowValues,
\PDO::FETCH_OBJ => (object) $rows,
default => throw new \PDOException('Unsupported fetch mode.'),
};
}
$parameters = $this->parameterCasting($parameters);
foreach ($parameters as $key => $value) {
$this->statement->bind([$key => $value]);
}
$result = $this->statement->query()->fetchArray();
$rows = decode($result);
return match ($this->mode) {
\PDO::FETCH_ASSOC => collect($rows),
\PDO::FETCH_OBJ => (object) $rows,
\PDO::FETCH_NUM => array_values($rows),
default => collect($rows)
};
}
public function execute(array $parameters = []): bool
{
if (empty($parameters)) {
$parameters = $this->parameterCasting($this->bindings);
}
$this->statement->bind($parameters);
if (str_starts_with(strtolower($this->query), 'select')) {
$queryRows = $this->statement->query()->fetchArray();
$this->affectedRows = count($queryRows);
} else {
$this->affectedRows = $this->statement->execute();
}
return true;
}
#[\ReturnTypeWillChange]
public function fetch(int $mode = \PDO::FETCH_DEFAULT, int $cursorOrientation = \PDO::FETCH_ORI_NEXT, int $cursorOffset = 0): array|false
{
if ($mode === \PDO::FETCH_DEFAULT) {
$mode = $this->mode;
}
$parameters = $this->bindings;
$parameters = $this->parameterCasting($parameters);
foreach ($parameters as $key => $value) {
$this->statement->bind([$key => $value]);
}
$result = $this->statement->query();
$rows = $result->fetchArray();
$row = $rows[$cursorOffset];
$mode = \PDO::FETCH_ASSOC;
if ($this->response === $row) {
return false;
}
$this->response = $row;
$rowValues = array_values($row);
$response = match ($mode) {
\PDO::FETCH_BOTH => array_merge($row, $rowValues),
\PDO::FETCH_ASSOC, \PDO::FETCH_NAMED => $row,
\PDO::FETCH_NUM => $rowValues,
\PDO::FETCH_OBJ => (object) $row,
default => throw new \PDOException('Unsupported fetch mode.'),
};
return $response;
}
#[\ReturnTypeWillChange]
public function fetchAll(int $mode = \PDO::FETCH_DEFAULT, ...$args): array
{
if ($mode === \PDO::FETCH_DEFAULT) {
$mode = $this->mode;
}
$parameters = $this->parameterCasting($this->bindings);
foreach ($parameters as $key => $value) {
$this->statement->bind([$key => $value]);
}
$result = $this->statement->query();
$rows = $result->fetchArray();
$allRows = $rows;
$decodedRows = $this->parameterCasting($allRows);
$rowValues = \array_map('array_values', $decodedRows);
$data = match ($mode) {
\PDO::FETCH_BOTH => array_merge($allRows, $rowValues),
\PDO::FETCH_ASSOC, \PDO::FETCH_NAMED => $allRows,
\PDO::FETCH_NUM => $rowValues,
\PDO::FETCH_OBJ => (object) $allRows,
default => throw new \PDOException('Unsupported fetch mode.'),
};
return $data;
}
public function getAffectedRows(): int
{
return $this->affectedRows;
}
public function nextRowset(): bool
{
// TFIDK: database is support for multiple rowset.
return false;
}
public function rowCount(): int
{
return $this->affectedRows;
}
public function closeCursor(): void
{
$this->statement->reset();
}
private function parameterCasting(array $parameters): array
{
$parameters = collect(array_values($parameters))->map(function ($value) {
$type = match (true) {
is_string($value) && (!ctype_print($value) || !mb_check_encoding($value, 'UTF-8')) => 'blob',
is_float($value) || is_float($value) => 'float',
is_int($value) => 'integer',
is_bool($value) => 'boolean',
$value === null => 'null',
$value instanceof Carbon => 'datetime',
is_vector($value) => 'vector',
default => 'text',
};
if ($type === 'blob') {
$value = new Blob($value);
}
if ($type === 'boolean') {
$value = (int) $value;
}
if ($type === 'datetime') {
$value = $value->toDateTimeString();
}
if ($type === 'vector') {
$value = json_encode($value);
}
return $value;
})->toArray();
return $parameters;
}
}