olajoscs / querybuilder
关系型数据库的基本查询构建器。
Requires
- php: >=5.5.0
Requires (Dev)
- phpunit/dbunit: ^2.0
- phpunit/phpunit: ^5.3.0
README
QueryBuilder
关系型数据库的简单查询构建器,目前支持
- MySQL,
- PostgreSQL,
- SQLite。
代码100%经过单元测试。
包含4个基本(CRUD)操作及事务处理。
最低要求:PHP 5.5+及上述任意一种数据库。
创建连接
Connection类扩展了内置的PDO类。它是创建语句的起点。根据您使用的数据库类型创建一个新实例,并将其作为单例提供给DI容器。构造函数与PDO类中的相同。
$myDIContainer->singleton('connection', function() { // Create an own config object, which extends the ConnectionConfig abstract class, or implements the Config interface $configArray = include(..); $config = new Config($configArray); // Create the PDO by the config object $pdo = new \OlajosCs\QueryBuilder\PDO($config); // Create a connection factory $connectionFactory = new \OlajosCs\QueryBuilder\ConnectionFactory(); // Use the ConnectionFactory to create the Connection object by the PDO $connection = $connectionFactory->create($pdo); return $connection; });
从连接开始
有4种语句(选择、更新、插入、删除),每种语句都有自己的创建方法。所有语句都有一个执行方法,它执行构建的查询,然后返回PDOStatement对象。
事务
可以使用回调函数使用事务。在发生任何异常的情况下,所有更改都会回滚。
$connection->transaction(function() use ($connection) { $connection->get(...); });
基本选择语法
构建选择语句的语法类似于SQL中的操作。
$connection ->select(['field', 'list']) ->from('table_name') ->where('field', '=', $value) ->groupBy('field') ->orderBy('field') ->get();
获取方法列表
-
get(): 返回stdClasses的数组。
-
getAsClasses(string $className, array $constructorParameter): 返回显式对象的数组。
-
getOne(): 仅返回一个stdClass。如果返回多行,或者没有行返回,则抛出异常。
-
getOneClass(string $className, array $constructorParameter): 仅返回一个显式对象。如果返回多行,或者没有行返回,则抛出异常。
-
getOneField(string $fieldName): 仅返回行的单个字段。如果字段未找到,则抛出异常。
-
getList(string $fieldName): 返回行中指定字段的列表。如果字段未找到,则抛出异常。
-
getWithKey(string $keyField): 返回stdClasses的数组。元素的键是$keyField变量的当前值。
-
getClassesWithKey(string $className, array $constructorParameters, string $keyField): 返回显式对象的数组。元素的键是$keyField变量的当前值。
基本更新语法
更新语法类似于SQL中的语法。
只有当调用execute()方法时才执行查询。
$connection ->update('table_name') ->set( [ 'value1' => 1, 'value2' => 2 ] ) ->where('id', '=', 1) ->execute();
基本插入语法
插入语法类似于SQL中的语法。
只有当调用execute()方法时才执行查询。
$connection ->insert( [ 'field1' => 1, 'field2' => 2 ] ) ->into('table_name') ->execute(); // OR $connection ->insert() ->into('table_name') ->values( [ 'field1' => 1, 'field2' => 2 ] ) ->execute();
支持插入语句的多行,这些可以添加到一个二维数组中。
$connection ->insert( [ [ 'field1' => 1, 'field2' => 2 ], [ 'field1' => 3, 'field2' => 4 ], ] ) ->into('table_name') ->execute();
基本删除语法
删除语法类似于SQL中的语法。
只有当调用execute()方法时才执行查询。
$connection ->delete() ->from('table_name') ->where('id', '=', 1) ->execute();
原始表达式
当需要任何表达式但QueryBuilder无法处理时,可以使用RawExpression对象。这可以在选择和where方法中使用。
// for MySQL $connection ->select($connection->createRawExpression('count(1) as counter')) ->from('tests') ->getOneField('counter'); $connection ->select() // empty means * ->from('tests') ->where($connection->createRawExpression('select count....... = 1') ->get();
where子句
在Select、Update和Delete语句中可以使用where子句。所有这些语句都有关于where的方法,无论是使用"and"连接符(常规方法)还是"or"连接符,使用where...Or方法。
where...方法
- where(string $field, string $operator, mixed $value): 基本where,字段 {<>!=} 值。
- whereIn(string $field, array $values): Where field in (value1, value2)。
- whereNotIn(string $field, array $values): Where field not in (value1, value2)。
- whereBetween(string $field, mixed $min, mixed $max): Where field between min and max。
- whereNull(string $field): Where field is null。
- whereNotNull(string $field): Where field is not null。
- whereRaw(RawExpression('where ...')): 直接将where子句放入查询中。