Skip to content

Commit 6a70e92

Browse files
authored
Merge pull request #2 from phenixphp/feature/improve-worker-handling-and-statements
Improve worker handling and statements
2 parents ab2058f + 2725068 commit 6a70e92

12 files changed

+12
-305
lines changed

src/Internal/ConnectionProcessor.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,6 @@ public function rollbackTransaction(): Future
212212
return $deferred->getFuture();
213213
}
214214

215-
public function prepare(string $sql): Future
216-
{
217-
return $this->executePrepare(new Tasks\PrepareStatement($this->config, $sql));
218-
}
219-
220215
public function execute(string $sql, array $params = []): Future
221216
{
222217
return $this->executeQuery(new Tasks\ExecuteStatement($this->config, $sql, $params));
@@ -237,9 +232,11 @@ public function close(): void
237232
}
238233
}
239234

240-
public function shutdown(): void
235+
public function countParameters(string $sql): int
241236
{
242-
$this->worker->shutdown();
237+
$count = preg_match_all('/\?|:[a-zA-Z_]\w*/', $sql, $matches);
238+
239+
return $count ?: 0;
243240
}
244241

245242
/**
@@ -319,38 +316,6 @@ protected function executeQuery(Task $task): Future
319316
return $deferred->getFuture();
320317
}
321318

322-
/**
323-
* @return Future<array{parameterCount: int, columnDefinitions: array<array{name: string, type: string, declaredType: string|null, table: string|null, length: int, flags: int, decimals: int}>}>
324-
*/
325-
protected function executePrepare(Task $task): Future
326-
{
327-
if ($this->isClosed()) {
328-
$this->throwConnectionException();
329-
}
330-
331-
$execution = $this->worker->submit($task);
332-
333-
/** @var Result $taskResult */
334-
$taskResult = $execution->await();
335-
336-
if ($taskResult->failed()) {
337-
$deferred = new DeferredFuture();
338-
$deferred->error(new Error($taskResult->message() ?? "Failed to prepare statement"));
339-
340-
return $deferred->getFuture();
341-
}
342-
343-
$this->lastUsedAt = time();
344-
345-
$data = $taskResult->output();
346-
$data['columnDefinitions'] = $this->buildColumnDefinitions($data['columnDefinitions'] ?? null);
347-
348-
$deferred = new DeferredFuture();
349-
$deferred->complete($data);
350-
351-
return $deferred->getFuture();
352-
}
353-
354319
protected function throwConnectionException(): never
355320
{
356321
throw new ConnectionFailureException('The connection has been closed');

src/Internal/SqliteConnectionStatement.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,11 @@ public function execute(array $params = []): SqliteResult
5252
$result = $execution->await();
5353

5454
if ($result instanceof Error) {
55-
$this->processor->shutdown();
56-
5755
throw new SqliteException('Failed to execute statement: ' . $result->getMessage(), 0, $result);
5856
}
5957

6058
$this->lastUsedAt = time();
6159

62-
if ($this->processor::class === ConnectionProcessor::class) {
63-
$this->processor->shutdown();
64-
}
65-
6660
return $result;
6761
}
6862

@@ -113,8 +107,6 @@ public function close(): void
113107
{
114108
$this->closed = true;
115109
$this->bindings = [];
116-
117-
$this->processor->shutdown();
118110
}
119111

120112
public function onClose(Closure $onClose): void

src/Internal/SqliteConnectionTransaction.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
use Amp\Sql\SqlTransactionIsolation;
88
use Closure;
9-
use Error;
109
use Phenix\Sqlite\Contracts\SqliteResult;
1110
use Phenix\Sqlite\Contracts\SqliteStatement;
1211
use Phenix\Sqlite\Contracts\SqliteTransaction;
13-
use Phenix\Sqlite\Internal\Exceptions\SqliteException;
1412
use Phenix\Sqlite\Internal\Exceptions\SqliteTransactionException;
1513

1614
class SqliteConnectionTransaction implements SqliteTransaction
@@ -46,17 +44,11 @@ public function prepare(string $sql): SqliteStatement
4644
$this->throwTransactionException();
4745
}
4846

49-
$data = $this->processor->prepare($sql)->await();
50-
51-
if ($data instanceof Error) {
52-
throw new SqliteException('Failed to prepare statement: ' . $data->getMessage());
53-
}
54-
5547
return new SqliteConnectionStatement(
5648
processor: $this->processor,
5749
sql: $sql,
58-
parameterCount: $data['parameterCount'] ?? 0,
59-
columnDefinitions: $data['columnDefinitions'] ?? [],
50+
parameterCount: $this->processor->countParameters($sql),
51+
columnDefinitions: [],
6052
);
6153
}
6254

@@ -103,10 +95,6 @@ public function commit(): void
10395
foreach ($this->onCommitCallbacks as $callback) {
10496
$callback();
10597
}
106-
107-
if ($this->savepointId === null) {
108-
$this->processor->shutdown();
109-
}
11098
}
11199

