sqlbuilder v1.1.0
SqlBuilder allows users to build quickly and easily complex., (*1)
Install:
- With composer:, (*2)
composer require jinnguyen/puja-sqlbuilder
Usage:, (*3)
require_once 'path/to/vendor/autoload.php';
use Puja\SqlBuilder\Builder;
$builder = new Builder('php_');
Examples
SELECT, (*4)
$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:, (*5)
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, (*6)
$select = $builder->reset()->insert('content', array('name' => 'Jin', 'addtime__exact' => 'NOW()'));
echo $select->getQuery();
Result:, (*7)
INSERT INTO content(`name`,`addtime`) VALUES ("Jin", NOW())
UPDATE, (*8)
$select = $builder->reset()->update('content', array('name' => 'Jin', 'addtime__exact' => 'NOW()'))
->where('content_id=%d', 5);
echo $select->getQuery();
Result:, (*9)
UPDATE content SET `name`="Jin",`addtime`=NOW() WHERE (content_id=5)
REPLACE, (*10)
$select = $builder->reset()->replace('content', array('name' => 'Jin', 'addtime__exact' => 'NOW()'))
->where('content_id=%d', 5);
echo $select->getQuery();
Result:, (*11)
REPLACE INTO content(`name`,`addtime`) VALUES ("Jin", NOW())
DELETE, (*12)
$select = $builder->reset()->delete('content')->where('content_id=%d', 5);
echo $select->getQuery();
Result:, (*13)
DELETE FROM content WHERE (content_id=5)
TRUNCATE, (*14)
$select = $builder->reset()->truncate('content');
echo $select->getQuery();
Result:, (*15)
TRUNCATE TABLE content