2017 © Pedro Peláez
 

library aura-sql-module

aura/sql module for Ray.Di

image

ray/aura-sql-module

aura/sql module for Ray.Di

  • Wednesday, July 18, 2018
  • by koriym
  • Repository
  • 4 Watchers
  • 4 Stars
  • 18,371 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 25 Versions
  • 11 % Grown

The README.md

Ray.AuraSqlModule

Scrutinizer Code Quality codecov Type Coverage Continuous Integration, (*1)

An Aura.Sql Module for Ray.Di, (*2)

Installation

Composer install

$ composer require ray/aura-sql-module

Module install

use Ray\Di\AbstractModule;
use Ray\AuraSqlModule\AuraSqlModule;
use Ray\AuraSqlModule\AuraSqlQueryModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(
            new AuraSqlModule(
                'mysql:host=localhost;dbname=test',
                'username',
                'password',
                'slave1,slave2,slave3' // optional slave server list
                $options,              // optional key=>value array of driver-specific connection options
                $queries               // Queries to execute after the connection.
        );
    }
}

Use AuraSqlEnvModule to get the value from the environment variable each time at runtime, instead of specifying the value directly., (*3)

        $this->install(
            new AuraSqlEnvModule(
                'PDO_DSN',             // getenv('PDO_DSN')
                'PDO_USER',            // getenv('PDO_USER')
                'PDO_PASSWORD',        // getenv('PDO_PASSWORD')
                'PDO_SLAVE'            // getenv('PDO_SLAVE')
                $options,              // optional key=>value array of driver-specific connection options
                $queries               // Queries to execute after the connection.
        );

DI trait

  • AuraSqlInject for Aura\Sql\ExtendedPdoInterface interface, (*4)

    Replication

    Installing AuraSqlReplicationModule using a connection locator for master/slave connections., (*5)

    use Ray\Di\AbstractModule;
    use Ray\AuraSqlModule\AuraSqlModule;
    use Ray\AuraSqlModule\Annotation\AuraSqlConfig;
    use Aura\Sql\ConnectionLocator;
    
    class AppModule extends AbstractModule
    {
     protected function configure()
     {
         $locator = new ConnectionLocator;
         $locator->setWrite('master', new Connection('mysql:host=localhost;dbname=master', 'id', 'pass'));
         $locator->setRead('slave1',  new Connection('mysql:host=localhost;dbname=slave1', 'id', 'pass'));
         $locator->setRead('slave2',  new Connection('mysql:host=localhost;dbname=slave2', 'id', 'pass'));
         $this->install(new AuraSqlReplicationModule($locator));
     }
    }
    
    

You will now have a slave db connection when using HTTP GET, or a master db connection in other HTTP methods., (*6)

Multiple DB

You may want to inject different connection destinations on the same DB interface with @Named($qaulifier) annotation. Two modules are provided. NamedPdoModule is for non replication use. and AuraSqlReplicationModule is for replication use., (*7)

#[Inject]
public function setLoggerDb(#[Named('log_db') ExtendedPdoInterface $pdo)
{
    // ...
}

with no replication

Use NamedPdoModule to inject different named Pdo instance for non Replication use. For instance, This module install log_db named Pdo instance., (*8)

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new NamedPdoModule('log_db', 'mysql:host=localhost;dbname=log', 'username', 
    }
}

Or, (*9)

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new NamedPdoEnvModule('log_db', 'LOG_DSN', 'LOG_USERNAME', 
    }
}


### with replication

You can set `$qaulifer` in 2nd parameter of AuraSqlReplicationModule.

