ronnievisser/laravel-postgres-ext

Laravel 5.x 扩展 PostgreSQL 驱动

1.2 2017-07-12 12:22 UTC

This package is auto-updated.

Last update: 2024-09-13 23:34:11 UTC


README

这是一个从 yurykabanov/laravel-postgres-ext 克隆的项目,并支持 Laravel 5.4。

该项目受到了 PostgreSQL 支持但 Laravel 不支持的功能的启发。不幸的是,这些功能没有被官方仓库接受(如 这个),并且开发人员被告知要使用原始查询,这在我的观点中是完全错误的解决方案。

要求

  1. PHP >= 5.6 或 HHVM
  2. PostgreSQL。显然,它必须支持您想要使用的特定功能。例如,要使用 视图,它至少需要 9.1 版本,要使用 upsert,它至少需要 9.5 版本(当前稳定版)。

安装

  1. 运行 composer require ronnievisser/laravel-postgres-ext 安装此包。
  2. 将数据库服务提供程序从原始 Illuminate\Database\DatabaseServiceProvider::class 更改为 RonnieVisser\Database\DatabaseServiceProvider::class
  3. 模型应扩展 RonnieVisser\Database\Eloquent\Model 而不是 Illuminate\Database\Eloquent\Model

可用的功能

UPSERT

UPSERT (INSERT ON CONFLICT UPDATE) 从 PostgreSQL 9.5 版本开始支持,可以通过调用

Model::upsert($arrayOfAttibutes, $uniqueField)

与原始的 insert 方法一样,upsert 可以管理多个记录。

各种索引类型

PostgreSQL 支持 多种索引类型btreehashgistspgistginbrin(从 9.5 版本开始)以及其他索引相关功能(例如,索引可以并发创建,即无需锁定表)。此包支持创建所有当前支持的索引方法。

可以使用与原始相同的语法创建索引

$table->index('column_name');

但现在接受额外的参数

$table->index('column_name', 'index_name', $methodName, $arrayOfOptions);

其中 $methodName 是前面列出的方法之一,$arrayOfOptions 是包含不同选项(如并发性和唯一性)的数组。

示例:

// CREATE INDEX CONCURRENTLY ... USING GIST ...
$table->index('column_name', 'index_name', 'gist', [ 'concurrently' => true ]);
// CREATE UNIQUE INDEX ... USING BTREE ...
$table->index('column_name', 'index_name', 'gin',  [ 'unique' => true ]);

注意,有两种方法可以使列唯一:使用 约束索引更多信息)。Laravel 使用 约束 来使列唯一,这种行为在 $table->unique() 中保持不变,但您也可以使用 $table->index($col, $index, $method, [ 'unique' => true ]) 创建 唯一索引

视图

PostgreSQL 支持 视图(从 9.1 版本开始)和 物化视图(从 9.3 版本开始)。可以使用 DB::statement() 创建它们,但使用一些别名来管理它们更方便。

可以使用以下语句创建视图

// create non-materialized view using specified select statement
Schema::createView('some_view', 'select 1 as some_value');
// create materialized view using specified select statement
Schema::createView('some_view', 'select 1 as some_value', true);

和删除

// create non-materialized view using specified select statement
Schema::dropView('some_view');

到目前为止,它不支持某些查询构建器,因为视图的 SELECT 语句可能(通常 )非常复杂。

Jsonb 运算符

Laravel 声称它 确实 支持 jsonb 类型以及像 ??|?& 这样的 jsonb 运算符。但是,由于它们在预处理语句中被视为参数,因此无法在查询中使用它们。此包自动将这些运算符包装在相应的函数中(请注意,?| 也用于其他类型--目前不支持此行为)。

按分组集、rollup、cube 分组

官方文档中描述的可用分组表达式 描述

// GROUP BY GROUPING SETS ((brand), (size), ())
DB::table('some_table')->groupByGroupingSets('brand', 'size', null);

// GROUP BY ROLLUP (e1, e2, e3)
DB::table('some_table')->groupByRollup('e1', 'e2', 'e3');

// GROUP BY CUBE (e1, e2, e3)
DB::table('some_table')->groupByCube('e1', 'e2', 'e3');