v-dem/queasy-db
QuEasy DB is a set of database access classes for CRUD operations.
Some of the most usual queries can be built automatically (like SELECT
by unique field value/values, UPDATE
, INSERT
and DELETE
).
More complex queries can be defined in database and/or tables config.
The main goal is to separate SQL
queries out of PHP
code and provide an easy way for CRUD operations.
PDO
class, so any project which uses PDO
can be seamlessly moved to use QuEasy DB.composer require v-dem/queasy-db
It will also install v-dem/queasy-helper
.
setLogger()
method which accepts Psr\Log\LoggerInterface
implementation to log all queries, by default Psr\Log\NullLogger
is used.PDO::ERRMODE_EXCEPTION
(as in PHP8) if another mode is not set in $options
.PDO::MYSQL_ATTR_INIT_COMMAND
to SET GLOBAL SQL_MODE=ANSI_QUOTES
or run same query before calling DB-specific methods.SET QUOTED_IDENTIFIER ON
or SET ANSI_DEFAULTS ON
query before calling DB-specific methods.Sample:
$db = new queasy\db\Db(
[
'dsn' => 'pgsql:host=localhost;dbname=test',
'user' => 'test_user',
'password' => 'test_password',
'options' => [
...options...
]
]
);
Or:
$db = new queasy\db\Db(
[
'connection' => [
'dsn' => 'pgsql:host=localhost;dbname=test',
'user' => 'test_user',
'password' => 'test_password',
'options' => [
...options...
]
]
]
);
Or PDO-way:
$db = new queasy\db\Db('pgsql:host=localhost;dbname=test', 'test_user', 'test_password', $options);
$options
) is optional, will be passed to PDO::prepare()
users
table$users = $db->users->all();
foreach
with users
tableforeach ($db->users as $user) {
// Do something
}
users
table by id
key$user = $db->users->id[$userId];
It's possible to use select()
method to pass PDO options; select()
returns PDOStatement instance:
$users = $db->users->id->select($userId, $options);
$users = $db->users->id[[$userId1, $userId2]];
users
table using associative array$db->users[] = [
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
];
users
table by fields order$db->users[] = [
'john.doe@example.com',
sha1('myverystrongpassword')
];
users
table using associative array (it will generate single INSERT
statement)$db->users[] = [
[
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
], [
'email' => 'mary.joe@example.com',
'password_hash' => sha1('herverystrongpassword')
]
];
users
table by order$db->users[] = [
[
'john.doe@example.com',
sha1('myverystrongpassword')
], [
'mary.joe@example.com',
sha1('herverystrongpassword')
]
];
users
table with field names denoted separately$db->users[] = [
[
'email',
'password_hash'
], [
[
'john.doe@example.com',
sha1('myverystrongpassword')
], [
'mary.joe@example.com',
sha1('herverystrongpassword')
]
]
];
Also it's possible to use insert()
method (in the same way as above) when need to pass PDO options; returns last insert id for single insert and number of inserted rows for multiple inserts:
$userId = $db->users->insert([
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
], $options);
$insertedRowsCount = $db->users->insert([
[
'email' => 'john.doe@example.com',
'password_hash' => sha1('myverystrongpassword')
], [
'email' => 'mary.joe@example.com',
'password_hash' => sha1('herverystrongpassword')
]
], $options);
$options
) is optional, will be passed to PDO::prepare()
lastInsertId()
method)$newUserId = $db->id();
users
table by id
key$db->users->id[$userId] = [
'password_hash' => sha1('mynewverystrongpassword')
]
$updatedRowsCount = $db->users->id->update($userId, [
'password_hash' => sha1('mynewverystrongpassword')
], $options);
$options
) is optional, will be passed to PDO::prepare()
$db->users->id[[$userId1, $userId2]] = [
'is_blocked' => true
]
users
table by id
keyunset($db->users->id[$userId]);
unset($db->users->id[[$userId1, $userId2]]);
$deletedRowsCount = $db->users->id->delete([[$userId1, $userId2]], $options);
$options
) is optional, will be passed to PDO::prepare()
users
table$usersCount = count($db->users);
$db->trans(function() use($db) {
// Run queries inside a transaction, for example:
$db->users[] = [
'john.doe@example.com',
sha1('myverystrongpassword')
];
});
PDOStatement
)$users = $db->run('
SELECT *
FROM "users"
WHERE "name" LIKE concat(\'%\', :searchName, \'%\')',
[
':searchName' => 'John'
],
$options
)->fetchAll();
$options
) is optional, will be passed to PDO::prepare()
This feature can help keep code cleaner and place SQL code outside PHP, somewhere in config files.
$db = new queasy\db\Db(
[
'connection' => [
'dsn' => 'pgsql:host=localhost;dbname=test',
'user' => 'test_user',
'password' => 'test_password'
],
'queries' => [
'searchUsersByName' => [
'sql' => '
SELECT *
FROM "users"
WHERE "name" LIKE concat(\'%\', :searchName, \'%\')',
'returns' => Db::RETURN_ALL
]
]
]
);
$users = $db->searchUsersByName([
'searchName' => 'John'
]);
returns
option are Db::RETURN_STATEMENT
(default, returns PDOStatement
instance), Db::RETURN_ONE
, Db::RETURN_ALL
, Db::RETURN_VALUE
Also it is possible to group predefined queries by tables:
$db = new queasy\db\Db(
[
'connection' => [
'dsn' => 'pgsql:host=localhost;dbname=test',
'user' => 'test_user',
'password' => 'test_password'
],
'tables' => [
'users' => [
'searchByName' => [
'sql' => '
SELECT *
FROM "user_roles"
WHERE "name" LIKE concat(\'%\', :searchName, \'%\')',
'returns' => Db::RETURN_ALL
]
]
]
]
);
$users = $db->users->searchByName([
'searchName' => 'John'
]);
v-dem/queasy-db
together with v-dem/queasy-config
and v-dem/queasy-log
config.php:
return [
'db' => [
'connection' => [
'dsn' => 'pgsql:host=localhost;dbname=test',
'user' => 'test_user',
'password' => 'test_password'
],
'tables' => [
'users' => [
'searchByName' => [
'sql' => '
SELECT *
FROM "users"
WHERE "name" LIKE concat(\'%\', :searchName, \'%\')',
'returns' => Db::RETURN_ALL
]
]
]
],
'logger' => [
[
'class' => queasy\log\ConsoleLogger::class,
'minLevel' => Psr\Log\LogLevel::DEBUG
]
]
];
Initializing:
$config = new queasy\config\Config('config.php'); // Can be also INI, JSON or XML
$logger = new queasy\log\Logger($config->logger);
$db = new queasy\db\Db($config->db);
$db->setLogger($logger);
$users = $db->users->searchByName([
'searchName' => 'John'
]);
Psr\Log\LogLevel::DEBUG
level. Also it's possible to use any other logger class compatible with PSR-3.