Skip to content

Commit c51bb81

Browse files
authored
Merge pull request #12 from clue-labs/databaseinterface
Refactor to use dedicated `Factory` to open database and split `DatabaseInterface` and internal `ProcessIoDatabase` implementation
2 parents dc98482 + c957d83 commit c51bb81

File tree

9 files changed

+568
-477
lines changed

9 files changed

+568
-477
lines changed

README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ built on top of [ReactPHP](https://reactphp.org/).
77

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

2830
```php
2931
$loop = React\EventLoop\Factory::create();
32+
$factory = new Clue\React\SQLite\Factory($loop);
3033

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

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

5457
## Usage
5558

56-
### Database
59+
### Factory
5760

58-
The `Database` class represents a connection that is responsible for
59-
comunicating with your SQLite database wrapper, managing the connection state
60-
and sending your database queries.
61+
The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
62+
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
63+
64+
```php
65+
$loop = React\EventLoop\Factory::create();
66+
$factory = new Clue\React\SQLite\Factory($loop);
67+
```
6168

6269
#### open()
6370

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

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

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

8693
```php
87-
Database::open($loop, 'users.db', SQLITE3_OPEN_READONLY)->then(function (Database $db) {
94+
$factory->open('users.db', SQLITE3_OPEN_READONLY)->then(function (DatabaseInterface $db) {
8895
// database ready (read-only)
8996
// $db->quit();
9097
}, function (Exception $e) {
9198
echo 'Error: ' . $e->getMessage() . PHP_EOL;
9299
});
93100
```
94101

102+
### DatabaseInterface
103+
104+
The `DatabaseInterface` represents a connection that is responsible for
105+
comunicating with your SQLite database wrapper, managing the connection state
106+
and sending your database queries.
107+
95108
#### exec()
96109

97110
The `exec(string $query): PromiseInterface<Result>` method can be used to

examples/insert.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22

3-
use React\EventLoop\Factory;
4-
use Clue\React\SQLite\Database;
3+
use Clue\React\SQLite\DatabaseInterface;
4+
use Clue\React\SQLite\Factory;
55
use Clue\React\SQLite\Result;
66

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

9-
$loop = Factory::create();
9+
$loop = React\EventLoop\Factory::create();
10+
$factory = new Factory($loop);
1011

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

1516
for ($i = 0; $i < $n; ++$i) {

examples/search.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22

3-
use React\EventLoop\Factory;
4-
use Clue\React\SQLite\Database;
3+
use Clue\React\SQLite\DatabaseInterface;
4+
use Clue\React\SQLite\Factory;
55
use Clue\React\SQLite\Result;
66

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

9-
$loop = Factory::create();
9+
$loop = React\EventLoop\Factory::create();
10+
$factory = new Factory($loop);
1011

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

0 commit comments

Comments
 (0)