Git Product home page Git Product logo

query-bus's Introduction

QueryBus SensioLabsInsight Travis CI

A PHP library for Interrogatory Messages.

Interrogatory Messages are passed to the QueryBus service which returns the result of the first QueryMatcher that supports it.

Installation

Marshaller can be installed using Composer:

composer require "gnugat/query-bus:~2.0"

Simple conversion

Let's take the following table:

CREATE TABLE article (
    id int,
    title VARCHAR(255),
    content TEXT
);

If we want to execute the following query:

SELECT id, title, content FROM article WHERE id = 42;

Then we have first to create a Query, which is a simple DTO:

<?php

require __DIR__.'/vendor/autoload.php';

use Gnugat\QueryBus\QueryBus;

class GetArticle
{
    public $id;

    public function __construct($id)
    {
        if (null === $id) {
            throw new \InvalidArgumentException('Required parameter ID is missing');
        }
        $this->id = (int) $id;
    }
}

Note: Queries can contain simple validation, for example to check for null values.

Then we have to create a QueryMatcher:

// ...

use Gnugat\QueryBus\QueryMatcher;

class GetArticleMatcher implements QueryMatcher
{
    private $connection;

    public function __construct($connection)
    {
        $this->connection = $connection;
        pg_prepare($this->connection, 'get_article', 'SELECT id, title, content FROM articles WHERE id = $1');
    }

    public function supports($query)
    {
        return $query instanceof GetArticle;
    }

    public function match($query)
    {
        $result = pg_execute($this->connection, 'get_article', array($query->id));

        return pg_fetch_array($result, NULL, PGSQL_ASSOC);
    }
}

Next we need to register it in QueryBus:

// ...

use Gnugat\QueryBus\QueryBus;

$connection = pg_pconnect('dbname=blog');
$queryBus = new QueryBus();
$queryBus->add(new GetArticleMatcher($connection));

Finally we can actually execute the query:

// ...

$articles = $queryBus->match(new GetArticle(42));

Further documentation

You can see the current and past versions using one of the following:

You can find more documentation at the following links:

query-bus's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.