```php
class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new AuraSqlReplicationModule($locator, 'log_db'));
    }
}

Transaction

Any method marked with @Transactional will have a transaction started before, and ended after it is called., (*10)


use Ray\AuraSqlModule\Annotation\WriteConnection; // important use Ray\AuraSqlModule\Annotation\Transactional; // important class User { public $pdo; #[WriteConnection, Transactional] public function write() { // $this->pdo->rollback(); when exception thrown. } }

Query Builder

Aura.SqlQuery provides query builders for MySQL, Postgres, SQLite, and Microsoft SQL Server. Following four interfaces are bound and setter trait for them are available., (*11)

QueryBuilder interface, (*12)

  • Aura\SqlQuery\Common\SelectInterface
  • Aura\SqlQuery\Common\InsertInterface
  • Aura\SqlQuery\Common\UpdateInterface
  • Aura\SqlQuery\Common\DeleteInterface

QueryBuilder setter trait, (*13)

  • Ray\AuraSqlModule\AuraSqlSelectInject
  • Ray\AuraSqlModule\AuraSqlInsertInject
  • Ray\AuraSqlModule\AuraSqlUpdateInject
  • Ray\AuraSqlModule\AuraSqlDeleteInject
use Ray\AuraSqlModule\AuraSqlSelectInject;
clas Foo
{
    use AuraSqlSelectInject;

    public function bar()
    {
        /* @var $select \Aura\SqlQuery\Common\SelectInterface */
        $this->select // 
            ->distinct()                    // SELECT DISTINCT
            ->cols(array(                   // select these columns
                'id',                       // column name
                'name AS namecol',          // one way of aliasing
                'col_name' => 'col_alias',  // another way of aliasing
                'COUNT(foo) AS foo_count'   // embed calculations directly
            ))
            ->from('foo AS f');              // FROM these tables
        $sth = $this->pdo->prepare($this->select->getStatement());
        // bind the values and execute
        $sth->execute($this->select->getBindValues());
        // get the results back as an associative array
        $result = $sth->fetch(PDO::FETCH_ASSOC);
         = $sth->fetch(PDO::FETCH_ASSOC);

Pagination

Pagination service is provided for both ExtendedPdo raw sql and Select query builder., (*14)

ExtendedPdo, (*15)


use Ray\AuraSqlModule\AuraSqlPagerInject; class Foo { use AuraSqlPagerInject; publuc function bar() { // ... $pager = $this->pagerFactory->newInstance($pdo, $sql, $params, 10, '/?page={page}&category=sports'); // 10 items per page $page = $pager[2]; // page 2

Select query builder, (*16)

use Ray\AuraSqlModule\Pagerfanta\AuraSqlQueryPagerInject;

class Foo
{
    use AuraSqlQueryPagerInject;

    publuc function bar()
    {
        // ...     
        $pager = $this->queryPagerFactory->newInstance($pdo, $select, 10, '/?page={page}&category=sports');
        $page = $pager[2]; // page 2

An array access with page number returns Page value object., (*17)

/* @var Pager \Ray\AuraSqlModule\Pagerfanta\Page */

// $page->data // sliced data
// $page->current;
// $page->total
// $page->hasNext
// $page->hasPrevious
// $page->maxPerPage;
// (string) $page // pager html

It is iteratable., (*18)

foreach ($page as $item) {
 // ...

View

The view template can be changed with binding. See more at Pagerfanta., (*19)

use Pagerfanta\View\Template\TemplateInterface;
use Pagerfanta\View\Template\TwitterBootstrap3Template;
use Ray\AuraSqlModule\Annotation\PagerViewOption;

$this->bind(TemplateInterface::class)->to(TwitterBootstrap3Template::class);
$this->bind()->annotatedWith(PagerViewOption::class)->toInstance($pagerViewOption);

Profile

To log SQL execution, install AuraSqlProfileModule. It will be logged by a logger bound to the PSR-3 logger. This example binds a minimal function logger created in an unnamed class., (*20)

class DevModule extends AbstractModule
{
    protected function configure()
    {
        // ...
        $this->install(new AuraSqlProfileModule());
        $this->bind(LoggerInterface::class)->toInstance(
            /** 
            new class extends AbstractLogger {
                /** @inheritDoc */
                public function log($level, $message, array $context = [])
                {
                    $replace = [];
                    foreach ($context as $key => $val) {
                        if (! is_array($val) && (! is_object($val) || method_exists($val, '__toString'))) {
                            $replace['{' . $key . '}'] = $val;
                        }
                    }

                    error_log(strtr($message, $replace));
                }
            }
        );
}

The Versions

18/07 2018

1.x-dev

1.9999999.9999999.9999999-dev

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

pdo aura sql ray module

21/05 2018

1.6.5

1.6.5.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

pdo aura sql ray module

21/05 2018

1.6.4

1.6.4.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

pdo aura sql ray module

04/09 2017

1.6.3

1.6.3.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

pdo aura sql ray module

04/08 2017

1.6.2

1.6.2.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

pdo aura sql ray module

10/04 2017

1.6.1

1.6.1.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

pdo aura sql ray module

07/03 2017

1.6.0

1.6.0.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

22/02 2017

1.5.0

1.5.0.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

11/09 2016

1.4.6

1.4.6.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

10/08 2016

1.4.5

1.4.5.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

10/08 2016

1.4.4

1.4.4.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

11/07 2016

1.4.3

1.4.3.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

28/02 2016

1.4.2

1.4.2.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

21/02 2016

1.4.1

1.4.1.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

21/02 2016

1.4.0

1.4.0.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

15/02 2016

1.3.0

1.3.0.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

10/12 2015

1.2.2

1.2.2.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

02/11 2015

1.2.1

1.2.1.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

22/10 2015

1.2.0

1.2.0.0

aura/sql module for Ray.Di

  Sources   Download

MIT

The Requires

 

pdo aura sql ray module

22/04 2015

1.1.1

1.1.1.0

aura/sql module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

pdo aura sql ray module

25/03 2015

1.1.0

1.1.0.0

aura/sql module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

pdo aura sql ray module

06/03 2015

1.0.3

1.0.3.0

aura/sql module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

pdo aura sql ray module

18/02 2015

1.0.2

1.0.2.0

aura/sql module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

pdo aura sql ray module

14/02 2015

1.0.1

1.0.1.0

aura/sql module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

pdo aura sql ray module

24/12 2014

1.0.0

1.0.0.0

aura/sql module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

pdo aura sql ray module