maer/file-db

可扩展的平面文件数据库,用PHP编写

0.3.2 2017-12-21 16:09 UTC

This package is auto-updated.

Last update: 2024-09-10 13:15:19 UTC


README

Build Status

这个库使得在您的应用程序中存储和检索数据集成为可能,而无需数据库。数据以json文件的形式存储在文件系统中,可以通过简单的查询构建器检索。

这并不是要取代高性能应用程序中的数据库,而是一种方便存储、过滤和检索较小数据集的方法。

安装

使用composer

composer require maer/file-db

实例化

要创建实例,您需要传递要使用的存储驱动程序

$driver = new SomeStorageDriver();

$db = new Maer\FileDB\FileDB($driver);

存储驱动程序

文件系统

文件系统存储数据,正如其名,在文件系统中。如果您想数据在请求之间持久化,则需要使用此驱动程序

$driver = new Maer\FileDB\Storage\FileSystem(
    '/absolute-path/to/your/storage/folder'
);

存储文件夹必须是可写的。

重要:由于数据将保存为json格式,因此此文件夹还应放置在外部文档根目录,否则人们可以直接访问数据。

内存

内存驱动程序仅在当前请求中将数据存储在内存中,并且不会在请求之间持久化。此驱动程序主要用于作为测试的模拟驱动程序。

$driver = new Maer\FileDB\Storage\Memory();

查询构建器

您可以拥有尽可能多的表(或更确切地说是集合)。每个表将存储在自己的文件中。

当您从File DB获取表时,您实际上正在获取一个QueryBuilderMaer\FileDB\QueryBuilder)的新实例,因此您可以开始构建查询。

$query = $db->table('people');

// or shorthand

$query = $db->people;

如果该表不存在,则在您首次存储数据时会自动创建。

插入

插入数据很简单。只需传递关联数组即可。

$id = $db->people->insert([
    'first_name'   => 'Chuck',
    'last_name'    => 'Norris',
    'masters'      => 'everything',
]);

如果插入成功,则返回新ID。如果插入失败,则返回null

关于ID的注意:当您添加条目且未传递ID(作为id传递)时,将生成一个唯一的随机16位十六进制字符串。

如果您传递了该表中已存在的ID,则查询将失败并返回null

批量插入

如果您想一次性插入多个条目,可以使用batchInsert(array $data)

$ids = $db->people->batchInsert([
    [
        'first_name' => 'Chuck',
        'last_name'  => 'Norris',
    ],
    [
        'first_name' => 'Jackie',
        'last_name'  => 'Chan',
    ]
]);

此方法返回所有生成的ID。

获取数据

以下大部分项将返回匹配条目的多维数组。

获取所有条目

$rows = $db->people->get();

获取第一个条目

返回第一个匹配的条目。

$row = $db->people->first();

查找

获取符合条件的第一条目。

// Find by ID (default)
$row = $db->people->find('123');

// Find by some other column
$row = $db->people->find('Chuck', 'first_name');
where

通常,您只想返回符合某些类型标准的一些特定条目。您可以通过在查询中添加一些“where”条件来实现此操作。

$rows = $db->people->where('masters', 'everything')->get();

上述操作将匹配所有具有masters列值为everything的条目。这等于:where('masters', '=', 'everything')

运算符

您可以使用许多其他运算符来缩小结果。

以下操作符的使用方法如下:where($column, $operator, $value)

=           Equal to (Loose type comparison)
!=          Not equal to (Loose type comparison)
===         Equal to (Strict type comparison)
!==         Not equal to (Strict type comparison)
<           Lower than
>           Higher than
<=          Lower or equal to
>=          Higher or equal to
*           Contains
=*          Starts with
*=          Ends with
in          Exists in list
!in         Not exists in list
regex       Match using regular expressions.
func        Match using a custom callback (closure as the third argument)
array_has   Exists in array  (if the value is an array)
!array_has  Not exists in array (if the value is an array)
has_col     The column exist
!has_col    The column does not exist

您可以为同一个查询添加任意数量的where条件。为了简化,您可以连锁使用它们

$result = $db->people->where('col1', '=', 'some value')
    ->where('col2', '!=', 'some other value')
    ...
    ->get();

排序

要按特定方式排序结果,可以使用orderBy($column, $order = 'asc')

// Ascending order:
$results = $db->people->orderBy('first_name');

// Descending order:
$results = $db->people->orderBy('first_name', 'desc');

限制

您可以限制返回的项目数量。

// Only get the 2 first matches
$results = $db->people->limit(2)->get();

偏移

如果您需要添加偏移量(例如用于分页),可以使用offset($offset)

// Get all results from the second match and forward.
$results = $db->people->offset(2)->get();

对象

默认情况下,所有项目将以关联数组的形式返回。如果您希望它们以对象的形式返回,可以使用方法asObj($class = 'stdClass')

// Default, converts the item to a StdClass
$item = $db->people->asObj()->first();

// Using your own entity class
$item => $db->people->asObj('Namespace\PersonClass')->find();

在传递自定义类时,项目将被传递给类的构造函数(作为一个数组),因此自定义类需要相应地填充其属性。

更新

要更新项目,请使用update(array $data)

$affected = $db->people
    ->where('first_name', 'Chuck')
    ->where('last_name', 'Norris')
    ->update([
        'middle_name' => 'The king',
    ]);

此方法返回受影响的项目数量。

不要忘记where子句,否则您将更新所有项目。

替换

此方法与update()的区别在于,它将替换完整的项目,除了ID。例如

$id = $db->people->insert([
    'foo'     => 'Lorem',
    'bar'     => 'Ipsum',
]);

$affected = $db->people
    ->where('id', $id)
    ->replace([
        'foo_bar' => 'Lorem Ipsum',
    ]);

$item = $db->people->find($id);
// Returns:
// [
//     'id'      => 1234,
//     'foo_bar' => 'Lorem Ipsum',
// ]

此方法返回受影响的项目数量。

不要忘记where子句,否则您将替换所有项目。

删除

删除项目

$affected = $db->people
    ->where('first_name', 'Chuck')
    ->delete();

此方法返回受影响的项目数量。

不要忘记where子句,否则您将删除所有项目。

截断

截断表(删除所有项目)

$success = $db->people->truncate();

此方法返回一个布尔值。成功时返回true,错误时返回false