thewpleague/wp-query-builder

此包已被废弃且不再维护。作者建议使用 awesome9/database 包。

WordPress 查询构建器

1.0.9 2018-08-09 09:44 UTC

This package is not auto-updated.

Last update: 2022-08-21 14:16:41 UTC


README

Build Status

WordPress 查询构建器

一个用于 WordPress 的表达式查询构建器。由开发者为开发者构建。

如何在托管环境中使用

如果您想在托管环境中使用此扩展,只需使用 composer 安装即可。

composer require thewpleague/wp-query-builder

使用查询构建器

include('vendor/autoload.php');

$table = new TheLeague\Database\Database::table( 'users' );
$table = wp_query_builder( 'users' );

$table->where( 'id', 2 )
  ->orderBy( 'id', 'desc' )
  ->limit( 20 )
  ->get();

数据清理

此库的目的是提供一个 表达式丰富且安全 的方式来运行针对您的 WordPress 数据库的查询(通常涉及自定义表)。

为此,所有提供的 都被转义,但请注意,列和表 名称尚未被转义。在任何情况下,即使它们被转义,您也应该对任何允许的列/表进行白名单:否则,使用用户输入或其他不可信数据来确定列/表可能会允许攻击者检索他们不应该检索的数据或生成您的数据库的映射。

示例

选择语句

$table = new TheLeague\Database\Database::table( 'users' )

// select * from wp_users
$table->get();

// select distinct * from wp_users
$table->distinct()->get();

// select SQL_CALC_FOUND_ROWS * from wp_users
$table->found_rows()->get();

为选择语句指定列

// select id from wp_users
$table->select( 'id' )->get();

// select id, user_login from wp_users
$table->select( 'id, user_login' )->get();

// select id, user_login from wp_users
$table->select( array( 'id', 'user_login' ) )->get();

// select id, user_login as username from wp_users
$table->select( array( 'id', 'user_login as username' ) )->get();

// select id, user_login as username from wp_users
$table->select( array(
	'id',
	'user_login' => 'username'
) )->get();

带有 count、sum、avg 的选择语句

// select count(*) from wp_users
$table->selectCount()->get();

// select count( id ) as count from wp_users
$table->selectCount( 'id', 'count' )->get();

// select sum( id ) as total from wp_users
$table->selectSum( 'id', 'total' )->get();

// select avg( id ) as average from wp_users
$table->selectAvg( 'id', 'average' )->get();

选择单行

// select * from wp_users WHERE user_email = 'admin@example.com' LIMIT 0, 1;
$table->where( 'user_email', 'admin@example.com' )->one();

检索值

$email = $table->select( 'user_email' )->where( 'ID', 123 )->getVar();

插入语句

// insert into wp_users columnA, columbB values(`value`, `value`)
$table->insert(array(
	'columnA' => 'value',
	'columnB' => 'value',
), array( '%s', '%s' ) );

更新语句

// update wp_users set foo = `bar`
$table->set( 'foo', 'bar' )->update();

// update wp_users set foo = `bar`, bar = `foo`
$table->set( 'foo', 'bar' )
	->set( 'bar', 'foo' )
	->update();

// update wp_users set foo = `bar`, bar = `foo`
$table->set(array(
	'foo' => 'bar',
	'bar' => 'foo',
))->update();

// update wp_users set foo = `bar` where id = 1 limit 0,1
$table->set( 'foo', 'bar' )
	->where( 'id', 1 )
	->limit( 1 )
	->update();

删除语句

// delete from wp_users where id = 1 limit 0,1
$table->where( 'id', 1 )->limit( 1 )->delete();

// truncate table wp_users
$table->truncate();

WHERE 语句

// select * from wp_users where id = 2
$table->where( 'id', 2 )->get();

// select * from wp_users where id != 42
$table->where( 'id', '!=', 42 )->get();

// select * from wp_users where id = 2 and active = 1
$table->where( 'id', 2 )->where( 'active', 1 )->get();
$table->where( 'id', 2 )->andWhere( 'active', 1 )->get();

// select * from wp_users where id = 2 or active = 1
$table->where( 'id', 2 )->orWhere( 'active', 1 )->get();

// select * from wp_users where ( a = 'b' or c = 'd' )
$table->orWhere( array(
	array( 'a', 'b' ),
	array( 'c', 'd' ),
) )->get();

// select * from wp_users where a = 1 or ( a > 10 and a < 20 )
$table->where( 'a', 1 )
	->orWhere( array(
		array( 'a', '>', 10 ),
		array( 'a', '<', 20 ),
	), 'and' )->get();

// select * from wp_users where a = 1 or ( a > 10 and a < 20 ) and c = 30
$table->where( 'a', 1 )
	->orWhere( array(
		array( 'a', '>', 10 ),
		array( 'a', '<', 20 ),
	), 'and' )
	->andWhere( 'c', 30 )->get();

// select * from wp_users where id in (23, 25, 30)
$table->whereIn( 'id', array( 23, 25, 30 ) );

// select * from wp_users where id not in (23, 25, 30)
$table->whereNotIn( 'id', array( 23, 25, 30 ) );

// select * from wp_users where skills in ('php', 'javascript', 'ruby')
$table->whereIn( 'skills', array( 'php', 'javascript', 'ruby' ) );

// select * from wp_users where id between 10 and 100
$table->whereBetween( 'id', array( 10, 100 ) );

// select * from wp_users where id not between 10 and 100
$table->whereNotBetween( 'id', array( 10, 100 ) );

// select * from wp_users where dates between '10-04-2018' and '10-09-2018'
$table->whereBetween( 'dates', array( '10-04-2018', '10-09-2018' ) );

// select * from wp_users where id is null
$table->whereNull( 'id' )->get();

// select * from wp_users where name is not null
$table->whereNotNull( 'name' )->get();

GROUP BY 和 HAVING 语句

// select * from wp_users group by id
$table->groupBy( 'id' )->get();

// select count(id) as total, post_id from phpunit where post_id > 10 group by post_id having count(id) > 25
$table->selectCount( 'id', 'total' )
	->select( 'post_id' )
	->whereIn( 'post_id', '>', 10 )
	->groupBy( 'post_id' )
	->having( 'count(id)', '>', 25 );

ORDER BY 语句

// select * from wp_users order by id asc
$table->orderBy( 'id' )->get();

// select * from wp_users order by id desc
$table->orderBy( 'id', 'desc' )->get();

// select * from wp_users order by firstname desc, lastname desc
$table->orderBy( 'firstname, lastname', 'desc' )->get();

// select * from wp_users order by firstname asc, lastname desc
$table->orderBy(array(
	'firstname' => 'asc',
	'lastname'  => 'desc',
) )->get();

// select * from wp_users order by firstname <> nick
$table->orderBy( 'firstname <> nick', null )->get();

LIMIT 语句

// select * from wp_users limit 0, 1
$table->limit( 1 )->get();

// select * from wp_users limit 20, 10
$table->limit( 10, 20 )->get();

// select * from wp_users limit 20, 10
$table->page( 2, 10 )->get();

待办事项

  • 添加连接
  • 添加查询缓存系统