|
1 | 1 | # phenixphp/sqlite |
2 | 2 |
|
| 3 | +<p>Asynchronous SQLite 3 client for PHP based on <a href="https://amphp.org/">Amp</a>.</p> |
| 4 | + |
| 5 | +[](https://github.com/phenixphp/sqlite/actions/workflows/run-tests.yml) |
| 6 | +[](https://packagist.org/packages/phenixphp/sqlite) |
| 7 | +[](https://packagist.org/packages/phenixphp/sqlite) |
| 8 | +[](https://packagist.org/packages/phenixphp/sqlite) |
| 9 | +[](https://packagist.org/packages/phenixphp/sqlite) |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +**phenixphp/sqlite** is part of the **Phenix PHP** framework ecosystem. Phenix is a web framework built on pure PHP, without external extensions, based on the [Amphp](https://amphp.org/) ecosystem, which provides non-blocking operations, asynchronism and parallel code execution natively. It runs in the PHP SAPI CLI and on its own server — it is simply powerful. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Table of Contents |
| 18 | + |
| 19 | +- [Requirements](#requirements) |
| 20 | +- [Installation](#installation) |
| 21 | +- [Configuration](#configuration) |
| 22 | +- [Usage](#usage) |
| 23 | + - [Establishing a Connection](#establishing-a-connection) |
| 24 | + - [Executing Queries](#executing-queries) |
| 25 | + - [Prepared Statements](#prepared-statements) |
| 26 | + - [Transactions](#transactions) |
| 27 | +- [Dependencies](#dependencies) |
| 28 | +- [License](#license) |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Requirements |
| 33 | + |
| 34 | +| Requirement | Version | |
| 35 | +| --- | --- | |
| 36 | +| PHP | `^8.2` | |
| 37 | +| ext-sqlite3 | `*` | |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Installation |
| 42 | + |
| 43 | +Install the package via [Composer](https://getcomposer.org/): |
| 44 | + |
| 45 | +```bash |
| 46 | +composer require phenixphp/sqlite |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Configuration |
| 52 | + |
| 53 | +The `SqliteConfig` class is the entry point for configuring the SQLite connection. It allows you to define the database path and connection parameters aligned with the Amphp SQL ecosystem. |
| 54 | + |
| 55 | +```php |
| 56 | +<?php |
| 57 | + |
| 58 | +use Phenix\Sqlite\SqliteConfig; |
| 59 | + |
| 60 | +$config = new SqliteConfig( |
| 61 | + database: __DIR__ . '/database.db', |
| 62 | +); |
| 63 | +``` |
| 64 | + |
| 65 | +> You can use `:memory:` as the database path to create an in-memory SQLite database, which is ideal for testing scenarios. |
| 66 | +
|
| 67 | +```php |
| 68 | +$config = new SqliteConfig( |
| 69 | + database: ':memory:', |
| 70 | +); |
| 71 | +``` |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Usage |
| 76 | + |
| 77 | +### Establishing a Connection |
| 78 | + |
| 79 | +Use `SqliteConfig` to create and manage asynchronous connections to your SQLite database. Since this package is built on top of Amphp, all database operations are **non-blocking** and run asynchronously using fibers. |
| 80 | + |
| 81 | +```php |
| 82 | +<?php |
| 83 | + |
| 84 | +declare(strict_types=1); |
| 85 | + |
| 86 | +use Phenix\Sqlite\SqliteConfig; |
| 87 | + |
| 88 | +use function Phenix\Sqlite\connect; |
| 89 | + |
| 90 | +$config = new SqliteConfig( |
| 91 | + database: __DIR__ . '/database.db', |
| 92 | +); |
| 93 | + |
| 94 | +$connection = connect($config); |
| 95 | + |
| 96 | +// ... use connection ... |
| 97 | + |
| 98 | +$connection->close(); |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +### Executing Queries |
| 104 | + |
| 105 | +Once you have an active connection, you can execute SQL statements — `CREATE`, `INSERT`, `SELECT`, `UPDATE`, and `DELETE` — all asynchronously without blocking the event loop. |
| 106 | + |
| 107 | +#### Creating a Table |
| 108 | + |
| 109 | +```php |
| 110 | +$connection->execute( |
| 111 | + 'CREATE TABLE IF NOT EXISTS users ( |
| 112 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 113 | + name TEXT NOT NULL, |
| 114 | + email TEXT NOT NULL |
| 115 | + )' |
| 116 | +); |
| 117 | +``` |
| 118 | + |
| 119 | +#### Inserting Records |
| 120 | + |
| 121 | +```php |
| 122 | +$connection->execute( |
| 123 | + "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')" |
| 124 | +); |
| 125 | +``` |
| 126 | + |
| 127 | +#### Selecting Records |
| 128 | + |
| 129 | +```php |
| 130 | +$result = $connection->query('SELECT * FROM users'); |
| 131 | + |
| 132 | +foreach ($result as $row) { |
| 133 | + echo $row['id'] . ': ' . $row['name'] . ' — ' . $row['email'] . PHP_EOL; |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +#### Updating Records |
| 138 | + |
| 139 | +```php |
| 140 | +$connection->execute( |
| 141 | + "UPDATE users SET name = 'Jane Doe' WHERE id = 1" |
| 142 | +); |
| 143 | +``` |
| 144 | + |
| 145 | +#### Deleting Records |
| 146 | + |
| 147 | +```php |
| 148 | +$connection->execute( |
| 149 | + "DELETE FROM users WHERE id = 1" |
| 150 | +); |
| 151 | +``` |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +### Prepared Statements |
| 156 | + |
| 157 | +Prepared statements allow you to precompile a SQL template and execute it repeatedly with different bound parameters. This improves both performance and security by preventing SQL injection. |
| 158 | + |
| 159 | +#### Preparing and Executing a Statement |
| 160 | + |
| 161 | +```php |
| 162 | +$statement = $connection->prepare( |
| 163 | + 'INSERT INTO users (name, email) VALUES (?, ?)' |
| 164 | +); |
| 165 | + |
| 166 | +$statement->execute(['Alice', 'alice@example.com']); |
| 167 | +$statement->execute(['Bob', 'bob@example.com']); |
| 168 | +``` |
| 169 | + |
| 170 | +#### Prepared Statement with Named Parameters |
| 171 | + |
| 172 | +```php |
| 173 | +$statement = $connection->prepare( |
| 174 | + 'SELECT * FROM users WHERE name = ? AND email = ?' |
| 175 | +); |
| 176 | + |
| 177 | +$result = $statement->execute(['Alice', 'alice@example.com']); |
| 178 | + |
| 179 | +foreach ($result as $row) { |
| 180 | + echo $row['name'] . PHP_EOL; |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +#### Closing a Prepared Statement |
| 185 | + |
| 186 | +```php |
| 187 | +$statement->close(); |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +### Transactions |
| 193 | + |
| 194 | +Transactions group multiple SQL operations into a single atomic unit. If any operation fails, all changes within the transaction are rolled back, ensuring data consistency. |
| 195 | + |
| 196 | +#### Basic Transaction |
| 197 | + |
| 198 | +```php |
| 199 | +$transaction = $connection->beginTransaction(); |
| 200 | + |
| 201 | +try { |
| 202 | + $transaction->execute( |
| 203 | + "INSERT INTO users (name, email) VALUES ('Charlie', 'charlie@example.com')" |
| 204 | + ); |
| 205 | + $transaction->execute( |
| 206 | + "INSERT INTO users (name, email) VALUES ('Diana', 'diana@example.com')" |
| 207 | + ); |
| 208 | + |
| 209 | + $transaction->commit(); |
| 210 | +} catch (\Throwable $e) { |
| 211 | + $transaction->rollback(); |
| 212 | + |
| 213 | + throw $e; |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +#### Transaction with Prepared Statements |
| 218 | + |
| 219 | +```php |
| 220 | +$transaction = $connection->beginTransaction(); |
| 221 | + |
| 222 | +try { |
| 223 | + $statement = $transaction->prepare( |
| 224 | + 'INSERT INTO users (name, email) VALUES (?, ?)' |
| 225 | + ); |
| 226 | + |
| 227 | + $statement->execute(['Eve', 'eve@example.com']); |
| 228 | + $statement->execute(['Frank', 'frank@example.com']); |
| 229 | + |
| 230 | + $transaction->commit(); |
| 231 | +} catch (\Throwable $e) { |
| 232 | + $transaction->rollback(); |
| 233 | + |
| 234 | + throw $e; |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +## License |
| 241 | + |
| 242 | +This package is open-sourced software licensed under the [MIT](https://opensource.org/licenses/MIT) license. |
0 commit comments