Giter VIP home page Giter VIP logo

dbquery's Introduction

Jasny's Query builder for PHP

Build Status

This library is designed to be the ultimate tool for building, splitting and modifying SQL queries.

Automatic smart quoting helps against SQL injection and problems with reserved keywords.

The query builder can be used standalone, in conjunction with Jasny's DB layer or in almost any framework.

The current version only support MySQL SQL queries

Installation

Jasny's Query builder is registred at packagist as jasny/dbquery and can be easily installed using composer. Alternatively you can simply download the .zip and copy the file from the 'src' folder.

Examples

An example to simple to be using a query builder

<?php
    use Jasny\DB\MySQL\Query;
    
    $query = Query::select()->columns('id, name')->from('foo')->where('active = 1');
    $result = $mysqli->query($query); // SELECT `id`, `name` FROM `foo` WHERE `active` = 1

Dynamicly apply paging and filtering on a query

<?php
    use Jasny\DB\MySQL\Query;
    
    $query = new Query("SELECT * FROM foo LEFT JOIN bar ON foo.bar_id = bar.id WHERE active = 1 LIMIT 25");
    if (isset($_GET['page'])) $query->page(3);

    $filter = isset($_POST['filter']) ? $_POST['filter'] : array(); // array('type' => 'bike', 'price between ? and ?' => array(10, 20))
    foreach ($filter as $field => $value) {
        $query->where($field, $value);
    }

    $result = $mysqli->query($query); // SELECT * FROM foo LEFT JOIN bar ON foo.bar_id = bar.id WHERE (active = 1) AND (`type` = "bike") AND (`price` between 10 and 20) LIMIT 25 OFFSET 50

Map fields for an INSERT INTO ... SELECT ... ON DUPLICATE KEY query

<?php
    use Jasny\DB\MySQL\Query;
    
    $columns = array(
        'ref' => 'ref',
        'man' => 'boy',
        'woman' => 'girl',
        'amount' => 'SUM(z.bucks)'
    );

    $select = Query::select()->columns($columns)->from('foo')->innerJoin('z', 'foo.id = z.foo_id')->groupBy('foo.id');
    $insert = Query::insert()->into('abc')->columns(array_keys($columns))->set($select)->onDuplicateKeyUpdate();

    $mysql->query($insert); // INSERT INTO `abc` (`ref`, `man`, `woman`, `amount`)
                            //  SELECT `ref` AS `ref`, `boy` AS `man`, `girl` AS `woman`, SUM(`z`.`bucks`) AS `amount` FROM `foo` LEFT JOIN `z` ON `foo`.`id` = `z`.`foo_id` GROUP BY `foo`.id`
                            //  ON DUPLICATE KEY UPDATE `ref` = VALUES(`ref`), `man` = VALUES(`man`), `woman` = VALUES(`woman`), `amount` = VALUES(`amount`)

API documentation (generated)

http://jasny.github.com/dbquery/docs

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.