Skip to content

Improve documentation and examples #43

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
Oct 4, 2021
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
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ so you can query your data without blocking your main application.
and does not get in your way.
Future or custom commands and events require no changes to be supported.
* **Good test coverage** -
Comes with an automated tests suite and is regularly tested against actual SQLite databases in the wild.
Comes with an automated test suite and is regularly tested against actual SQLite databases in the wild.

**Table of contents**

Expand Down Expand Up @@ -64,6 +64,10 @@ existing SQLite database file (or automatically create it on first run) and then
`INSERT` a new record to the database:

```php
<?php

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

$factory = new Clue\React\SQLite\Factory();

$db = $factory->openLazy('users.db');
Expand Down Expand Up @@ -164,11 +168,11 @@ the underlying database is ready. Additionally, it will only keep this
underlying database in an "idle" state for 60s by default and will
automatically end the underlying database when it is no longer needed.

From a consumer side this means that you can start sending queries to the
From a consumer side, this means that you can start sending queries to the
database right away while the underlying database process may still be
outstanding. Because creating this underlying process may take some
time, it will enqueue all oustanding commands and will ensure that all
commands will be executed in correct order once the database is ready.
time, it will enqueue all outstanding commands and will ensure that all
commands will be executed in the correct order once the database is ready.
In other words, this "virtual" database behaves just like a "real"
database as described in the `DatabaseInterface` and frees you from
having to deal with its async resolution.
Expand Down Expand Up @@ -213,7 +217,7 @@ $db = $factory->openLazy('users.db', SQLITE3_OPEN_READONLY);
By default, this method will keep "idle" connection open for 60s and will
then end the underlying connection. The next request after an "idle"
connection ended will automatically create a new underlying connection.
This ensure you always get a "fresh" connection and as such should not be
This ensures you always get a "fresh" connection and as such should not be
confused with a "keepalive" or "heartbeat" mechanism, as this will not
actively try to probe the connection. You can explicitly pass a custom
idle timeout value in seconds (or use a negative number to not apply a
Expand Down Expand Up @@ -355,7 +359,7 @@ The `close(): void` method can be used to
force-close the connection.

Unlike the `quit()` method, this method will immediately force-close the
connection and reject all oustanding commands.
connection and reject all outstanding commands.

```php
$db->close();
Expand Down Expand Up @@ -400,7 +404,7 @@ See also the [`close()`](#close) method.

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
The recommended way to install this library is [through Composer](https://getcomposer.org/).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)

This project follows [SemVer](https://semver.org/).
Expand All @@ -427,7 +431,7 @@ $ sudo apt install php-sqlite3
## Tests

To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org):
dependencies [through Composer](https://getcomposer.org/):

```bash
$ composer install
Expand All @@ -436,7 +440,7 @@ $ composer install
To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
$ vendor/bin/phpunit
```

## License
Expand Down
9 changes: 4 additions & 5 deletions examples/insert.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php

use Clue\React\SQLite\Factory;
use Clue\React\SQLite\Result;

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

$factory = new Factory();
$factory = new Clue\React\SQLite\Factory();

$n = isset($argv[1]) ? $argv[1] : 1;
$db = $factory->openLazy('test.db');
Expand All @@ -14,8 +11,10 @@
$promise->then(null, 'printf');

for ($i = 0; $i < $n; ++$i) {
$db->exec("INSERT INTO foo (bar) VALUES ('This is a test')")->then(function (Result $result) {
$db->exec("INSERT INTO foo (bar) VALUES ('This is a test')")->then(function (Clue\React\SQLite\Result $result) {
echo 'New row ' . $result->insertId . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}

Expand Down
13 changes: 6 additions & 7 deletions examples/search.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?php

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

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

$factory = new Factory();
$factory = new Clue\React\SQLite\Factory();

$search = isset($argv[1]) ? $argv[1] : 'foo';
$db = $factory->openLazy('test.db');

$db->query('SELECT * FROM foo WHERE bar LIKE ?', ['%' . $search . '%'])->then(function (Result $result) {
$db->query('SELECT * FROM foo WHERE bar LIKE ?', ['%' . $search . '%'])->then(function (Clue\React\SQLite\Result $result) {
echo 'Found ' . count($result->rows) . ' rows: ' . PHP_EOL;
echo implode("\t", $result->columns) . PHP_EOL;
foreach ($result->rows as $row) {
echo implode("\t", $row) . PHP_EOL;
}
}, 'printf');
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

$db->quit();