ruhulfbr/csvqry

在 CSV 上进行数据检索查询,类似于 laravel 的 `Query Builder`

v1.0.3 2024-05-10 14:11 UTC

This package is auto-updated.

Last update: 2024-09-10 14:46:48 UTC


README

CSV 查询 允许您在 CSV 上执行查询。

使用场景

如果您需要在以下方面执行一些查询,此软件包适合您:

  • 执行 SELECT 查询
  • 执行 WHERE 查询(WHERE、orWhere、whereIn、whereDate 等)
  • 执行排序
  • 执行 Limit、Offset
  • 执行聚合查询(count、sum、avg、max、min)

安装

要安装此软件包,您可以使用 Composer

composer require ruhulfbr/csvqry

基本用法

要实例化 CSVQ,请执行以下操作

require_once 'vendor/autoload.php';
use Ruhul\CSVQuery\CSVQ;

try {
    $result = CSVQ::from("example.csv")
        ->select('id', 'name')
        ->get();

} catch (\Exception $e) {
    $result = $e->getMessage();
}

pr($result);

查询、排序并获取结果

您可以在 CSV 上执行查询

$result = CSVQ::from("example.csv")
        ->select('id', 'name')
        //->select(['id', 'name'])
        ->where('id', 2)
        //->where('id', '>' ,2)
        ->orWhere('id', 3)
        //->orWhere('id', '>=', 3)
        ->whereDate('dob', '2010-10-10')
        //->whereDate('dob', '>=','2010-10-10')
        ->whereLike('name', 'ruhul')
        //->whereLike('name', 'ruhul', 'start')
        //->whereLike('name', 'ruhul', 'end')
        ->whereIn('age', [22,23,25,26])
        ->whereNotIn('age', [11,12,13])
        
        ->orderBy('id')
        //->orderBy('id', 'desc')
        //->orderBy('id', 'asc')
        //->latest('id')  // Default Id
        //->oldest('id')  // Default Id
        ->get();

更多示例

// To Get All Result
$result = CSVQ::from("example.csv")->all();

// To Get All Sorted Result
$result = CSVQ::from("example.csv")->orderBy('id', 'desc')->all();

// To Get Specific Row
$result = CSVQ::from("example.csv")->where('id', 1)->row();

// To Get First Result
$result = CSVQ::from("example.csv")->where('id', 1)->first();

// To Get Last Result
$result = CSVQ::from("example.csv")->where('id', 1)->last();

// To Get nth row
$result = CSVQ::from("example.csv")->getNth(2); // [0-n]

// Check Is row exist
$result = CSVQ::from("example.csv")->where('id', 1)->hasData(); // boolean
$result = CSVQ::from("example.csv")->where('id', 1)->doesExist(); // boolean

// To Get All Sorted Result
$result = CSVQ::from("example.csv")->orderBy('id', 'desc')->all();

可用的 WHERE 操作符

  • =(默认操作符,可以省略)
  • >
  • <
  • <=
  • >=
  • !=

可用的排序操作符

  • ASC
  • DESC(默认操作符,可以省略)
  • asc
  • desc

Limit 和 Offset

您可以为查询结果添加条件并指定 limit 和 offset

$result = CSVQ::from("example.csv")
        ->select('*')
        ->orderBy('id')
        ->limit(10)
        //->limit(10, 2)    
        ->get();

聚合查询

您可以为查询结果添加条件并指定 limit 和 offset

// To Get Count
$result = CSVQ::from("example.csv")->count();

// To Get Sum
$result = CSVQ::from("example.csv")->sum('age');

// To Get Average
$result = CSVQ::from("example.csv")->avg('age');

// To Get row with minimum column value
$result = CSVQ::from("example.csv")->min('age');

// To Get row with maximum column value
$result = CSVQ::from("example.csv")->max('age');

支持

如果您发现了一个问题或有想法,请参考本节

作者