2017 © Pedro Peláez
 

library db

Database Abstraction Layer over DBAL

image

gonzalo123/db

Database Abstraction Layer over DBAL

  • Monday, December 23, 2013
  • by gonzalo123
  • Repository
  • 1 Watchers
  • 7 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

SQL wrapper

Build Status SensioLabsInsight, (*1)

First we set up de DBAL connection, (*2)

use Doctrine\DBAL\DriverManager;

// Set up DBAL Connection
$conn = DriverManager::getConnection([
    'driver' => 'pdo_sqlite',
    'memory' => true
]);

We also can set up the DBAL connection from a PDO instance, (*3)

$conn = DriverManager::getConnection(['pdo' => new PDO('sqlite::memory:')]);

Then we create the database and populate tables with dummy data, (*4)

// init database
$conn->exec("CREATE TABLE users (
            userid VARCHAR PRIMARY KEY  NOT NULL ,
            password VARCHAR NOT NULL ,
            name VARCHAR,
            surname VARCHAR
            );");
$conn->exec("INSERT INTO users VALUES('user','pass','Name','Surname');");
$conn->exec("INSERT INTO users VALUES('user2','pass2','Name2','Surname2');");

Now we can use the library:, (*5)

use G\Sql;
use G\Db;

// setting up G\Db with connection
$db = new Db($conn);

// select from string
$data = $db->select("select * from users");
// select from table
$data = $db->select(SQL::createFromTable("users"));
// select from table with where clause
$data = $db->select(SQL::createFromTable("users", ['userid' => 'user2']));
// iterating select statement changing the recordset
$data = $db->select(SQL::createFromTable("users"), function (&$row) {
    $row['name'] = strtoupper($row['name']);
});
// transactions
$db->transactional(function (Db $db) {
    $userId = 'temporal';

    $db->insert('users', [
        'USERID'   => $userId,
        'PASSWORD' => uniqid(),
        'NAME'     => 'name3',
        'SURNAME'  => 'name3'
    ]);

    $db->update('users', ['NAME' => 'updatedName'], ['USERID' => $userId]);
    $db->delete('users', ['USERID' => $userId]);
});

The Versions

23/12 2013

dev-master

9999999-dev

Database Abstraction Layer over DBAL

  Sources   Download

MIT

The Requires

 

The Development Requires