aberdeener / koss
比以往任何时候都更快、更简单地用PHP编写MySQL查询。
0.3.1
2022-01-21 16:21 UTC
Requires (Dev)
- doctrine/dbal: ^3.1.0
- nunomaduro/phpinsights: ^2.0
- phpunit/phpunit: ^9.5
README
路线图
- 在
on()
之后使链式连接子句更加流畅,添加then($callback)
(而不是当前的多次单独调用和through($table)
的使用)? - 添加
prefix(string $prefix)
函数以设置要自动附加的表前缀。
文档
- 需要PHP 8.0
设置
- 使用Composer自动加载Koss。
- 通过传递您的MySQL登录和数据库信息来初始化一个新的Koss实例。
- 参数
- 1: 主机名
- 2: 端口
- 3: 数据库名称
- 4: 用户名
- 5: 密码
注意:您函数的顺序不重要。您可以在limit()
之后选择通过columns()
选择更多列。
核心函数
在选择和更新/插入查询中都可用的函数。
execute()
- 执行编译的Koss MySQL代码并输出结果
- 注意:如果不调用此函数,则代码末尾将不会有任何输出!
when(Closure | bool $expression, Closure $callback)
- 当
$expression
为true时,仅执行$callback
函数。 - 注意:
$expression
可以是布尔值(例如5 < 10
)或返回布尔值的匿名函数
- 当
unless(Closure | bool $expression, Closure $callback)
- 当
$expression
为false时,仅执行$callback
函数。 - 注意:
$expression
可以是布尔值或返回布尔值的匿名函数
- 当
where(string $column, string $operator, string $matches)
- 在
$table
(必须通过选择语句预先提供)中选择具有$column
中值的行,这些值与$match
相关。 - 注意:如果不提供
$operator
,则假定'='
- 示例SQL代码:
WHERE username <> 'Aberdeener'
- 在
like(string $column, string $like)
- 在
$table
(必须通过选择语句预先提供)中选择具有与$column
中值相似的行的值。 - 您必须在想要它们出现的地方提供
%
,Koss无法假设任何事情。 - 注意:可以传递多个
like
和where
子句,Koss将处理编译正确的MySQL代码 - 示例SQL代码:
WHERE first_name LIKE %Tadhg%
- 在
选择函数
getAll(string $table)
- 选择
$table
中的所有列 - 示例SQL代码:
SELECT * FROM users
- 选择
getSome(string $table, array | string $columns)
- 选择
$table
中的特定$columns
(如果提供字符串,则只选择一个列) - 示例SQL代码:
SELECT username, first_name, last_name FROM users
- 选择
groupBy(string $column)
- 将具有相同
$column
值的行分组在一起 - 示例SQL代码:
GROUP BY age
- 将具有相同
orderBy(string $column, string $order)
- 按
$column
排序输出,要么是ASC
,要么是DESC
- 示例SQL代码:
ORDER BY first_name DESC
- 按
limit(int $limit)
- 仅返回
$limit
行。 - 示例SQL代码:
LIMIT 3
- 仅返回
columns(array $columns)
- 除了在原始
getSome()
中提供的列之外,还选择$columns
column(string $column)
允许选择单个列。- 示例 SQL 代码:
SELECT username, first_name
- 除了在原始
cast(string $column, string $type)
- 在从数据库检索时将特定的
$column
数据转换为$type
。
- 在从数据库检索时将特定的
casts(array $casts)
- 在 SelectQuery 中同时转换多个列。
$casts
必须是以下格式的数组// Column name => Type $casts = array( 'id' => 'int', 'username' => 'string', 'money' => 'float' );
更新/插入函数
update(string $table, array $values)
- 更新
$table
中的任何行到新的$values
。 $values
必须是以下格式的数组// Column name => Value $values = array( 'username' => 'Aber' );
- 示例 SQL 代码:
UPDATE users SET username = 'Aberdeener' WHERE ...
- 示例 SQL 代码:
- 更新
insert(string $table, array $row)
- 向
$table
中插入新行。 $row
必须是以下格式的数组// Column name => Value $row = array( 'username' => 'Aberdeener', 'first_name' => 'Tadhg', 'last_name' => 'Boyle' );
- 示例 SQL 代码:
INSERT INTO users (username, first_name, last_name) VALUES ('Aberdeener', 'Tadhg', 'Boyle')
- 向
onDuplicateKey(array $values)
- 在插入时,如果有唯一列被覆盖,则运行此代码。
$values
必须是以下格式的数组// Column name => New value $values = array( 'username' => 'Aber' );
- 示例 SQL 代码:
ON DUPLICATE KEY UPDATE username = 'Aber'
其他函数
不在选择或更新/插入查询中的函数
execute(string $query)
- 执行提供的
$query
并输出结果。 - 常见用法是原始查询,其中 Koss 没有提供帮助的功能。
- 注意:不能与其他函数混合使用
- 执行提供的
示例
所有这些假设你已经自动加载了 Koss.php
并使用数据库凭据创建了一个新实例。
-
选择信息
// Get the "username" and "first_name" column in the "users" table, limit to only the first 5 rows, and sort by their username descending. $results = $koss->getSome('users', ['username', 'first_name'])->limit(5)->orderBy('username', 'DESC')->execute(); // MySQL Output: SELECT `username`, `first_name` FROM `users` ORDER BY `username` DESC LIMIT 5 // Get all columns in the "users" table, and when they're logged in, limit to only the first 5 rows. // Note the usage of new variable, $query in anonymous function. This will be passed by Koss. $results = $koss->getAll('users')->when(fn() => isset($_SESSION['logged_in']), fn(SelectQuery $query) => $query->limit(5))->execute(); // MySQL Output: SELECT * FROM `users` LIMIT 5 // Get the "username" column in the "users" table, but also select the "last_name" column. $results = $koss->getSome('users', 'username')->columns(['last_name'])->execute(); // MySQL Output: SELECT `username`, `last_name` FROM `users`
-
插入信息
// Insert a new row into the "users" table, if there is a unique row constraint, update only the username to "Aber" $koss->insert('users', ['username' => 'Aberdeener', 'first_name' => 'tadhg', 'last_name' => 'boyle'])->onDuplicateKey(['username' => 'Aber'])->execute(); // MySQL Output: INSERT INTO `users` (`username`, `first_name`, `last_name`) VALUES ('Aberdeener', 'tadhg', 'boyle') ON DUPLICATE KEY UPDATE `username` = 'Aber'
-
更新信息
// Update any existing rows in the "users" table which match the following criteria, update the username to "Aber" and the first_name to "Tadhg" where their "id" is 1 and their last_name is "Boyle" $koss->update('users', ['username' => 'Aber', 'first_name' => 'Tadhg'])->where('id', 1)->where('last_name', '=', 'Boyle')->execute(); // MySQL Output: UPDATE `users` SET `username` = 'Aber', `first_name` = 'Tadhg' WHERE `id` = '1' AND `last_name` = 'Boyle'