Coder Social home page Coder Social logo

puja-sqlbuilder's Introduction

sqlbuilder v1.1.0

SqlBuilder allows users to build quickly and easily complex.

Install:

  • With composer:
composer require jinnguyen/puja-sqlbuilder

Usage:

    require_once 'path/to/vendor/autoload.php';
    use Puja\SqlBuilder\Builder;
    $builder = new Builder('php_');

Examples
SELECT

$select = $builder->select()
    ->from(array('c' => 'content'), array('id' => 'content_id'))
    ->from(array('ln' => 'content_ln'), array('title' => 'name', 'iso2_code'))
    ->joinLeft('category', 'category.category_id=content.category_id', array('name', 'category_id'))
    ->order('content.content_id') ->order('category.category_id', Builder::ORDER_DESC) ->limit(10)
    ->having('content.content_id=%d', 10) ->groupBy('content.content_id')
    ->where('c.content_id=%d AND ln.name LIKE "%%%s%%"', 4, 'search term')
    ->orWhere('category.name IS EMPTY AND category.category_id >= %d', 5);

echo 'Query:' . $select->getQuery();
echo 'Count:' . $select->getCount();

Result:

Query:SELECT c.content_id AS id,ln.name AS title,ln.iso2_code,category.name,category.category_id FROM content AS c,content_ln AS ln LEFT JOIN category AS category ON category.category_id=content.category_id WHERE (c.content_id=4 AND ln.name LIKE "%search term%") OR (category.name IS EMPTY AND category.category_id >= 5) GROUP BY content.content_id HAVING (content.content_id=10) ORDER BY content.content_id ,category.category_id DESC LIMIT 0,10
Count:SELECT COUNT(*) AS total FROM content AS c,content_ln AS ln LEFT JOIN category AS category ON category.category_id=content.category_id WHERE (c.content_id=4 AND ln.name LIKE "%search term%") OR (category.name IS EMPTY AND category.category_id >= 5) GROUP BY content.content_id HAVING (content.content_id=10)

INSERT

$select = $builder->reset()->insert('content', array('name' => 'Jin', 'addtime__exact' => 'NOW()'));
echo $select->getQuery();

Result:

INSERT INTO content(`name`,`addtime`) VALUES ("Jin", NOW())

UPDATE

$select = $builder->reset()->update('content', array('name' => 'Jin', 'addtime__exact' => 'NOW()'))
    ->where('content_id=%d', 5);
echo $select->getQuery();

Result:

UPDATE content SET `name`="Jin",`addtime`=NOW() WHERE (content_id=5)

REPLACE

$select = $builder->reset()->replace('content', array('name' => 'Jin', 'addtime__exact' => 'NOW()'))
    ->where('content_id=%d', 5);
echo $select->getQuery();

Result:

REPLACE INTO content(`name`,`addtime`) VALUES ("Jin", NOW())

DELETE

$select = $builder->reset()->delete('content')->where('content_id=%d', 5);
echo $select->getQuery();

Result:

DELETE FROM content WHERE (content_id=5)

TRUNCATE

$select = $builder->reset()->truncate('content');
echo $select->getQuery();

Result:

TRUNCATE TABLE content

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.