All Projects → ray-di → Ray.aurasqlmodule

ray-di / Ray.aurasqlmodule

Licence: bsd-3-clause
Aura.Sql module for Ray.Di

Projects that are alternatives of or similar to Ray.aurasqlmodule

Beekeeper Studio
Modern and easy to use SQL client for MySQL, Postgres, SQLite, SQL Server, and more. Linux, MacOS, and Windows.
Stars: ✭ 8,053 (+160960%)
Mutual labels:  sql, database
Godb
A Go SQL query builder and struct mapper.
Stars: ✭ 651 (+12920%)
Mutual labels:  sql, database
Manticoresearch
Database for search
Stars: ✭ 610 (+12100%)
Mutual labels:  sql, database
Yugabyte Db
The high-performance distributed SQL database for global, internet-scale apps.
Stars: ✭ 5,890 (+117700%)
Mutual labels:  sql, database
Db Dumper
Dump the contents of a database
Stars: ✭ 744 (+14780%)
Mutual labels:  sql, database
Tiny tds
TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
Stars: ✭ 575 (+11400%)
Mutual labels:  sql, database
Node Sqlite
SQLite client for Node.js applications with SQL-based migrations API written in Typescript
Stars: ✭ 642 (+12740%)
Mutual labels:  sql, database
Qb
The database toolkit for go
Stars: ✭ 524 (+10380%)
Mutual labels:  sql, database
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (+14240%)
Mutual labels:  sql, database
Baikaldb
BaikalDB, A Distributed HTAP Database.
Stars: ✭ 707 (+14040%)
Mutual labels:  sql, database
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (+10680%)
Mutual labels:  sql, database
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (+15240%)
Mutual labels:  sql, database
Cosette
Cosette is an automated SQL solver.
Stars: ✭ 533 (+10560%)
Mutual labels:  sql, database
Jailer
Database Subsetting and Relational Data Browsing Tool.
Stars: ✭ 576 (+11420%)
Mutual labels:  sql, database
Firebird
Firebird server, client and tools
Stars: ✭ 522 (+10340%)
Mutual labels:  sql, database
Genji
Document-oriented, embedded SQL database
Stars: ✭ 636 (+12620%)
Mutual labels:  sql, database
Citus
Distributed PostgreSQL as an extension
Stars: ✭ 5,580 (+111500%)
Mutual labels:  sql, database
Ragtime
Database-independent migration library
Stars: ✭ 519 (+10280%)
Mutual labels:  sql, database
Tidb
TiDB is an open source distributed HTAP database compatible with the MySQL protocol
Stars: ✭ 29,871 (+597320%)
Mutual labels:  sql, database
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+154140%)
Mutual labels:  sql, database

Ray.AuraSqlModule

Scrutinizer Code Quality Code Coverage Build Status Build Status

Aura.Sql Module for Ray.Di

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
                $attributes            // optional key=>value attriburtes
        );
    }
}

DI trait

Replication

Installing AuraSqlReplicationModule using a connection locator for master/slave connections.

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.

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.

/**
 * @Inject
 * @Named("log_db")
 */
public function setLoggerDb(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.

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

with replication

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

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.

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.

QueryBuilder interface

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

QueryBuilder setter trait

  • 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.

ExtendedPdo

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

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.

/* @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.

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

View

The view template can be changed with binding. See more at Pagerfanta.

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);

Demo

$ php docs/demo/run.php
// It works!
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].