mosamirzz/

bulk-query

使用laravel执行批量更新/插入/删除。

2.1 2023-04-10 05:35 UTC

This package is auto-updated.

Last update: 2024-09-22 03:36:24 UTC


README

Latest Version on Packagist Total Downloads

使用laravel执行批量/批量更新/插入/删除。

问题

我尝试使用laravel进行批量更新,但发现Laravel不支持该功能;因此,我创建了此包,通过它您可以执行批量/批量更新、插入和删除。

安装

您可以通过composer安装此包

composer require mosamirzz/bulk-query

用法

批量或批量更新意味着:您可以单次查询更新多条记录。删除和插入也是如此(可以单次查询插入或删除多条记录)。

此包提供了4个类

  • Insert::class 用于单次查询插入多条记录。
  • Update::class 用于单次查询更新多条记录。
  • InsertOrUpdate::class 用于单次查询插入多条记录,如果发现记录中有重复的记录,则会更新它。
    • 此查询仅在找到唯一主键时更新记录。这意味着如果表中没有唯一主键列,并且尝试插入两次记录,则将作为两条记录插入,而不会更新记录。
    • 注意:必须将主键发送到查询以确定是否更新重复记录。如果不发送,并且没有将唯一列发送到查询,则查询将仅执行插入。
  • Delete::class 用于单次查询删除多条记录。

批量删除

您可以使用以下类 Delete::class 执行批量或批量删除

use Mosamirzz\BulkQuery\Delete;

// 1. create new instance and pass the table name to the constructor.
$delete = new Delete("users");
// 2. send the `IDs` of the records to the prepare as array.
$delete->prepare([1, 2, 3, 4]);
// 3. execute the bulk delete query.
$delete->execute();

如上图所示,默认的删除语句是根据列 id 删除,但如果您想使用不同的列执行删除查询,则可以使用方法 useKey 并传递列名,如下所示

use Mosamirzz\BulkQuery\Delete;

$delete = new Delete("users");
// change the default column used by the delete statment.
$delete->useKey("email");
// pass the values of the key that we want to delete it's records.
$delete->prepare(["gm.mohamedsamir@gmail.com", "user1@test.com", "user2@test.com"]);
$delete->execute();

删除中使用的默认 key 是列 id

批量插入

您可以使用以下类 Insert::class 执行批量插入

use Mosamirzz\BulkQuery\Insert;

// 1. create new instance and pass the table name.
$insert = new Insert("users");
// 2. pass the columns used by the insert query.
$insert->useColumns(["name", "email", "password"]);
// 3. send the records that we want to insert.
$insert->prepare([
    [
        "name" => "mohamed samir",
        "email" => "gm.mohamedsamir@gmail.com",
        "password" => "123456"
    ],
    [
        "name" => "user 1",
        "email" => "user1@test.com",
        "password" => "123456"
    ],
]);
// 4. execute the bulk insert query.
$insert->execute();

如上图所示,prepare 方法接受 array[],每个数组代表我们想要插入到数据库中的记录。

如果您尝试发送已在表中存在的记录,则查询将抛出以下异常:SQLSTATE[23000]: Integrity constraint violation

批量更新

您可以使用以下类 Update::class 执行批量更新

use Mosamirzz\BulkQuery\Update;

// 1. create instance and pass the table name.
$update = new Update("users");
// 2. pass the columns that we need to update them.
$update->useColumns(["name", "password"]);
// 3. pass the records that we need to update them
$update->prepare([
    1001 => [
        "name" => "mohamed samir updated",
        "password" => "1234_updated"
    ],
    1105 => [
        "name" => "user updated",
        "password" => "4321_updated"
    ],
]);
// 4. execute the bulk update query.
$update->execute();

如上图所示

  • prepare 方法接受 array[],每个数组代表我们想要在数据库中更新的记录。
  • 每条记录必须像 "key" => [...] 这样,其中 key 代表我们用于更新记录的列名。在上面的示例中,键是默认的 id,因此当我们想要更新记录时,我们必须发送记录 id 作为 key 以及我们想要更新的值的 array

您可以使用 useKey 方法将更新中使用的默认列 id 更改为其他列。

例如,您可以使用以下方式将 email 列用作更新中的键

use Mosamirzz\BulkQuery\Update;

$update = new Update("users");
$update->useColumns(["name", "password"]);
// change the default key used by the update.
$update->useKey("email");
$update->prepare([
    "gm.mohamedsamir@gmail.com" => [
        "name" => "mohamed samir updated",
        "password" => "mohamed"
    ],
    "user1@gmail.com" => [
        "name" => "user 1",
        "password" => "123456"
    ],
]);
$update->execute();

如上图所示,我们使用了 email 列作为更新语句中的键。

更新中使用的默认 key 是列 id

如果您想更新数据库中某条记录的一列,并将其他列的值设置为旧值,您可以按照以下步骤从数组中移除该列的 key=>value

$update = new Update("users");
$update->useColumns(["name", "password", "is_admin"]);
$update->prepare([
    1 => [
        "is_admin" => true,
    ],
    3 => [
        "name" => "ahmed",
        "is_admin" => false,
    ],
    70 => [
        "password" => "123123"
    ],
]);
$update->execute();

如上图所示,用户 id = 1 只更新了 is_admin 这一列,我们不需要传递 namepassword 列给它,查询将会为 id = 1 的用户使用 namepassword 的旧值。

对于 id = 3 的用户,将更新 nameis_admin 列,但 password 列将使用旧值。

对于 id = 70 的用户,只更新 password 列。

批量插入或更新

您可以使用类 InsertOrUpdate::class 在同一个查询中执行批量插入和更新。

查询只会在找到 unique 列或 primary key 列时更新记录。

您可以使用该类如下

use Mosamirzz\BulkQuery\InsertOrUpdate;

// 1. create instance and pass the table name.
$query = new InsertOrUpdate("users");
// 2. pass the columns used in insertion.
$query->useColumns(["name", "email", "password"]);
// 3. pass the columns used in the update when it find duplicate record.
$query->updatableColumns(["name", "password"]);
// 4. pass the records that we need to insert or update.
$query->prepare([
    [
        "name" => "mohamed samir",
        "email" => "gm.mohamedsamir@gmail.com",
        "password" => "mohamed124"
    ],
    [
        "name" => "hello",
        "email" => "hello@user.com",
        "password" => "hello"
    ],
]);
// 5. execute the bulk insert or update query.
$query->execute();

如上图所示

  • 假设在表中已经存在一个邮箱为 gm.mohamedsamir@gmail.com 的用户。
  • 因此,当查询执行时,邮箱为 gm.mohamedsamir@gmail.com 的记录将被更新,但只有 namepassword 列会被更新。
  • 第二个邮箱为 hello@user.com 的记录将被插入到表中,因为它之前并不存在。

测试

composer run test

变更日志

请参阅 变更日志 了解最近发生的变更。

贡献

请参阅 贡献指南 了解详细信息。

安全性

如果您发现任何安全相关的问题,请通过电子邮件 gm.mohamedsamir@gmail.com 而不是使用问题跟踪器。

致谢

许可协议

MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件

Laravel 包模板

本包使用 Laravel 包模板 生成。