vajexal / amp-sqlite
SQLite client for amphp
0.1.1
2022-03-15 22:12 UTC
Requires
- php: ^8.0|^8.1
- ext-pcntl: *
- ext-sqlite3: *
- amphp/amp: ^2.5
- amphp/parallel: ^1.4
- amphp/sql: ^1.0
- amphp/sync: ^1.4
Requires (Dev)
- ext-parallel: *
- amphp/php-cs-fixer-config: dev-master
- amphp/phpunit-util: ^2.0
This package is auto-updated.
Last update: 2025-03-16 05:09:41 UTC
README
SQLite client for amphp using parallel
Installation
composer require vajexal/amp-sqlite
Usage
<?php declare(strict_types=1); use Amp\Loop; use Vajexal\AmpSQLite\SQLiteCommandResult; use Vajexal\AmpSQLite\SQLiteConnection; use Vajexal\AmpSQLite\SQLiteResultSet; use Vajexal\AmpSQLite\SQLiteStatement; use function Vajexal\AmpSQLite\connect; require_once 'vendor/autoload.php'; Loop::run(function () { /** @var SQLiteConnection $connection */ $connection = yield connect('database.sqlite'); yield $connection->execute('drop table if exists users'); yield $connection->execute('create table users (id integer primary key, name text not null)'); yield $connection->execute('insert into users (name) values (:name)', [ ':name' => 'Bob', ]); /** @var SQLiteResultSet $results */ $results = yield $connection->query('select * from users'); while (yield $results->advance()) { $row = $results->getCurrent(); echo "Hello {$row['name']}\n"; } /** @var SQLiteStatement $statement */ $statement = yield $connection->prepare('update users set name = :name where id = 1'); /** @var SQLiteCommandResult $result */ $result = yield $statement->execute([ ':name' => 'John', ]); echo "Updated {$result->getAffectedRowCount()} rows\n"; });
Transactions
<?php declare(strict_types=1); use Amp\Loop; use Vajexal\AmpSQLite\SQLiteConnection; use Vajexal\AmpSQLite\SQLiteTransaction; use function Vajexal\AmpSQLite\connect; require_once 'vendor/autoload.php'; Loop::run(function () { /** @var SQLiteConnection $connection */ $connection = yield connect('database.sqlite'); yield $connection->execute('drop table if exists users'); yield $connection->execute('create table users (id integer primary key, name text not null)'); /** @var SQLiteTransaction $transaction */ $transaction = yield $connection->beginTransaction(); yield $transaction->execute('insert into users (name) values (:name)', [ ':name' => 'Bob', ]); yield $transaction->createSavepoint('change_name'); yield $transaction->execute('update users set name = :name where id = 1', [ ':name' => 'John', ]); yield $transaction->releaseSavepoint('change_name'); yield $transaction->commit(); });