soosyze / queryflatfile
Queryflatfile 是一个简单的数据库 PHP 库,不是 SQL
3.1.0
2022-12-11 10:04 UTC
Requires
- php: >=7.2
Requires (Dev)
- phpstan/phpstan: ^1.1
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^8.5
- rector/rector: ^0.12
- soosyze/php-cs-fixer-config: ^1.0
Suggests
- phpdocumentor/phpdocumentor: To generate documentation.
- phpmetrics/phpmetrics: To generate a code status report in HTML format.
README
- 🇬🇧 英文 README
- 🇫🇷 法语文档 README
关于
Queryflatfile 是一个用 PHP 编写的平面文件数据库库。默认以 JSON
格式存储您的数据,也支持 txt
、msgPack 和 igbinary 格式。使用类似 SQL 语法 QueryBuilder 操作数据。
摘要
要求
PHP 版本
扩展
内存要求
所需的最小内存量取决于您要处理的数据量以及操作类型。
文件和目录权限
在将存储您的数据的目录中写入和读取文件的权限。
安装
Composer
要使用 Composer 安装 Queryflatfile,您必须拥有安装程序或二进制文件 Composer
转到您的项目目录,打开命令提示符并运行以下命令
composer require soosyze/queryflatfile --no-dev
或者,如果您使用二进制文件,
php composer.phar require soosyze/queryflatfile --no-dev
简单示例
require __DIR__ . '/vendor/autoload.php'; use Soosyze\Queryflatfile\Schema; use Soosyze\Queryflatfile\Request; use Soosyze\Queryflatfile\TableBuilder; use Soosyze\Queryflatfile\Driver\Json; $sch = new Schema(__DIR__ . 'data', 'schema', new Json()); $req = new Request($sch); $sch->createTableIfNotExists('user', function(TableBuilder $table): void { $table->increments('id') $table->string('name') $table->string('firstname')->nullable(); }); $req->insertInto('user', [ 'name', 'firstname' ]) ->values([ 'NOEL', 'Mathieu' ]) ->values([ 'DUPOND', 'Jean' ]) ->values([ 'MARTIN', null ]) ->execute(); $data = $req->select('id', 'name') ->from('user') ->where('firstname', '=', 'Jean') ->fetch(); print_r($data); $sch->dropTableIfExists('user');
上面的示例将输出
Array
(
[id] => 2
[name] => DUPOND
)
方法
模式
dropSchema()
,getIncrement( string $tableName )
,getSchema()
,getTableSchema( string $tableName )
,hasColumn( string $tableName, $columnName )
,hasTable( string $tableName )
,setConfig( string $host, string $name = 'schema', DriverInterface $driver = null )
.
处理表
alterTable( string $tableName, callable $callback )
,createTable( string $tableName, callable $callback = null )
,createTableIfNotExists( string $tableName, callable $callback = null )
:boolean( string $name )
,char( string $name, $length = 1)
,date( string $name )
,dateTime( string $name )
,float( string $name )
,increments( string $name )
,integer( string $name )
,string( string $name, $length = 255)
,text( string $name )
.
dropTable( string $tableName )
,dropTableIfExists( string $tableName )
,truncateTable( string $tableName )
.
选择请求
select( string ...$columnNames )
,from( string $tableName )
,leftJoin( string $tableName, \Closure|string $column, string $condition = '', string $value = '' )
,rightJoin( string $tableName, \Closure|string $column, string $condition = '', string $value = '' )
,union( RequestInterface $union )
,unionAll( RequestInterface $union )
,orderBy( string $columnName, int $order = SORT_DESC|SORT_ASC )
,limit( int $limit, int $offset = 0 )
.
执行请求
insertInto( string $tableName, array $columnNames )
,values( array $rowValues )
,update( string $tableName, array $row )
,delete()
,execute()
执行数据的插入、修改和删除。
查询结果
fetch(): array
返回查询的第一个结果,fetchAll(): array
返回查询的所有结果,lists( string $columnName, string $key = null ): array
返回传入参数的列列表。
其中
where( string $columnName, string $condition, null|scalar $value )
,orWhere( string $columnName, string $condition, null|scalar $value )
,notWhere( string $columnName, string $condition, null|scalar $value )
,orNotWhere( string $columnName, string $condition, null|scalar $value )
.
支持的条件(===, ==, !=, <>, <, <=, >, >=, like, ilike, not like, not ilike)
其中
whereGroup( \Closure $columnName )
,orWhereGroup( \Closure $columnName )
,notWhereGroup( \Closure $columnName )
,orNotWhereGroup( \Closure $columnName )
.
Where between
between( string $columnName, $min, $max )
,orBetween( string $columnName, $min, $max )
,notBetween( string $columnName, $min, $max )
,orNotBetween( string $columnName, $min, $max )
.
Where in
in( string $columnName, array $values )
,orIn( string $columnName, array $values )
,notIn( string $columnName, array $values )
,orNotIn( string $columnName, array $values )
.
Where isNull
isNull( string $columnName )
,orIsNull( string $columnName )
,isNotNull( string $columnName )
,orIsNotNull( string $columnName )
.
Where regex
regex( string $columnName, string $pattern )
,orRegex( string $columnName, string $pattern )
,notRegex( string $columnName, string $pattern )
,orNotRegex( string $columnName, string $pattern )
.
使用
有关使用示例,请参阅用户文档。
许可协议
本项目采用MIT许可协议。