Best laravel framework open-source packages.

LibSQL

A minimalistic implementation of asynchronous SQL
Updated 3 weeks ago

libSQL

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

Tags sqlite php