-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLibsqlConnection.php
More file actions
255 lines (197 loc) · 6.28 KB
/
LibsqlConnection.php
File metadata and controls
255 lines (197 loc) · 6.28 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
declare(strict_types=1);
namespace Libsql\Laravel\Database;
use Illuminate\Database\Connection;
use Illuminate\Filesystem\Filesystem;
use Libsql\Transaction;
class LibsqlConnection extends Connection
{
protected LibsqlDatabase $db;
protected Transaction $tx;
/**
* The active PDO connection used for reads.
*
* @var LibsqlDatabase|\Closure
*/
protected $readPdo;
protected array $lastInsertIds = [];
protected array $bindings = [];
protected int $mode = \PDO::FETCH_OBJ;
public function __construct(LibsqlDatabase $db, string $database = ':memory:', string $tablePrefix = '', array $config = [])
{
$libsqlDb = function () use ($db) {
return $db;
};
parent::__construct($libsqlDb, $database, $tablePrefix, $config);
$this->db = $db;
$this->schemaGrammar = $this->getDefaultSchemaGrammar();
}
public function sync(): void
{
$this->db->sync();
}
public function getConnectionMode(): string
{
return $this->db->getConnectionMode();
}
public function inTransaction(): bool
{
return $this->db->inTransaction();
}
public function setFetchMode(int $mode, mixed ...$args): bool
{
$this->mode = $mode;
return true;
}
public function getServerVersion(): string
{
return $this->db->version();
}
public function getPdo(): LibsqlDatabase
{
return $this->db;
}
/**
* Set the active PDO connection used for reads.
*
* @param LibsqlDatabase|\Closure $pdo
* @return \Libsql\Laravel\Database\LibsqlConnection
*/
public function setReadPdo($pdo): self
{
$this->readPdo = $pdo;
return $this;
}
public function createReadPdo(array $config): ?LibsqlDatabase
{
$db = function () use ($config) {
return new LibsqlDatabase($config);
};
$this->setReadPdo($db);
return $db();
}
public function selectOne($query, $bindings = [], $useReadPdo = true)
{
$records = $this->select($query, $bindings, $useReadPdo);
return array_shift($records);
}
public function select($query, $bindings = [], $useReadPdo = true)
{
$bindings = array_map(function ($binding) {
return is_bool($binding) ? (int) $binding : $binding;
}, $bindings);
$data = $this->run($query, $bindings, function ($query, $bindings) {
if ($this->pretending()) {
return [];
}
$statement = $this->getPdo()->prepare($query);
$results = (array) $statement->query($bindings);
return $results;
});
$rowValues = array_values($data);
return match ($this->mode) {
\PDO::FETCH_BOTH => array_merge($data, $rowValues),
\PDO::FETCH_ASSOC, \PDO::FETCH_NAMED => $data,
\PDO::FETCH_NUM => $rowValues,
\PDO::FETCH_OBJ => arrayToStdClass($data),
default => throw new \PDOException('Unsupported fetch mode.'),
};
}
public function insert($query, $bindings = []): bool
{
return $this->affectingStatement($query, $bindings) > 0;
}
public function update($query, $bindings = [])
{
return $this->affectingStatement($query, $bindings);
}
public function delete($query, $bindings = [])
{
return $this->affectingStatement($query, $bindings);
}
public function affectingStatement($query, $bindings = [])
{
$bindings = array_map(function ($binding) {
return is_bool($binding) ? (int) $binding : $binding;
}, $bindings);
return $this->run($query, $bindings, function ($query, $bindings) {
if ($this->pretending()) {
return 0;
}
$statement = $this->getPdo()->prepare($query);
foreach ($bindings as $key => $value) {
$type = is_resource($value) ? \PDO::PARAM_LOB : \PDO::PARAM_STR;
$statement->bindValue($key, $value, $type);
}
$statement->execute();
$this->recordsHaveBeenModified(($count = $statement->rowCount()) > 0);
return $count;
});
}
#[\ReturnTypeWillChange]
protected function getDefaultSchemaGrammar(): LibsqlSchemaGrammar
{
return new LibsqlSchemaGrammar($this);
}
public function getSchemaBuilder(): LibsqlSchemaBuilder
{
if ($this->schemaGrammar === null) {
$this->useDefaultSchemaGrammar();
}
return new LibsqlSchemaBuilder($this->db, $this);
}
public function getDefaultPostProcessor(): LibsqlQueryProcessor
{
return new LibsqlQueryProcessor();
}
public function useDefaultPostProcessor()
{
$this->postProcessor = $this->getDefaultPostProcessor();
}
protected function getDefaultQueryGrammar()
{
return new LibsqlQueryGrammar($this);
}
public function useDefaultQueryGrammar()
{
$this->queryGrammar = $this->getDefaultQueryGrammar();
}
public function query()
{
$grammar = $this->getQueryGrammar();
$processor = $this->getPostProcessor();
return new LibsqlQueryBuilder(
$this,
$grammar,
$processor
);
}
public function getSchemaState(?Filesystem $files = null, ?callable $processFactory = null): LibsqlSchemaState
{
return new LibSQLSchemaState($this, $files, $processFactory);
}
public function isUniqueConstraintError(\Exception $exception): bool
{
return (bool) preg_match('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage());
}
public function escapeString($input)
{
if ($input === null) {
return 'NULL';
}
return \SQLite3::escapeString($input);
}
public function quote($input)
{
if ($input === null) {
return 'NULL';
}
if (is_string($input)) {
return "'" . $this->escapeString($input) . "'";
}
if (is_resource($input)) {
return $this->escapeBinary(stream_get_contents($input));
}
return $this->escapeBinary($input);
}
}