2017 © Pedro Peláez
 

library pdowrapper

A useful PDO wrapper.

image

phputil/pdowrapper

A useful PDO wrapper.

  • Wednesday, May 18, 2016
  • by thiagodp
  • Repository
  • 3 Watchers
  • 1 Stars
  • 71 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

PDOWrapper

A useful PDO wrapper., (*1)

Classes:, (*2)

This project uses semantic version. See our releases., (*3)

Installation

composer require phputil/pdowrapper

Example 1

Creating PDO with PDOBuilder and counting rows with PDOWrapper., (*4)

dsn( 'mysql:dbname=mydb;host=127.0.0.1;' )
    ->username( 'myuser' )
    ->password( 'mypass' )
    ->modeException()
    ->persistent()
    ->mySqlUTF8()
    ->build();

$pdoW = new PDOWrapper( $pdo );

echo 'Table "customer" has ', $pdoW->countRows( 'customer' ), ' rows.';
?>

Example 2

Delete by id, (*5)

$id = $_GET[ 'id' ];
// ... <-- validate $id here
$deleted = $pdoW->deleteWithId( $id, 'customer' );
echo 'Deleted ', $deleted, ' rows.';

Example 3

Paginated query, (*6)

$limit = $_GET[ 'limit' ];
$offset = $_GET[ 'offset' ];
// ... <-- validate $limit and $offset here
// makeLimitOffset returns a SQL clause depending of the used database.
// Currently supports MySQL, PostgreSQL, SQLite, HSQLDB, H2, Firebird, MS SQL Server,
// or an ANSI SQL 2008 database.
$pdoStatement = $pdo->execute( 'SELECT name FROM customer',
    $pdoW->makeLimitOffset( $limit, $offset ) );
echo 'Showing customers from ', $limit, ' to ', $offset, '<br />';
foreach ( $pdoStatement as $customer ) {
    echo $customer[ 'name' ], '<br />';
}

Example 4

Query objects, (*7)

class User {
    private $id;
    private $name;

    function __construct( $id = 0, $name = '' ) {
        $this->id = $id;
        $this->name = $name;
    }

    function getId() { return $this->id; }
    function getName() { return $this->name; }
}

class UserRepositoryInRelationalDatabase {

    private $pdoW;

    function __construct( PDOWrapper $pdoW ) {
        $this->pdoW = $pdoW;
    }

    /**
     * Return all the users, considering a limit and an offset.
     * @return array of User
     */
    function allUsers( $limit = 0, $offset = 0 ) { // throw
        // Paginated query
        $sql = 'SELECT * FROM user' .
            $this->pdoW->makeLimitOffset( $limit, $offset );
        // Call rowToUser to convert each row to a User
        return $this->pdoW->queryObjects( array( $this, 'rowToUser' ), $sql );
    }

    /**
     * Converts a row into a User.
     * @return User
     */
    function rowToUser( array $row ) {
        return new User( $row[ 'id' ], $row[ 'name' ] );
    }
}

$limit = $_GET[ 'limit' ];
$offset = $_GET[ 'offset' ];
// ... <-- validate $limit and $offset here
$repository = new UserRepositoryInRelationalDatabase( $pdoW );
$users = $repository->allUsers( $limit, $offset );
foreach ( $users as $u ) {
    echo 'Name: ', $u->getName(), '<br />';
}

Development

After cloning the repo, run composer install to install the dependencies., (*8)

How to run the test cases:, (*9)

./vendor/bin/phpunit tests

The Versions

18/05 2016

dev-master

9999999-dev http://github.com/thiagodp/pdowrapper

A useful PDO wrapper.

  Sources   Download

LGPL-3

The Requires

  • php >=5.2.0

 

php pdo

18/05 2016

1.0.2

1.0.2.0 http://github.com/thiagodp/pdowrapper

A useful PDO wrapper.

  Sources   Download

LGPL-3

The Requires

  • php >=5.2.0

 

php pdo

23/10 2015

1.0.1

1.0.1.0 http://github.com/thiagodp/pdowrapper

A useful PDO wrapper.

  Sources   Download

LGPL-3

The Requires

  • php >=5.2.0

 

php pdo

22/10 2015

1.0

1.0.0.0 http://github.com/thiagodp/pdowrapper

A useful PDO wrapper.

  Sources   Download

LGPL-3

The Requires

  • php >=5.2.0

 

php pdo