Git Product home page Git Product logo

c9s / sqlbuilder Goto Github PK

View Code? Open in Web Editor NEW
146.0 13.0 21.0 2.25 MB

A powerful, fast, cross-platform SQL Builder for PHP. Convert your structured data into SQL queries with a fluent style interface and targeting on all the mainstream database (MySQL, PostgreSQL, SQLite)

License: Other

PHP 79.18% C 1.80% Shell 0.06% CSS 0.68% HTML 11.17% JavaScript 7.03% M4 0.08%
php sql-builder sql-query mysql pgsql database

sqlbuilder's Introduction

SQLBuilder for PHP

Build Status Coverage Status Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License

If you're looking for something that is not an ORM but can generate SQL for you, you just found the right one.

SQLBuilder is not an ORM (Object relational mapping) system, but a toolset that helps you generate cross-platform SQL queries in PHP.

SQLBuilder is a stand-alone library, you can simply install it through composer or just require them (the class files) with your autoloader, and it has no dependencies.

Features

  • Simple API, easy to remember.
  • Fast & Powerful.
  • Custom parameter marker support:
    • Question-mark parameter marker.
    • Named parameter marker.
  • Configurable quote handler.
  • Zero dependency.

Synopsis

Here is a short example of using Universal SelectQuery

use SQLBuilder\ArgumentArray;
use SQLBuilder\Universal\Query\SelectQuery;
use SQLBuilder\Driver\MySQLDriver;
use SQLBuilder\Driver\PgSQLDriver;
use SQLBuilder\Driver\SQLiteDriver;

$mysql = new MySQLDriver;
$args = new ArgumentArray;

$query = new SelectQuery;
$query->select(array('id', 'name', 'phone', 'address','confirmed'))
    ->from('users', 'u')
    ->partitions('u1', 'u2', 'u3')
    ->where()
        ->is('confirmed', true)
        ->in('id', [1,2,3])
    ;
$query
    ->join('posts')
        ->as('p')
        ->on('p.user_id = u.id')
    ;
$query
    ->orderBy('rand()')
    ->orderBy('id', 'DESC')
    ;

$sql = $query->toSql($mysql, $args);

var_dump($sql);
var_dump($args);

A More Detailed Description

Unlike other SQL utilities, SQLBuilder let you define the quote style and the parameter marker type. there are 2 parameter marker type you can choose:

  1. Question mark parameter marker (?)
  2. Named parameter. (:id, :name, :address, :p1)

The above two are supported by PDO directly, and the first one is also supported by mysqli, pgsql extension.

The API is dead simple, easy to remember, you can just define one query, then pass different query driver to the query object to get a different SQL string for your targettting platform.

It also supports cross-platform query generation, there are three types of query (currently): Universal, MySQL, PgSQL. The Universal queries are cross-platform, you can use them to create a cross-platform PHP API of your database system, and the supported platforms are: MySQL, PgSQL and SQLite.

Universql Queries:

  • CreateDatabaseQuery
  • DropDatabaseQuery
  • SelectQuery
  • InsertQuery
  • UpdateQuery
  • DeleteQuery
  • UnionQuery
  • CreateIndexQuery
  • DropIndexQuery

To see the implementation details, you can check the source code inside Universal namespace: https://github.com/c9s/SQLBuilder/tree/master/SQLBuilder/Universal/Query

MySQL Queries:

  • CreateUserQuery
  • DropUserQuery
  • GrantQuery
  • SetPasswordQuery

For MySQL platform, the implementation is according to the specification of MySQL 5.6.

For PostgreSQL platform, the implementation is according to the specification of PostgreSQL 9.2.

Installation

Install through Composer

composer require corneltek/sqlbuilder

Getting Started

Documentation

Development

composer install

Copy the phpunit.xml file for your local configuration:

phpunit -c your-phpunit.xml tests

Contribution

To test with mysql database:

mysql -uroot -p
CREATE DATABASE sqlbuilder CHARSET utf8;
GRANT ALL PRIVILEGES ON sqlbuilder.* TO 'testing'@'localhost' identified by '';

--- or use this to remove password for testing account
SET PASSWORD FOR testing@localhost=PASSWORD('');

To test with pgsql database:

sudo -u postgres createdb sqlbuilder

Reference

Author

Yo-An Lin (c9s) [email protected]

sqlbuilder's People

Contributors

bubach avatar c9s avatar iggyvolz avatar joushx avatar kbinani avatar krichprollsch avatar smetdenis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqlbuilder's Issues

Documentation Update

  • What's Query Driver?
  • Deflating values (BaseDriver::deflate)
  • Arguments
  • Bind
  • ParamMarker
  • Deflating SQL Function Call

Queries:

  • SelectQuery
  • InsertQuery
  • UpdateQuery
  • DeleteQuery

Support for SQL_CALC_FOUND_ROWS

Hi @c9s!

The current php-SQLBuilder does not support SQL_CALC_FOUND_ROWS.
Any plan to add it?

Perhaps adding a new method selectCalcFoundRows()

// A classic QueryBuilder
// To generate : SELECT user.id FROM user LIMIT 10
$query = (new \SQLBuilder\QueryBuilder($driver))->table('user')->select('user.id')->limit(10);

// Adding a selectCalcFoundRows() methods
// To generate : SELECT SQL_CALC_FOUND_ROWS user.id FROM user LIMIT 10
$query->selectCalcFoundRows();

// The method could take a boolean to disable it
// To generate : SELECT user.id FROM user LIMIT 10
$query->selectCalcFoundRows(false);

I could try to code it.
Are you interested in a PR?

Cheers,
Thomas.

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.