2017 © Pedro Peláez
 

library lib-database

Ride library for database abstraction.

image

ride/lib-database

Ride library for database abstraction.

  • Thursday, August 31, 2017
  • by ride-user
  • Repository
  • 7 Watchers
  • 0 Stars
  • 3,157 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 2 % Grown

The README.md

Ride: Database Library

Database abstraction library of the PHP Ride framework., (*1)

Code Sample

Check this code sample to see some of the possibilities of this library:, (*2)

<?php

use ride\library\database\driver\Driver;
use ride\library\database\exception\DatabaseException;
use ride\library\database\manipulation\condition\SimpleCondition;
use ride\library\database\manipulation\expression\FieldExpression;
use ride\library\database\manipulation\expression\ScalarExpression;
use ride\library\database\manipulation\expression\TableExpression;
use ride\library\database\manipulation\statement\SelectStatement;
use ride\library\database\DatabaseManager;
use ride\library\database\Dsn;
use ride\library\log\Log;

function createDatabaseManager(Log $log) {
    $databaseManager = new DatabaseManager();
    $databaseManager->setLog($log);
    $databaseManager->registerDriver('mysql', 'ride\\library\\database\\driver\\PdoDriver');
    $databaseManager->registerDriver('sqlite', 'ride\\library\\database\\driver\\PdoDriver');
    $databaseManager->registerDriver('postgres', 'ride\\library\\database\\driver\\PostgresPdoDriver');

    $databaseManager->registerConnection('my-database', new Dsn('mysql://user:pass@host/database'));
    $databaseManager->registerConnection('my-2nd-database', new Dsn('sqlite:///path/to/file'));

    return $databaseManager;
}

function getConnection(DatabaseManager $databaseManager) {
    $connections = $databaseManager->getConnections();

    // get the default connection
    $connection = $databaseManager->getConnection();

    if ($databaseManager->hasConnection('my-database')) {
        // get my connection
        $connection = $databaseManager->getConnection('my-database');

        // get my connection but don't connect just yet
        $connection = $databaseManager->getConnection('my-database', false);
    }

    if (!$connection->isConnected()) {
        $connection->connect();
    }

    return $connection;
}

function executeSelectSql(Driver $connection) {
    $sql = 
        'SELECT ' . $connection->quoteIdentifier('id') . ' ' . 
        'FROM ' . $connection->quoteIdentifier('MyTable') . ' ' .
        'WHERE ' . $connection->quoteIdentifier('name') . ' LIKE ' . $connection->quoteValue('%Ride%');

    $result = $connection->execute($sql);

    // get the columns or the column count
    $columns = $result->getColumns();
    $columnCount = $result->getColumnCount();

    // same for rows
    $rows = $result->getRows(); 
    $rowCount = $result->getRowCount();

    // get the first or the last row
    $firstRow = $result->getFirst();
    $lastRow = $result->getLast();

    // you can loop the result straight
    foreach ($result as $row) {
        echo $row['id'];
    }
}

function executeInsertSql(Driver $connection) {
    try {
        $connection->beginTransaction();

        $sql = 
            'INSERT INTO ' . $connection->quoteIdentifier('MyTable') . ' ' .
            'VALUES (' . $connection->quoteValue('My name') . ')';

        $result = $connection->execute($sql);

        if ($connection->isTransactionStarted()) {
            $connection->commitTransaction();
        } 

        $connection->getLastInsertId();
    } catch (DatabaseException $exception) {
        $connection->rollback();

        throw $exception;
    }
}

function executeStatement(Driver $connection) {
    $statement = new SelectStatement(new FieldExpression('id'));
    $statement->addField($field);
    $statement->addTable(new TableExpression('MyTable'));
    $statement->addCondition(new SimpleCondition(new FieldExpression('name'), new ScalarExpression('%Ride%'), '='));

    $result = $connection->executeStatement($statement);
}

Installation

You can use Composer to install this library., (*3)

composer require ride/lib-database

The Versions

31/08 2017

dev-master

9999999-dev

Ride library for database abstraction.

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

31/08 2017

dev-develop

dev-develop

Ride library for database abstraction.

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

31/08 2017

1.0.1

1.0.1.0

Ride library for database abstraction.

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

13/10 2016

1.0.0

1.0.0.0

Ride library for database abstraction.

  Sources   Download

MIT

The Requires

 

by Joris Vandeweerd

24/05 2016

0.1.5

0.1.5.0

Ride library for database abstraction.

  Sources   Download

The Requires

 

by Joris Vandeweerd

04/06 2015

0.1.4

0.1.4.0

Ride library for database abstraction.

  Sources   Download

The Requires

 

by Joris Vandeweerd

13/02 2015

0.1.3

0.1.3.0

Ride library for database abstraction.

  Sources   Download

The Requires

 

by Joris Vandeweerd

21/11 2014

0.1.2

0.1.2.0

Ride library for database abstraction.

  Sources   Download

The Requires

 

by Joris Vandeweerd

06/06 2014

0.1.1

0.1.1.0

Ride library for database abstraction.

  Sources   Download

The Requires

 

by Joris Vandeweerd

14/01 2014

0.1.0

0.1.0.0

Pallo library for database abstraction.

  Sources   Download

The Requires

 

by Joris Vandeweerd