SinriDatabaseAgent
Provide a old-style alike quick SQL handler for PHP. With this toolkit developers could be free from writing fundamental code for database.
Composer
composer require sinri/sinri-database-agent
Usage
- Include the
autoload.php
into your PHP project. - Declare what you need for convenience.
// PDO
use sinri\SinriDatabaseAgent\SinriPDO;
$db=new SinriPDO($params);
// MySQLi
use sinri\SinriDatabaseAgent\SinriMySQLi;
$db=new SinriMySQLi($params);
- General Method
If the following SQL
SELECT X,Y FROM T WHERE KEY>0;
refers to this records:
KEY | X | Y |
1 | x1 | y1 |
2 | x2 | y2 |
$sql="SELECT X,Y FROM T WHERE KEY>0;";
$db->getAll($sql);
// return [["X"=>"x1","Y"=>"y1"],["X"=>"x2","Y"=>"y2"]]
$db->getRow($sql);
// return ["X"=>"x1","Y"=>"y1"]
$db->getCol($sql);
// return ["x1","x2"]
$db->getOne($sql);
// return "x1"
If you need to do some modification.
$sql="INSERT INTO...";
$db->insert($sql);
// return the last inserted record's PK value.
$sql="UPDATE ...";// or DELETE, etc.
$db->exec($sql);
// return affected row count, or FALSE on failure.
You may want to use transactions.
$db->beginTransaction();
// do some work
$db->inTransaction();
// return if this line is in transaction
if($has_error){
$db->rollBack();
}else{
$db->commit();
}
For Developers
- Use Code Rule
PSR2
, just run./PSR2.sh
before commit and push ifphpcs
andphpcbf
is correctly installed. - Run
php test/test.php
to do Unit Test after correctingtest\config.php
settings.
Check
phpcs --report=full --standard=PSR2 --ignore=vendor .
phpcs --report=summary --standard=PSR2 --ignore=vendor .
Correct
phpcbf --report=full --standard=PSR2 --ignore=vendor .
License
SinriDatabaseAgent is published under MIT License.