jbzoo / sqlbuilder
此包已废弃且不再维护。未建议替代包。
简单的 SQL 查询构建器
1.1.4
2016-06-23 13:46 UTC
Requires
- php: >=5.3.10
Requires (Dev)
- jbzoo/phpunit: dev-master
This package is auto-updated.
Last update: 2021-04-25 12:03:16 UTC
README
PHP 库描述
初始化构建器
use JBZoo\SqlBuilder\SqlBuilder;
// composer autoload.php
require_once './vendor/autoload.php';
// register database database type, connection and table prefix (if exists)
$connection = mysqli_connect('127.0.0.1', 'root', '', 'sqlbuilder', '3306');
SqlBuilder::set('mysqli', $connection, 'prefix_');
SELECT 查询
use JBZoo\SqlBuilder\Query\Select;
echo (new Select('table', 'tTable')); // or __toString() method
SELECT * FROM `table` AS `tTable`
简单 SELECT 查询
$select = (new Select('#__items', 'tItem'))
->select('tItem.item_id', 'id')
->leftJoin(['#__categories', 'tCategory'], 'tCategory.id = tItem.category_id')
->where(['tItem.name', ' = ?s'], "O'Realy")
->where('tItem.name IN ?a', ['42', '43', '44'])
->group('tItem.author')
->order('tItem', 'desc')
->limit(5, 10);
SELECT `tItem`.`item_id` FROM `prefix_items` AS `tItem` LEFT JOIN `prefix_categories` AS `tCategory` ON (tCategory.id = tItem.category_id) WHERE `tItem`.`name` = 'O\'Realy' AND tItem.name IN ('42', '43', '44') GROUP BY `tItem`.`author` ORDER BY `tItem` DESC LIMIT 10, 5
灵活且安全的 WHERE 条件
$select = (new Select(['items', 'tAlias']))
->where("prop = 'O\\'Realy'") // old school
->where('prop = ?s', "O'Realy") // escape string
->where(['prop', '= ?s'], "O'Realy", 'or') // quote field
->whereOR(['tAlias.prop', '=', '?s'], "O'Realy"); // more syntax suger :)
SELECT * FROM `items`AS `tAlias` WHERE prop = 'O\'Realy' AND prop = 'O\'Realy' OR `prop` = 'O\'Realy' OR `tAlias`.`prop` = 'O\'Realy'
转义其他变量类型
$select = (new Select(['items', 'tAlias']))
->where('prop <> ?e', 'tAlias.col') // entities
->where('prop < ?i', ' -10,56 ') // integer
->where('prop > ?f', ' -10,56 ') // float
->where('prop = ?b', 1) // bool TRUE
->where('prop <> ?b', 0) // and FALSE
->where('prop ?n', 0) // IS [NOT] NULL
->where('prop IN ?a', [null, '1', "'qwerty'"]); // array
SELECT * FROM `items` AS `tAlias` WHERE prop <> `tAlias`.`col` AND prop < -10 AND prop > -10.56 AND prop = TRUE AND prop <> FALSE AND prop IS NULL AND prop IN (NULL, '1', '\'qwerty\'')
WHERE 组
$select = (new Select(['items', 'tAlias']))
->whereGroup([
['tAlias.prop > ?i', 42],
[['tAlias.prop', '< ?i'], 4242],
])
->whereGroup([
['prop NOT IN ?a', [0, 1, null, 'Some string']],
['prop ?n', 0],
], 'OR');
SELECT * FROM `items` AS `tAlias` WHERE ( tAlias.prop > 42 AND `tAlias`.`prop` < 4242 ) OR ( prop NOT IN ('0', '1', NULL, 'Some string') AND prop IS NULL )
其他查询
- 删除
- 插入
- 替换
- 联合
- 更新
许可
MIT