Work on documentation 1
This commit is contained in:
parent
9ddc05dd8a
commit
76c902e742
64
README.md
64
README.md
|
@ -8,35 +8,55 @@ the model level.
|
||||||
By default, foreign keys are enabled and the database is in WAL mode. In general
|
By default, foreign keys are enabled and the database is in WAL mode. In general
|
||||||
these defaults should be perfect for most use cases.
|
these defaults should be perfect for most use cases.
|
||||||
|
|
||||||
## Testing
|
## Usage
|
||||||
I wrote a simple test file; run it with PHP directly. `php test.php`
|
### `__construct(string $path, array $opts = [])`
|
||||||
This test script covers almost every relevant method in the class.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
```php
|
```php
|
||||||
$db = new Database('test.db');
|
$db = new Database('test.db');
|
||||||
|
```
|
||||||
|
This wrapper is non-static; create an instance and pass a path to where the SQLite database will reside. If the database does not already exist, it will be created. You can pass PDO options to the second parameter. By default the error mode is set to Exceptions and the fetch method is set to associative arrays.
|
||||||
|
|
||||||
$db->table('test')->create([
|
The wrapper also enables foreign keys and WAL mode by default, and at the moment this cannot be changed. This configuration should work great for most use-cases anyway, as SQLite itself can handle WAL-related upkeep such as checkpointing.
|
||||||
|
|
||||||
|
### `table(string $name): Database`
|
||||||
|
```php
|
||||||
|
$db->table('foo');
|
||||||
|
```
|
||||||
|
Sets the current working table for the instance. Allows you to switch table contexts for operations. Can method chain from it.
|
||||||
|
Since the current working table is persistent, you do not need to call this method before each operation if you do not want to.
|
||||||
|
|
||||||
|
### `create(array $columns): PDOStatement|false`
|
||||||
|
```php
|
||||||
|
$db->table('foo')->create([
|
||||||
'id INTEGER PRIMARY KEY',
|
'id INTEGER PRIMARY KEY',
|
||||||
'name TEXT NOT NULL',
|
'name TEXT NOT NULL',
|
||||||
'age INTEGER DEFAULT 18'
|
'age INTEGER DEFAULT 18'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$db->table('test')->insert([
|
|
||||||
'name' => 'John',
|
|
||||||
'age' => 32
|
|
||||||
]);
|
|
||||||
|
|
||||||
$db->table('test')->where('name', '=', 'John')->select();
|
|
||||||
$db->table('test')->where('age', '>', 16)->selectAll();
|
|
||||||
|
|
||||||
$db->table('test')->where('name', '=', 'John')->delete();
|
|
||||||
```
|
```
|
||||||
|
Creates a table where the name of the table is the current working directory and the columns are passed via an array. The underlying SQL uses `CREATE TABLE IF NOT EXISTS` and so this will error/not execute if the table already exists.
|
||||||
|
|
||||||
## Notes
|
### `drop(): PDOStatement|false`
|
||||||
`Database->table()` sets the current working table's name; there is no real need to use it for every query.
|
|
||||||
```php
|
```php
|
||||||
$db->table('test');
|
$db->table('foo')->drop();
|
||||||
$db->where('name', '=', 'John')->select();
|
```
|
||||||
$db->where('age', '>', 16)->selectAll('age');
|
Drops the current working table if it exists.
|
||||||
```
|
|
||||||
|
### `schema(): array`
|
||||||
|
```php
|
||||||
|
$db->table('foo')->schema();
|
||||||
|
```
|
||||||
|
Returns a dump from `PRAGMA table_info()` on the current working table.
|
||||||
|
|
||||||
|
### `insert(array $data): int|false`
|
||||||
|
```php
|
||||||
|
$db->table('foo')->insert(['name' => 'John', 'age' => 32]);
|
||||||
|
$db->table('foo')->insert([
|
||||||
|
['name' => 'Smith', 'age' => 22],
|
||||||
|
['name' => 'Jane', 'age' => 18]
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
Inserts a record into the current working table. Can pass an array of arrays to insert multiple records.
|
||||||
|
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
I wrote a simple test file; run it with PHP directly. `php test.php`
|
||||||
|
This test script covers almost every relevant method in the class.
|
Loading…
Reference in New Issue
Block a user