whizsid/arraybase

纯PHP运行时SQL-like查询语言,用于操作PHP数组数据。

1.0.1 2019-09-05 17:13 UTC

This package is auto-updated.

Last update: 2024-09-06 04:04:04 UTC


README

License: MIT Total Downloads Latest Stable Version Build: parsing Style CI: parsed

运行时SQL-like查询语言,用于操作PHP数组。完全使用纯PHP编写,不使用任何SQL引擎。注意:这不是任何类型的查询构建器。

安装

您可以使用以下命令通过composer包管理器安装arraybase。

composer require whizsid/arraybase

基础

创建ArrayBase实例

这是创建数组基础实例的方法。

use WhizSid\ArrayBase\AB;

$ab = new AB;

ArrayBase比其他SQL引擎更简单。

创建ArrayBase表

use WhizSid\ArrayBase\AB\Table;
use WhizSid\ArrayBase\AB\Table\Column;

$ab->createTable('customers',function(Table $tbl){
    $tbl->createColumn('cus_id',function(Column $clmn){
        $clmn->setType('integer')->setAutoIncrement();
    });
    $tbl->createColumn('cus_name',function(Column $clmn){
        $clmn->setType('varchar');
    });
    $tbl->createColumn('cus_phone',function(Column $clmn){
        $clmn->setType('varchar');
    })
});

或者使用数据数组。

$ab->createTable('tbl_another',[
	[
		'c_id'=>1,
		'ant_id'=>"A"
	],
	[
		'c_id'=>2,
		'ant_id'=>"B"
	]
]);

连接子句

use WhizSid\ArrayBase\AB\Query\Clause\Join;

$query = $ab->query();

$select = $query->select($ab->tbl_customer->as('cus'));

$select->join($ab->tbl_customer_cv->as('cv'))->on($query->cv->c_id,$query->cus->c_id);

$results = $select->execute();

WHERE子句

$select->where($query->cus->c_id,"4567")->and($query->cv->c_name,"my name");

限制

$select->limit(10,20);

排序

$select->orderBy($query->cus->c_name)->orderBy($query->cus->c_address,"desc");

选择查询

$selectQuery = $ab->query()->select(
	$ab->tbl_customer,
	$ab::groupConcat(AB_DISTINCT,$ab->tbl_facility->fac_code)->as('new_sum'),
	$ab->tbl_customer->c_id,
	$ab->tbl_another->ant_id,
	$ab->tbl_facility->fac_code
);

$selectQuery->join(AB_JOIN_INNER,$ab->tbl_facility)->on($ab->tbl_customer->c_id,'=',$ab->tbl_facility->c_id);
$selectQuery->join(AB_JOIN_INNER,$ab->tbl_another)->on($ab->tbl_customer->c_id,'=',$ab->tbl_another->c_id);
$selectQuery->orderBy($ab->tbl_customer->c_id,'desc');
$selectQuery->groupBy($ab->tbl_another->ant_id);
$selectQuery->where($ab->tbl_another->ant_id,'=',"A");
$selectQuery->limit(1);
$result = $selectQuery->execute()->fetchAssoc();

更新查询

$updateQuery = $ab->query()->update($ab->tbl_customer)->set($ab->tbl_customer->c_name,'Updated name');
$updateQuery->where($ab->tbl_another->ant_id,"B");
$updateQuery->join(AB_JOIN_INNER,$ab->tbl_another)->on($ab->tbl_another->c_id,'=',$ab->tbl_customer->c_id);
$updateQuery->limit(1);
$updateQuery->execute();

删除查询

$deleteQuery = $ab->query()->delete($ab->tbl_customer);
$deleteQuery->where($ab->tbl_another->ant_id,"B");
$deleteQuery->join(AB_JOIN_INNER,$ab->tbl_another)->on($ab->tbl_another->c_id,'=',$ab->tbl_customer->c_id);
$deleteQuery->limit(1);
$deleteQuery->execute();

所有示例在example/index.php文件中。

目标

将所有MySQL函数带到PHP中。

阅读完整文档