2014-12-24 03:28:26 +01:00
|
|
|
PicoDb
|
|
|
|
======
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
PicoDb is a minimalist database query builder for PHP.
|
2014-12-24 03:28:26 +01:00
|
|
|
**It's not an ORM**.
|
|
|
|
|
|
|
|
Features
|
|
|
|
--------
|
|
|
|
|
|
|
|
- No dependency
|
|
|
|
- Easy to use, fast and very lightweight
|
|
|
|
- Use prepared statements
|
|
|
|
- Handle schema versions (migrations)
|
|
|
|
- License: [WTFPL](http://www.wtfpl.net)
|
|
|
|
|
|
|
|
Requirements
|
|
|
|
------------
|
|
|
|
|
|
|
|
- PHP >= 5.3
|
|
|
|
- PDO
|
|
|
|
- A database: Sqlite, Mysql or Postgresql
|
|
|
|
|
|
|
|
Documentation
|
|
|
|
-------------
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Installation
|
|
|
|
|
|
|
|
```bash
|
|
|
|
composer require fguillot/picodb dev-master
|
|
|
|
```
|
|
|
|
|
|
|
|
### Connect to your database
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
use PicoDb\Database;
|
|
|
|
|
|
|
|
// Sqlite driver
|
|
|
|
$db = new Database(['driver' => 'sqlite', 'filename' => ':memory:']);
|
|
|
|
|
|
|
|
// Mysql driver
|
|
|
|
// Optional options: "schema_table" (the default table name is "schema_version")
|
|
|
|
$db = new Database(array(
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'hostname' => 'localhost',
|
|
|
|
'username' => 'root',
|
|
|
|
'password' => '',
|
|
|
|
'database' => 'my_db_name',
|
|
|
|
'charset' => 'utf8',
|
|
|
|
));
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Execute a SQL request
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->execute('CREATE TABLE toto (column1 TEXT)');
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Insert some data
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')->save(['column1' => 'hey']);
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Transations
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->transaction(function($db) {
|
|
|
|
$db->table('toto')->save(['column1' => 'foo']);
|
|
|
|
$db->table('toto')->save(['column1' => 'bar']);
|
|
|
|
});
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Fetch all data
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$records = $db->table('toto')->findAll();
|
|
|
|
|
|
|
|
foreach ($records as $record) {
|
|
|
|
var_dump($record['column1']);
|
|
|
|
}
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Update something
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
$db->table('toto')->eq('id', 1)->save(['column1' => 'hey']);
|
|
|
|
|
|
|
|
You just need to add a condition to perform an update.
|
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Remove rows
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')->lowerThan('column1', 10)->remove();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Sorting
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')->asc('column1')->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')->desc('column1')->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Limit and offset
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')->limit(10)->offset(5)->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Fetch only some columns
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')->columns('column1', 'column2')->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Equals condition
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->equals('column1', 'hey')
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->eq('column1', 'hey')
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
Yout got: 'SELECT * FROM toto WHERE column1=?'
|
|
|
|
|
|
|
|
### IN condition
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->in('column1', ['hey', 'bla'])
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Like condition
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
Case-sensitive (only Mysql and Postgres):
|
|
|
|
|
|
|
|
```php
|
|
|
|
$db->table('toto')
|
|
|
|
->like('column1', '%Foo%')
|
|
|
|
->findAll();
|
|
|
|
```
|
|
|
|
|
|
|
|
Not case-sensitive:
|
|
|
|
|
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
2014-12-29 22:52:36 +01:00
|
|
|
->ilike('column1', '%foo%')
|
2014-12-24 03:28:26 +01:00
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Lower than
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->lowerThan('column1', 2)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->lt('column1', 2)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Lower than or equals
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->lowerThanOrEquals('column1', 2)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->lte('column1', 2)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Greater than
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->greaterThan('column1', 3)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->gt('column1', 3)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Greater than or equals
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->greaterThanOrEquals('column1', 3)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->gte('column1', 3)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Multiple conditions
|
|
|
|
|
|
|
|
Each condition is joined by a AND.
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->like('column2', '%toto')
|
|
|
|
->gte('column1', 3)
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
How to make a OR condition:
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$db->table('toto')
|
|
|
|
->beginOr()
|
|
|
|
->like('column2', '%toto')
|
|
|
|
->gte('column1', 3)
|
|
|
|
->closeOr()
|
|
|
|
->eq('column5', 'titi')
|
|
|
|
->findAll();
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
### Schema migrations
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
#### Define a migration
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
- Migrations are defined in simple functions inside a namespace named "Schema".
|
|
|
|
- An instance of PDO is passed to first argument of the function.
|
|
|
|
- Function names has the version number at the end.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace Schema;
|
|
|
|
|
|
|
|
function version_1($pdo)
|
|
|
|
{
|
|
|
|
$pdo->exec('
|
|
|
|
CREATE TABLE users (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name TEXT UNIQUE,
|
|
|
|
email TEXT UNIQUE,
|
|
|
|
password TEXT
|
|
|
|
)
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function version_2($pdo)
|
|
|
|
{
|
|
|
|
$pdo->exec('
|
|
|
|
CREATE TABLE tags (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name TEXT UNIQUE
|
|
|
|
)
|
|
|
|
');
|
|
|
|
}
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2015-01-07 01:08:10 +01:00
|
|
|
#### Run schema update automatically
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
- The method "check()" executes all migrations until to reach the correct version number.
|
|
|
|
- If we are already on the last version nothing will happen.
|
|
|
|
- The schema version for the driver Sqlite is stored inside a variable (PRAGMA user_version)
|
|
|
|
- You can use that with a dependency injection controller.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
$last_schema_version = 5;
|
|
|
|
|
|
|
|
$db = new PicoDb\Database(array(
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
'filename' => '/tmp/mydb.sqlite'
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($db->schema()->check($last_schema_version)) {
|
|
|
|
|
|
|
|
// Do something...
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
die('Unable to migrate database schema.');
|
|
|
|
}
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
### Use a singleton to handle database instances
|
|
|
|
|
|
|
|
Setup a new instance:
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
PicoDb\Database::bootstrap('myinstance', function() {
|
|
|
|
|
|
|
|
$db = new PicoDb\Database(array(
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
'filename' => DB_FILENAME
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($db->schema()->check(DB_VERSION)) {
|
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die('Unable to migrate database schema.');
|
|
|
|
}
|
|
|
|
});
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
Get this instance anywhere in your code:
|
|
|
|
|
2014-12-29 22:52:36 +01:00
|
|
|
```php
|
2014-12-24 03:28:26 +01:00
|
|
|
PicoDb\Database::get('myinstance')->table(...)
|
2014-12-29 22:52:36 +01:00
|
|
|
```
|