jaqb / jaqb
Just Another Query Builder built on PDO
Installs: 40 850
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=5.6.0
Requires (Dev)
- infuse/infuse: ~1.4
- mockery/mockery: ~1.0
- phpunit/phpunit: 5.7.*
- satooshi/php-coveralls: 1.0.0
README
JAQB: Just Another Query Builder for PHP (pronounced "jacob")
Requirements
- PHP 5.6+, PHP 7+
- PDO
Installation
The easiest way to install JAQB is with composer:
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.
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.
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.
$db = $manager->get('main');
If there is only one connection then you can also get it as the default.
$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 aPDOStatement
all()
- returns all of the rowsone()
- returns the first rowcolumn($index = 0)
- returns a specific column from each rowscalar($index = 0)
- returns a specific column from the first row
Also:
rowCount()
- returns the number of rows affected by the last executed statement$db->lastInsertId()
- returns the last inserted ID
Transactions
Queries support PDO transactions.
$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.
Tests
Use phpunit to run the included tests:
phpunit
Contributing
Please feel free to contribute by participating in the issues or by submitting a pull request. :-)
License
The MIT License (MIT)
Copyright © 2015 Jared King
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.