kanryu/quick-csv

可移植的PHP库,允许您通过向RDBs发出特殊查询,非常快速地导入和导出CSV。

v1.0.3 2019-11-04 13:42 UTC

This package is auto-updated.

Last update: 2024-09-26 10:37:41 UTC


README

可移植的PHP库,允许您通过执行特殊查询到RDBs,非常快速地导入和导出CSV。

为什么QuickCsv这么快?

与Web表单的输入值相比,通过CSV导入的数据行数更多。

CVS中的每一行都有若干列,您需要检查不同的验证。该行可能已存在于数据库中,或者可能还不存在。您将检查每一行,最终数据将通过INSERT/UPDATE输入。由于行数很多,您需要为行数x列数执行这些操作。尽管实现起来非常困难,但结果仍然很慢。您可能会失望。

QuickCsv灵活且快速地解决了CSV导入过程。您只需提供CSV数据拥有的列信息作为数组,库将自动创建表并将CSV的所有行放入其中。无需在PHP中实现验证过程,所有列的所有行的值、行之间的相关性检查、外键约束等都可以通过SQL快速解决。所有这些都是由QuickCsv自动生成和执行的。

QuickCsv为您提供的服务

采用此库后,您将能够为用户提供此类服务。

  • 解决CSV数据和数据库之间的不同字符编码
  • 快速接受大量CSV
  • 接受任何添加、省略或重新排列的CSV列(但在处理之前您需要了解列顺序)
  • 接受在目标表中不存在的用户定义列

安装

composer require kanryu/quick-csv

用法

use Kanryu\QuickCsv\QuickCsvImporter;

// -- (initialize begins)
$table_field_tmpl = array(
    array('name' => 'productId',   'type' => 'decimal(15)',   'maxlength' => 15,   ), 
    array('name' => 'categoryId',  'type' => 'decimal(9)',    'maxlength' => 9,    'required' => true),
    array('name' => 'productCode', 'type' => 'varchar',       'maxlength' => 20,   ),
    array('name' => 'productName', 'type' => 'varchar',       'maxlength' => 40,   'required' => true),
    array('name' => 'price',       'type' => 'decimal(8,2)',  'maxlength' => 8,    'required' => true),
    array('name' => 'cost',        'type' => 'decimal(14,5)', 'maxlength' => 14,   'default' => "NULL"),
    array('name' => 'deleteFlag',  'type' => 'decimal(1)',    'maxlength' => 1,    'default' => "'0'", 'custom' => "deleteFlag BETWEEN '0' AND '1'"),
);
$qcsv = new QuickCsvImporter([
    'destTableName' => 'Product', 
    'destPrimaryKey' => 'productId', 
    'fieldSchema' => $table_field_tmpl
]);
$qcsv->setPdo($pdo); // set your pdo or other RDB drivers;

$qcsv->create(); // create temporary table for importing
$qcsv->import('./test.csv'); // import csv to the temporary table
// -- (initialize ends)

// -- (validate begins)
$qcsv->validateAllFields(); // validate all records, all fields
$qcsv->validateDuplicatedId('productId'); // validate duplicated uniqued field

// If the specified field is the primary key of an external table, the key must exist in the external table.
$qcsv->validateNonExistForeignKey('categoryId', 'categoryId', 'Category', 'deleteFlag = 0');
// -- (validate ends)

// -- merge to the destination table
$qcsv->updateExistingRecords(); // Overwrite records existing in the destination table with CSV
$qcsv->insertNonExistingRecords(); // Add a new record from CSV that does not exist in the destination table

CSV字段架构

您将QuickCsvImporter的CSV列定义作为数组提供。这实际上被创建为一个临时表。导入的CSV数据将被验证。

  • name
    • CSV列的名称。它成为临时表的字段名。
    • 如果整个数组以关联数组的形式给出,则可以省略每个列的名字属性,作为外数组的键。
  • type
    • 字段假定的数据类型。临时表首先被导入为VARCHAR,然后确定是否可以将其转换为给定的类型。
    • 可用类型:varchar, alphanumeric, datetime, date, decimal(n), decimal(n,m)
      • 注意:int, tinyint字段作为decimal(n)
    • 对于varchar,不执行任何操作。对于其他类型,需要进行一些确认。
    • 'alphanumeric'是REGEXP '^[a-zA-Z0-9\-]+$'。例如:'abcde123''123-456'
    • 您可以通过调用setValidatorForType()添加新类型。
    • 错误:XXX_notalphanumeric, XXX_notdatetime, XXX_notdecimal
  • maxlength
    • 确定字段中输入的字符串长度。
    • 临时表的每个字段都定义为maxlength + 1个字符的列。
    • 错误:XXX_maxlength
  • field(可选)
    • 通过手动定义临时表的字段架构。
    • 然而,由于'CREATE TABLE'的DEFAULT选项在CSV导入期间不起作用,因此指定default键。
    • 没有 字段键,将自动生成 {$name} VARCHAR({$maxlength+1}) DEFAULT {$default}
  • 必填(可选)
    • CSV列是必填输入,空值将导致错误。
    • 不要同时指定 默认值。因为 必填 检查字段是否为 ''
    • 错误: XXX_required
  • 默认值(可选)
    • 如果CSV列的值为空,则更改临时表中实际输入的初始值。
    • 因为它是以SQL的形式插入的,所以当提供字符串时,需要写 "'abc'"。
    • 例如: '0''NULL'"'abc'"'NOW()'
  • 自定义(可选)
    • 描述SQL公式以直接验证字段值。
    • 给出一个SQL表达式,使得标准值返回 TRUE。
    • 您可以在表达式中放置您设置的 所有字段和 id 字段(作为CSV行号)。
    • 错误: XXX_custom
    • 例如: deleteFlag BETWEEN '0' AND '1'
  • 跳过(可选)
    • 如果为真,它是CSV字段之一,但不是目标表字段。
    • 在执行 updateExistingRecords/insertNonExistingRecords 时跳过传输字段。
    • 这可以用于未以某种方式在目标表中输入的列,例如用户定义的列和注释列。

由于一些CSV列值的验证结果敏感,必须以固定的顺序识别错误。

  1. 必填
  2. maxlength
  3. type
  4. 自定义

参考(API文档)

https://kanryu.github.io/quick-csv/

完整示例

查看 tests/test.php

每个API实际发出的SQL

查看 tests/QuickCsvImporterSchemaTest.php

许可

MIT

作者

版权所有 2019 KATO Kanryu(k.kanryu@gmail.com