Coder Social home page Coder Social logo

sqlbuilder's Introduction

SQL Builder

The library bases on two ideas:

  • an SQL statement expressed with this library should be similar to the statement itself;
  • flexible combination of statement components.

Thus, using this library you get simple but, at the same time, powerful tool to build SQL statements in flexible and intuitive way.

use AlephTools\SqlBuilder\PostgreSql\SelectStatement;
use AlephTools\SqlBuilder\Sql\Expression\ConditionalExpression;

$st = (new SelectStatement())
    ->select([
        'u.id',
        'u.name',
        'company' => 'c.name',
        'unreadMessageCount' => (new SelectStatement())
            ->select('COUNT(*)')
            ->from('user_messages', 'm')
            ->where('m.user_id = u.id')
            ->andWhere('m.read_at IS NULL')
    ])
    ->from('users u')
    ->innerJoin('companies c', 'c.id = u.company_id')
    ->where('u.deleted_at IS NULL')
    ->andWhere((new ConditionalExpression())
        ->where('u.roles', 'IN', ['ADMIN', 'RESELLER'])
        ->orWhere(
            (new SelectStatement())
                ->select('COUNT(*)')
                ->from('user_contacts uc')
                ->where('uc.user_id = u.id'),
            '>',
            5
        )
    );
    
// Outputs: 
// SELECT
//     u.id, u.name, c.name company,
//     (SELECT COUNT(*) FROM user_messages m WHERE m.user_id = u.id AND m.read_at IS NULL) unreadMessageCount
// FROM users u
// INNER JOIN companies c ON c.id = u.company_id
// WHERE
//     u.deleted_at IS NULL AND (
//         u.roles IN (:p1, :p2) OR 
//         (SELECT COUNT(*) FROM user_contacts uc WHERE uc.user_id = u.id) > :p3
//     )
echo $st->toSql();

// Outputs:
// ['p1' => 'ADMIN', 'p2' => 'RESELLER', 'p3' => 5]
print_r($st->getParams());

// Executes statement if StatementExecutor is defined, otherwise an exception is thrown
$rows = $st->rows();

Installation

composer require alephtools/sqlbuilder

sqlbuilder's People

Contributors

alephtav avatar elisdn avatar

Stargazers

 avatar

Watchers

 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.