Skip to content

Refactor to use dedicated Factory to open database and split DatabaseInterface and internal ProcessIoDatabase implementation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ built on top of [ReactPHP](https://reactphp.org/).

* [Quickstart example](#quickstart-example)
* [Usage](#usage)
* [Database](#database)
* [Factory](#factory)
* [open()](#open)
* [DatabaseInterface](#databaseinterface)
* [exec()](#exec)
* [query()](#query)
* [quit()](#quit)
Expand All @@ -27,10 +29,11 @@ existing SQLite database file (or automatically create it on first run) and then

```php
$loop = React\EventLoop\Factory::create();
$factory = new Clue\React\SQLite\Factory($loop);

$name = 'Alice';
Clue\React\SQLite\Database::open($loop, 'users.db')->then(
function (Clue\React\SQLite\Database $db) use ($name) {
$factory->open('users.db')->then(
function (Clue\React\SQLite\DatabaseInterface $db) use ($name) {
$db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');

$db->query('INSERT INTO foo (bar) VALUES (?)', array($name))->then(
Expand All @@ -53,25 +56,29 @@ See also the [examples](examples).

## Usage

### Database
### Factory

The `Database` class represents a connection that is responsible for
comunicating with your SQLite database wrapper, managing the connection state
and sending your database queries.
The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).

```php
$loop = React\EventLoop\Factory::create();
$factory = new Clue\React\SQLite\Factory($loop);
```

#### open()

The static `open(LoopInterface $loop, string $filename, int $flags = null): PromiseInterface<Database>` method can be used to
The `open(string $filename, int $flags = null): PromiseInterface<DatabaseInterface>` method can be used to
open a new database connection for the given SQLite database file.

This method returns a promise that will resolve with a `Database` on
This method returns a promise that will resolve with a `DatabaseInterface` on
success or will reject with an `Exception` on error. The SQLite extension
is inherently blocking, so this method will spawn an SQLite worker process
to run all SQLite commands and queries in a separate process without
blocking the main process.

```php
Database::open($loop, 'users.db')->then(function (Database $db) {
$factory->open('users.db')->then(function (DatabaseInterface $db) {
// database ready
// $db->query('INSERT INTO users (name) VALUES ("test")');
// $db->quit();
Expand All @@ -84,14 +91,20 @@ The optional `$flags` parameter is used to determine how to open the
SQLite database. By default, open uses `SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE`.

```php
Database::open($loop, 'users.db', SQLITE3_OPEN_READONLY)->then(function (Database $db) {
$factory->open('users.db', SQLITE3_OPEN_READONLY)->then(function (DatabaseInterface $db) {
// database ready (read-only)
// $db->quit();
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

### DatabaseInterface

The `DatabaseInterface` represents a connection that is responsible for
comunicating with your SQLite database wrapper, managing the connection state
and sending your database queries.

#### exec()

The `exec(string $query): PromiseInterface<Result>` method can be used to
Expand Down
9 changes: 5 additions & 4 deletions examples/insert.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

use React\EventLoop\Factory;
use Clue\React\SQLite\Database;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory;
use Clue\React\SQLite\Result;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);

$n = isset($argv[1]) ? $argv[1] : 1;
Database::open($loop, 'test.db')->then(function (Database $db) use ($n) {
$factory->open('test.db')->then(function (DatabaseInterface $db) use ($n) {
$db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');

for ($i = 0; $i < $n; ++$i) {
Expand Down
9 changes: 5 additions & 4 deletions examples/search.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

use React\EventLoop\Factory;
use Clue\React\SQLite\Database;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory;
use Clue\React\SQLite\Result;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);

$search = isset($argv[1]) ? $argv[1] : 'foo';
Database::open($loop, 'test.db')->then(function (Database $db) use ($search){
$factory->open('test.db')->then(function (DatabaseInterface $db) use ($search){
$db->query('SELECT * FROM foo WHERE bar LIKE ?', ['%' . $search . '%'])->then(function (Result $result) {
echo 'Found ' . count($result->rows) . ' rows: ' . PHP_EOL;
echo implode("\t", $result->columns) . PHP_EOL;
Expand Down
Loading