PHP的平面文件数据库。

v1.2 2017-08-27 17:51 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:09:15 UTC


README

PHP的平面文件数据库。

Logo

基于Wylst的FllatPrequel库。特别感谢Wylst。另外,感谢Alfred Xing,似乎是这两个库的主要贡献者。增加了对

安装

使用Composer安装Condense,如下所示

composer require lambdacasserole/condense

或者,如果您正在使用PHAR(请确保php.exe可执行文件在您的PATH中)

php composer.phar require lambdacasserole/condense

使用

要初始化一个新的数据库或加载现有的一个,执行以下操作。

$db = new Database('employees');

这将默认创建一个文件db/employees.dat或如果它已经存在则加载该文件。您可以通过这种方式更改平面文件数据库创建的路径。

$db = new Database('employees', '../storage');

构造函数还接受第三个参数,允许您指定用于加密数据库的密钥。

$db = new Database('secrets', '../private', 'my-secret-password');

再次加载数据库时,必须使用相同的密码。

创建

使用insert向数据库中添加记录(行)。

$hire = ['first_name' => 'Ethan', 'last_name' => 'Benson', 'salary' => 20000];
$employees->insert($hire);

检索

Condense提供了一些数据检索方法。

一个值

使用get方法。指定字段名、另一个字段名和值。它将返回第一个字段(在同一行中),第二个字段的值匹配给定值的值。

// Returns the salary of the first employee with the first name 'Ethan' (20000).
$employees->get('salary', 'first_name', 'Ethan');

字段子集

使用select方法。通过提供一个希望的字段名数组,返回表中的某些(或全部)字段。

// Returns the whole database.
$employees->select([]);

// Returns the first name of each employee, for example: 
// [['Ethan'],['Thomas'],['James']]
$employees->select(['first_name']);

更新

Condense提供了一些方法来更新数据库中的现有数据。

一个字段

使用to方法更新满足条件的任何行的单个字段。

// Change every employee with a first name 'Ethan' to have the surname 'Smithers'.
$employees->to('last_name', 'Smithers', 'first_name', 'Ethan');

一行

使用update方法通过索引更新行。

// Change the first row in the database completely.
$employees->update(0, ['first_name' => 'Alison', 'last_name' => 'Bradberry']);

删除

使用remove方法删除数据库中的行。

// Remove the first row in the database.
$employees->remove(0);

注意事项

这是一个平面文件数据库系统。它消除了设置和配置数据库服务器的烦恼,但引入了一些自己的

  • I/O会因为许多磁盘读写操作而非常
  • 加密数据库将极大地影响性能
  • 由于并发问题,可能出现错误
  • 错误配置的Web应用程序使用此库可能会意外地允许通过HTTP下载其数据库