cooldogedev/libsql
最新稳定版本:v0.2.6
Composer 安装命令:
composer require cooldogedev/libsql
包简介
A minimalistic implementation of asynchronous SQL
README 文档
README
A minimalistic implementation of asynchronous SQL for PHP.
Usage
Initialise the connection pool
$pool = new ConnectionPool(PluginBase, [ "provider" => "sqlite", "threads" => 2, "sqlite" => [ "path" => "test.db" ] ]);
Examples
Retrieve all customer records
- Create the query class
final class CustomerRetrievalQuery extends SQLiteQuery { public function onRun(SQLite3 $connection): void { $this->setResult($connection->query($this->getQuery())?->fetchArray() ?: []); } public function getQuery(): string { return "SELECT * FROM customers"; } }
- Execute the query
$query = new CustomerRetrievalQuery(); $query->execute( onSuccess: function (array $customers): void { foreach ($customers as $customer) { echo $customer["name"] . " " . $customer["lastName"] . ": " . $customer["age"]; echo PHP_EOL; } }, onFailure: function (SQLException $exception): void { echo "Failed to retrieve customers due to: " . $exception->getMessage(); } );
Create a new customer record
- Create the query class
final class CustomerCreationQuery extends SQLiteQuery { public function __construct( protected string $name, protected string $lastName, protected int $age ) {} public function onRun(SQLite3 $connection): bool { $statement = $connection->prepare($this->getQuery()); $statement->bindValue(":name", $this->getName()); $statement->bindValue(":lastName", $this->getLastName()); $statement->bindValue(":age", $this->getAge()); $statement->execute(); $this->setResult($connection->changes() > 0); $statement->close(); } public function getQuery(): string { return "INSERT OR IGNORE INTO customers (name, lastName, age) VALUES (:name, :lastName, :age)"; } public function getName(): string { return $this->name; } public function getLastName(): string { return $this->lastName; } public function getAge(): int { return $this->age; } }
- Execute the query
$query = new CustomerCreationQuery("Saul", "Goodman", 41); $pool->submit( query: $query, onSuccess: function (bool $created): void { echo $created ? "Customer created successfully!" : "Customer already exists!"; }, onFailure: function (SQLException $exception): void { echo "Failed to create the record due to: " . $exception->getMessage(); } );
Projects using libSQL
统计信息
- 总下载量: 101
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-24