JAQB
, (*1)
JAQB: Just Another Query Builder for PHP (pronounced "jacob"), (*2)
Requirements
Installation
The easiest way to install JAQB is with composer:, (*3)
composer require jaqb/jaqb
Configuration
The connection manager accepts an array of connection configurations. With the connection manager you can conveniently manage one or more database connections. Connections are referenced by a unique ID in the configuration., (*4)
use JAQB\ConnectionManager;
$config = [
'main' => [
'type' => 'mysql',
'host' => 'localhost',
'name' => 'dbname'
'username' => 'myuser',
'password' => 'mypassword',
'options' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]
],
'sqlite' => [
'dsn' => 'sqlite:mydb.sqlite'
]
];
$manager = new ConnectionManager($config);
Supplying an existing PDO object
You can also manually add existing PDO connections., (*5)
use JAQB\ConnectionManager;
use JAQB\QueryBuilder;
$pdo = new PDO('...');
$connection = new QueryBuilder($pdo);
$manager = new ConnectionManager();
$manager->add('users', $connection);
Usage
Retrieving a connection
You can retrieve a connection by calling get()
on the connection manager with the ID of the connection., (*6)
$db = $manager->get('main');
If there is only one connection then you can also get it as the default., (*7)
$db = $manager->getDefault();
Select Query
$db->select('*')
->from('Movies')
->join('Directors', 'Directors.id = Movies.director_id')
->where('Directors.name', 'Quentin Tarantino')
->between('year', 1990, 2015)
->groupBy('category')
->having('rating', 4.5, '>')
->orderBy('rating', 'DESC')
->limit(100, 10)
->all();
Insert Query
$db->insert(['name' => 'Catcher in the Rye', 'author' => 'JD Salinger'])
->into('Books')
->execute();
Update Query
$db->update('Users')
->where('uid', 10)
->values(['first_name' => 'JAQB', 'website' => 'example.com'])
->orderBy('uid', 'ASC')
->limit(100)
->execute();
Delete Query
$db->delete('Users')
->where('last_login', strtotime('-1 year'), '<')
->limit(100)
->orderBy('last_login', 'ASC')
->execute();
Pure SQL Query
$db->raw('SHOW COLUMNS FROM `Events`')
->execute();
Executing a Query
The following methods can be used to execute a query and retrieve results:
- execute()
- returns a PDOStatement
- all()
- returns all of the rows
- one()
- returns the first row
- column($index = 0)
- returns a specific column from each row
- scalar($index = 0)
- returns a specific column from the first row, (*8)
Also:
- rowCount()
- returns the number of rows affected by the last executed statement
- $db->lastInsertId()
- returns the last inserted ID, (*9)
Transactions
Queries support PDO transactions., (*10)
$db->beginTransaction();
try {
for ($i = 1; $i <= 10; $i++) {
$db->insert(['name' => "Item # $i"])
->into('Items')
->execute();
}
$db->commit();
} catch (Exception $e) {
$db->rollBack();
}
Building a Query
If you want to build a query without executing it just use build()
instead. getValues()
will retrieve any ordered question mark parameters., (*11)
Tests
Use phpunit to run the included tests:, (*12)
phpunit
Contributing
Please feel free to contribute by participating in the issues or by submitting a pull request. :-), (*13)
License
The MIT License (MIT), (*14)
Copyright © 2015 Jared King, (*15)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*16)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*17)
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*18)