比以往任何时候都更快、更简单地用PHP编写MySQL查询。

0.3.1 2022-01-21 16:21 UTC

This package is auto-updated.

Last update: 2024-09-21 22:00:58 UTC


README

Koss Header Image

路线图

  • 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无法假设任何事情。
    • 注意:可以传递多个likewhere子句,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 ...
  • 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'