/power-pdo

Powerful and thin wrapper of PDO

Primary LanguagePHPMIT LicenseMIT

Powerful and thin wrapper of PDO

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

Powerful and thin wrapper of PDO

Feature

  • Fluent Query Builder
  • Supports POPO(Plain Old Php Object) entity class
  • Supports SQL JOIN
  • PSR-3 Logger

How to use

Entity class

class UserEntity
{
    public $ID;
    public $deleted;
    public $user_name;
    public $nickname;
    public $email;
}

For MySQL

use Stk2k\PowerPDO\PowerPDO;

$dsn = 'mysql:dbname=mydatabase;host=localhost';
$user = 'myuser';
$password = 'mypass';

$ppdo = new PowerPDO(new PDO($dsn, $user, $password));
or 
$ppdo = PowerPDO::make($dsn, $user, $password);

For SQLite

use Stk2k\PowerPDO\PowerPDO;

$dsn = 'sqlite:/path/to/dbfile_of_sqlite';
$ppdo = new PowerPDO(new PDO($dsn));
or 
$ppdo = PowerPDO::make($dsn);

Logging(PSR-3 Logger)

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// monolog
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

$ppdo = new PowerPDO(new PDO($dsn), $log);
or 
$ppdo = PowerPDO::make($dsn)->logger($log);

SELECT

// array style
$users = PowerPDO::make($dsn)
    ->select("ID, user_name, nickname, email")
    ->from("users")
    ->where("deleted = 0")
    ->getAll();
foreach($users as $u){
    $uid = $u['ID'];
    $name = $u['user_name'];
    echo "[$uid]$name";
}

// entity style
$users = PowerPDO::make($dsn)
    ->select("ID, user_name, nickname, email")
    ->from("users")
    ->where("deleted = 0")
    ->getAll(UserEntity::class);

foreach($users as $u){
    $uid = $u->ID;
    $name = $u->user_name;
    echo "[$uid]$name";
}

Placeholders(Prepared statement)

$users = PowerPDO::make($dsn)
    ->select("ID, user_name, nickname, email")
    ->from("users")
    ->where("nickname LIKE :nickname")
    ->bind(['nickname' => '%Bi%'])
    ->getAll();

Count

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$users = PowerPDO::make($dsn)
    ->count()
    ->from("users")
    ->where("deleted = 0")
    ->get();

Transaction

use Stk2k\PowerPDO\Transaction;
use Stk2k\PowerPDO\exception\TransactionException;

try{
    $tr = new Transaction($pdo);
    $tr->begin();
    // execute UPDATE/DELETE/INSERT SQL here
    $tr->commit();
}
catch(TransactionException $ex){
    $tr->rollback();
}

INSERT

// array style
PowerPDO::make($dsn)
    ->insert()
    ->into("users", "ID, user_name, nickname, email")
    ->values(
        "ID" => "123",
        "user_name" => "hanako", 
        "nickname" => "hana", 
        "email" => "hanako@sample.com"
    )
    ->execute();

// entity style
$new_user = new UserEntity();

$new_user->ID = 123;
$new_user->user_name = 'hanako';
$new_user->nickname = 'hana';
$new_user->email = 'hanako@sample.com';

PowerPDO::make($dsn)
    ->insert()
    ->into("users")
    ->values($new_user)
    ->execute();

UPDATE

// literal style
PowerPDO::make($dsn)
    ->update("users")
    ->set("user_name", "hanako2")
    ->set("email", "hanako2@sample.com")
    ->where("ID = :ID", ['ID'=>1])
    ->execute();

// array style
PowerPDO::make($dsn)
    ->update("users")
    ->values([
            'user_name' => 'hanako2',
            'email' => 'hanako2@sample.com',
        ])
    ->where("ID = :ID", ['ID'=>1])
    ->execute();

// entity style
$new_user = new UserEntity();

$new_user->user_name = 'hanako2';
$new_user->nickname = 'hana2';
$new_user->email = 'hanako2@sample.com';

PowerPDO::make($dsn)
    ->update("users")
    ->values($new_user)
    ->where("ID = :ID", ['ID'=>1])
    ->execute();

DELETE

use Stk2k\PowerPDO\PowerPDO;

// literal style
PowerPDO::make($dsn)
    ->delete()
    ->from("users")
    ->where("email = 'hanako2@sample.com'")
    ->execute();

// placeholder style
PowerPDO::make($dsn)
    ->delete()
    ->from("users")
    ->where("email = :email",['email' => 'hanako2@sample.com'])
    ->execute();

Requirement

PHP 7.3 or later

Installing stk2k/power-pdo

The recommended way to install stk2k/power-pdo is through Composer.

composer require stk2k/power-pdo

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

This library is licensed under the MIT license.

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.