matthiasweb/wpdb-batch

WordPress $wpdb 批量查询

dev-master 2018-11-02 14:23 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:39 UTC


README

WordPress $wpdb 批量 UPDATE (欢迎 INSERT PR)。不要运行多个 SQL 查询 - 运行一个批量 SQL 查询,提高插件性能。

安装

composer require MatthiasWeb\wpdb-batch:dev-master

批量 UPDATE

使用以下参数构造新实例

global $wpdb;
$wpbu = new MatthiasWeb\WpdbBatch\Update($wpdb->terms, 'term_id', array(
    'term_id' => '%d',
    'term_order' => '%d'
));

add($indexValue, $updates)

添加新的更新行。返回:用于方法链的 this 实例。

$wpbu->add(1, array(
    'term_order' => 10
));

$wpbu->add(2, array(
    'term_order' => 11,
    'another_col' => 'test'
));

sql($chunkSize = 0)

获取 SQL 查询字符串。返回:如果 $chunkSize > 0,则返回 array<string>,否则返回 string

示例结果

UPDATE `wp_terms` 
SET    `term_order` = ( CASE 
                          WHEN `term_id` = 1 THEN 10 
                          WHEN `term_id` = 2 THEN 11 
                          ELSE `term_order` 
                        end ), 
       `another_col` = ( CASE 
                           WHEN `term_id` = 2 THEN 'test' 
                           ELSE `another_col` 
                         end ) 
WHERE  `term_id` IN ( 1, 2 ); 

sqlArray($chunkSize = 0)

获取 SQL 查询数组。返回:array<string>

execute($chunkSize = 0)

执行批量更新。结果:包含 failures (失败的 SQL 查询数组),updated (整数)和 success (布尔值)的 array

批量 INSERT

由于我还没有需要这个功能,因此 INSERT 尚未实现。欢迎 Pull requests。

许可

此存储库采用 MIT 许可证。该机制灵感来自 mavinoo/laravelBatch