An open API service indexing awesome lists of open source software.

https://github.com/sinri/sinri-database-agent

Provide a old-style alike quick SQL handler for PHP
https://github.com/sinri/sinri-database-agent

Last synced: 8 months ago
JSON representation

Provide a old-style alike quick SQL handler for PHP

Awesome Lists containing this project

README

          

# SinriDatabaseAgent

![Packagist Version Badge](https://img.shields.io/packagist/v/sinri/sinri-database-agent.svg)
![Packagist](https://img.shields.io/packagist/dt/sinri/sinri-database-agent.svg)
![Packagist](https://img.shields.io/packagist/l/sinri/sinri-database-agent.svg)

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

1. Include the `autoload.php` into your PHP project.
2. Declare what you need for convenience.

```PHP
// PDO
use sinri\SinriDatabaseAgent\SinriPDO;
$db=new SinriPDO($params);
// MySQLi
use sinri\SinriDatabaseAgent\SinriMySQLi;
$db=new SinriMySQLi($params);
```

3. 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

```PHP
$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.

```PHP
$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.

```PHP
$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 if `phpcs` and `phpcbf` is correctly installed.
* Run `php test/test.php` to do Unit Test after correcting `test\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.