112100
public function rollback(): void
@@ -128,10 +116,6 @@ public function rollback(): void
128116
foreach ($this->onRollbackCallbacks as $callback) {
129117
$callback();
130118
}
131-
132-
if ($this->savepointId === null) {
133-
$this->processor->shutdown();
134-
}
135119
}
136120

137121
public function isActive(): bool

src/Internal/SqliteWorkerFactory.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Internal/Tasks/ConnectDatabase.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,6 @@ protected function mapToSqliteType(string $nativeType): string
128128
};
129129
}
130130

131-
protected function countParameters(string $sql): int
132-
{
133-
$count = preg_match_all('/\?|:[a-zA-Z_][a-zA-Z0-9_]*/', $sql, $matches);
134-
135-
return $count ?: 0;
136-
}
137-
138131
protected function query(PDO $pdo, string $sql): Result
139132
{
140133
$stmt = $pdo->query($sql);
@@ -164,23 +157,6 @@ protected function query(PDO $pdo, string $sql): Result
164157
]);
165158
}
166159

167-
protected function prepare(PDO $pdo, string $sql): Result
168-
{
169-
$stmt = $pdo->prepare($sql);
170-
171-
if (! $stmt) {
172-
return Result::failure(message: "Failed to prepare statement: {$sql}");
173-
}
174-
175-
$parameterCount = $this->countParameters($sql);
176-
$columnDefinitions = $this->buildColumnDefinitions($stmt);
177-
178-
return Result::success([
179-
'parameterCount' => $parameterCount,
180-
'columnDefinitions' => $columnDefinitions,
181-
]);
182-
}
183-
184160
protected function execute(PDO $pdo, string $sql, array $params): Result
185161
{
186162
$stmt = $pdo->prepare($sql);

src/Internal/Tasks/PrepareStatement.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Internal/Tasks/PrepareTransactionStatement.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Internal/TransactionConnectionProcessor.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public function query(string $query): Future
1717
return $this->executeQuery(new ExecuteTransactionQuery($this->config, $query));
1818
}
1919

20-
public function prepare(string $sql): Future
21-
{
22-
return $this->executePrepare(new Tasks\PrepareTransactionStatement($this->config, $sql));
23-
}
24-
2520
public function execute(string $sql, array $params = []): Future
2621
{
2722
return $this->executeQuery(new Tasks\ExecuteTransactionStatement($this->config, $sql, $params));

src/Internal/sqlite-worker.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/SqliteConnection.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
use Amp\Sql\SqlTransactionIsolation;
1212
use Amp\Sql\SqlTransactionIsolationLevel;
1313
use Closure;
14-
use Error;
1514
use Phenix\Sqlite\Contracts\SqliteConnection as SqliteConnectionContract;
1615
use Phenix\Sqlite\Contracts\SqliteResult;
1716
use Phenix\Sqlite\Contracts\SqliteStatement;
1817
use Phenix\Sqlite\Contracts\SqliteTransaction;
1918
use Phenix\Sqlite\Internal\Exceptions\SqliteException;
2019
use Phenix\Sqlite\Internal\SqliteConnectionStatement;
21-
use Phenix\Sqlite\Internal\SqliteWorkerFactory;
2220
use Revolt\EventLoop;
2321
use Throwable;
2422

23+
use function Amp\Parallel\Worker\getWorker;
24+
2525
class SqliteConnection implements SqliteConnectionContract
2626
{
2727
use ForbidCloning;
@@ -108,7 +108,7 @@ public function beginTransaction(): SqliteTransaction
108108

109109
$this->busy = $deferred = new DeferredFuture();
110110

111-
$processor = new Internal\TransactionConnectionProcessor($this->config, SqliteWorkerFactory::create());
111+
$processor = new Internal\TransactionConnectionProcessor($this->config, getWorker());
112112

113113
try {
114114
$result = $processor->beginTransaction()->await();
@@ -141,20 +141,14 @@ public function prepare(string $sql): SqliteStatement
141141

142142
$this->busy = $deferred = new DeferredFuture();
143143

144-
$processor = new Internal\ConnectionProcessor($this->config, SqliteWorkerFactory::create());
144+
$processor = new Internal\ConnectionProcessor($this->config, getWorker());
145145

146146
try {
147-
$data = $processor->prepare($sql)->await();
148-
149-
if ($data instanceof Error) {
150-
throw new SqliteException('Failed to prepare statement: ' . $data->getMessage());
151-
}
152-
153147
$statement = new SqliteConnectionStatement(
154148
processor: $processor,
155149
sql: $sql,
156-
parameterCount: $data['parameterCount'] ?? 0,
157-
columnDefinitions: $data['columnDefinitions'] ?? [],
150+
parameterCount: $processor->countParameters($sql),
151+
columnDefinitions: [],
158152
);
159153
} catch (Throwable $exception) {
160154
$this->busy = null;

0 commit comments

Comments
 (0)