diff --git a/src/Internal/ConnectionProcessor.php b/src/Internal/ConnectionProcessor.php index 7a58a15..568d3c2 100644 --- a/src/Internal/ConnectionProcessor.php +++ b/src/Internal/ConnectionProcessor.php @@ -212,11 +212,6 @@ public function rollbackTransaction(): Future return $deferred->getFuture(); } - public function prepare(string $sql): Future - { - return $this->executePrepare(new Tasks\PrepareStatement($this->config, $sql)); - } - public function execute(string $sql, array $params = []): Future { return $this->executeQuery(new Tasks\ExecuteStatement($this->config, $sql, $params)); @@ -237,9 +232,11 @@ public function close(): void } } - public function shutdown(): void + public function countParameters(string $sql): int { - $this->worker->shutdown(); + $count = preg_match_all('/\?|:[a-zA-Z_]\w*/', $sql, $matches); + + return $count ?: 0; } /** @@ -319,38 +316,6 @@ protected function executeQuery(Task $task): Future return $deferred->getFuture(); } - /** - * @return Future}> - */ - protected function executePrepare(Task $task): Future - { - if ($this->isClosed()) { - $this->throwConnectionException(); - } - - $execution = $this->worker->submit($task); - - /** @var Result $taskResult */ - $taskResult = $execution->await(); - - if ($taskResult->failed()) { - $deferred = new DeferredFuture(); - $deferred->error(new Error($taskResult->message() ?? "Failed to prepare statement")); - - return $deferred->getFuture(); - } - - $this->lastUsedAt = time(); - - $data = $taskResult->output(); - $data['columnDefinitions'] = $this->buildColumnDefinitions($data['columnDefinitions'] ?? null); - - $deferred = new DeferredFuture(); - $deferred->complete($data); - - return $deferred->getFuture(); - } - protected function throwConnectionException(): never { throw new ConnectionFailureException('The connection has been closed'); diff --git a/src/Internal/SqliteConnectionStatement.php b/src/Internal/SqliteConnectionStatement.php index 205a210..fd4e7ca 100644 --- a/src/Internal/SqliteConnectionStatement.php +++ b/src/Internal/SqliteConnectionStatement.php @@ -52,17 +52,11 @@ public function execute(array $params = []): SqliteResult $result = $execution->await(); if ($result instanceof Error) { - $this->processor->shutdown(); - throw new SqliteException('Failed to execute statement: ' . $result->getMessage(), 0, $result); } $this->lastUsedAt = time(); - if ($this->processor::class === ConnectionProcessor::class) { - $this->processor->shutdown(); - } - return $result; } @@ -113,8 +107,6 @@ public function close(): void { $this->closed = true; $this->bindings = []; - - $this->processor->shutdown(); } public function onClose(Closure $onClose): void diff --git a/src/Internal/SqliteConnectionTransaction.php b/src/Internal/SqliteConnectionTransaction.php index 2bb08c1..1ca8f6f 100644 --- a/src/Internal/SqliteConnectionTransaction.php +++ b/src/Internal/SqliteConnectionTransaction.php @@ -6,11 +6,9 @@ use Amp\Sql\SqlTransactionIsolation; use Closure; -use Error; use Phenix\Sqlite\Contracts\SqliteResult; use Phenix\Sqlite\Contracts\SqliteStatement; use Phenix\Sqlite\Contracts\SqliteTransaction; -use Phenix\Sqlite\Internal\Exceptions\SqliteException; use Phenix\Sqlite\Internal\Exceptions\SqliteTransactionException; class SqliteConnectionTransaction implements SqliteTransaction @@ -46,17 +44,11 @@ public function prepare(string $sql): SqliteStatement $this->throwTransactionException(); } - $data = $this->processor->prepare($sql)->await(); - - if ($data instanceof Error) { - throw new SqliteException('Failed to prepare statement: ' . $data->getMessage()); - } - return new SqliteConnectionStatement( processor: $this->processor, sql: $sql, - parameterCount: $data['parameterCount'] ?? 0, - columnDefinitions: $data['columnDefinitions'] ?? [], + parameterCount: $this->processor->countParameters($sql), + columnDefinitions: [], ); } @@ -103,10 +95,6 @@ public function commit(): void foreach ($this->onCommitCallbacks as $callback) { $callback(); } - - if ($this->savepointId === null) { - $this->processor->shutdown(); - } } public function rollback(): void @@ -128,10 +116,6 @@ public function rollback(): void foreach ($this->onRollbackCallbacks as $callback) { $callback(); } - - if ($this->savepointId === null) { - $this->processor->shutdown(); - } } public function isActive(): bool diff --git a/src/Internal/SqliteWorkerFactory.php b/src/Internal/SqliteWorkerFactory.php deleted file mode 100644 index c03e176..0000000 --- a/src/Internal/SqliteWorkerFactory.php +++ /dev/null @@ -1,24 +0,0 @@ -create(); - } -} diff --git a/src/Internal/Tasks/ConnectDatabase.php b/src/Internal/Tasks/ConnectDatabase.php index 40022ac..c23994b 100644 --- a/src/Internal/Tasks/ConnectDatabase.php +++ b/src/Internal/Tasks/ConnectDatabase.php @@ -128,13 +128,6 @@ protected function mapToSqliteType(string $nativeType): string }; } - protected function countParameters(string $sql): int - { - $count = preg_match_all('/\?|:[a-zA-Z_][a-zA-Z0-9_]*/', $sql, $matches); - - return $count ?: 0; - } - protected function query(PDO $pdo, string $sql): Result { $stmt = $pdo->query($sql); @@ -164,23 +157,6 @@ protected function query(PDO $pdo, string $sql): Result ]); } - protected function prepare(PDO $pdo, string $sql): Result - { - $stmt = $pdo->prepare($sql); - - if (! $stmt) { - return Result::failure(message: "Failed to prepare statement: {$sql}"); - } - - $parameterCount = $this->countParameters($sql); - $columnDefinitions = $this->buildColumnDefinitions($stmt); - - return Result::success([ - 'parameterCount' => $parameterCount, - 'columnDefinitions' => $columnDefinitions, - ]); - } - protected function execute(PDO $pdo, string $sql, array $params): Result { $stmt = $pdo->prepare($sql); diff --git a/src/Internal/Tasks/PrepareStatement.php b/src/Internal/Tasks/PrepareStatement.php deleted file mode 100644 index f536bb2..0000000 --- a/src/Internal/Tasks/PrepareStatement.php +++ /dev/null @@ -1,31 +0,0 @@ -connect(); - - return $this->prepare($pdo, $this->sql); - } catch (PDOException $e) { - return Result::failure(message: $e->getMessage()); - } - } -} diff --git a/src/Internal/Tasks/PrepareTransactionStatement.php b/src/Internal/Tasks/PrepareTransactionStatement.php deleted file mode 100644 index ad46801..0000000 --- a/src/Internal/Tasks/PrepareTransactionStatement.php +++ /dev/null @@ -1,35 +0,0 @@ -connect(); - - if (! $pdo->inTransaction()) { - return Result::failure(message: "Not in a transaction, cannot execute prepared statement: {$this->sql}"); - } - - return $this->prepare($pdo, $this->sql); - } catch (Throwable $e) { - return Result::failure(message: $e->getMessage()); - } - } -} diff --git a/src/Internal/TransactionConnectionProcessor.php b/src/Internal/TransactionConnectionProcessor.php index 435566a..5835139 100644 --- a/src/Internal/TransactionConnectionProcessor.php +++ b/src/Internal/TransactionConnectionProcessor.php @@ -17,11 +17,6 @@ public function query(string $query): Future return $this->executeQuery(new ExecuteTransactionQuery($this->config, $query)); } - public function prepare(string $sql): Future - { - return $this->executePrepare(new Tasks\PrepareTransactionStatement($this->config, $sql)); - } - public function execute(string $sql, array $params = []): Future { return $this->executeQuery(new Tasks\ExecuteTransactionStatement($this->config, $sql, $params)); diff --git a/src/Internal/sqlite-worker.php b/src/Internal/sqlite-worker.php deleted file mode 100644 index 800ea5f..0000000 --- a/src/Internal/sqlite-worker.php +++ /dev/null @@ -1,5 +0,0 @@ -busy = $deferred = new DeferredFuture(); - $processor = new Internal\TransactionConnectionProcessor($this->config, SqliteWorkerFactory::create()); + $processor = new Internal\TransactionConnectionProcessor($this->config, getWorker()); try { $result = $processor->beginTransaction()->await(); @@ -141,20 +141,14 @@ public function prepare(string $sql): SqliteStatement $this->busy = $deferred = new DeferredFuture(); - $processor = new Internal\ConnectionProcessor($this->config, SqliteWorkerFactory::create()); + $processor = new Internal\ConnectionProcessor($this->config, getWorker()); try { - $data = $processor->prepare($sql)->await(); - - if ($data instanceof Error) { - throw new SqliteException('Failed to prepare statement: ' . $data->getMessage()); - } - $statement = new SqliteConnectionStatement( processor: $processor, sql: $sql, - parameterCount: $data['parameterCount'] ?? 0, - columnDefinitions: $data['columnDefinitions'] ?? [], + parameterCount: $processor->countParameters($sql), + columnDefinitions: [], ); } catch (Throwable $exception) { $this->busy = null; diff --git a/tests/Unit/Tasks/PrepareStatementTest.php b/tests/Unit/Tasks/PrepareStatementTest.php deleted file mode 100644 index 695ed41..0000000 --- a/tests/Unit/Tasks/PrepareStatementTest.php +++ /dev/null @@ -1,40 +0,0 @@ -getDatabasePath()); - - $task = new PrepareStatement($config, 'SELECT 1'); - $result = $task->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $this->assertTrue($result->isSuccess()); - } - - /** - * @test - */ - public function it_returns_failure_when_statement_is_invalid(): void - { - $config = SqliteConfig::fromPath($this->getDatabasePath()); - - $task = new PrepareStatement($config, 'INVALID STATEMENT'); - $result = $task->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $this->assertTrue($result->failed()); - } -} diff --git a/tests/Unit/Tasks/PrepareTransactionStatementTest.php b/tests/Unit/Tasks/PrepareTransactionStatementTest.php deleted file mode 100644 index a36d5a0..0000000 --- a/tests/Unit/Tasks/PrepareTransactionStatementTest.php +++ /dev/null @@ -1,64 +0,0 @@ -getDatabasePath()); - - $create = new ExecuteQuery($config, 'CREATE TABLE test (id INTEGER PRIMARY KEY)'); - $create->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $begin = new BeginTransaction($config); - $begin->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $task = new PrepareTransactionStatement($config, 'SELECT * FROM test'); - $result = $task->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $this->assertTrue($result->isSuccess()); - } - - /** - * @test - */ - public function it_fails_when_no_transaction_active(): void - { - $config = SqliteConfig::fromPath($this->getDatabasePath()); - $task = new PrepareTransactionStatement($config, 'SELECT * FROM test'); - - $result = $task->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $this->assertFalse($result->isSuccess()); - } - - /** - * @test - */ - public function it_returns_failure_on_invalid_statement(): void - { - $config = SqliteConfig::fromPath($this->getDatabasePath()); - - $begin = new BeginTransaction($config); - $begin->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $task = new PrepareTransactionStatement($config, 'INVALID STATEMENT'); - $result = $task->run($this->createMock(Channel::class), $this->createMock(Cancellation::class)); - - $this->assertFalse($result->isSuccess()); - } -}