martimarkov / postgresify
Laravel的扩展PostgreSQL功能
v0.4.1
2018-10-21 16:17 UTC
Requires
- php: >=5.5.9
- asmiarowski/laravel-postgres: ^0.1.4
- illuminate/database: ~5.0
- illuminate/support: ~5.0
Requires (Dev)
- graham-campbell/testbench: ^3.1
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-05 06:14:19 UTC
README
注意
此包处于开发中。
目录
这是什么?
Postgresify 是一个针对 Laravel 和 Lumen 的包,它扩展了对一些有用 PostgreSQL 数据类型的支持。此包允许您在 Laravel 的 Schema Builder 中使用 PostgreSQL 的数据类型,如点、inet、圆形等,并在 Fluent/Eloquent 中检索有意义的日期。
示例迁移
Schema::create('hotel_search', function (Blueprint $table) {
// ...
$table->point('geocode_coordinates');
$table->ipAddress('visitor_ip_address');
$table->circle('search_area');
$table->dateRange('reservation_period');
$table->money('budget');
// ...
});
生活更简单,对吧?上述 PostgreSQL 类型用例消除了几个立即显而易见的烦恼。
- 点类型将地理坐标存储在一个字段中——而不是两个。
- IP地址类型将存储IPv4或IPv6——这里没有
VARCHAR
。 - 圆形类型将中心点和半径 <(x, y), r> 存储在一个字段中。没有这个,还可以有其他更“技巧性”的方法来存储与中心点相关的半径。
- 日期范围类型只存储日期范围。这与点类型一样,消除了第二个字段的必要性。
- 货币类型存储带符号的、与区域设置敏感的货币金额,范围从 -9200 万亿到 +9200 万亿!不再需要
DECIMAL(11,2)
或人们现在所做的一切。
现在让我们讨论这些附加类型提供的实际效用。PostgreSQL 为这些数据类型提供了良好的函数和运算符来有效地使用它们。这取决于您环境的架构,但结合这些类型和函数/运算符,您可以将在您的数据库服务器上卸载一些工作——这可能会更快,并减少应用程序代码中的某些责任。效果因人而异。请参阅此 StackExchange Q/A。
安装
要安装此包,您需要
- Laravel 5.1+ 或 Lumen 5.1+
- PHP 5.5.9+
此包适用于 PostgreSQL 9.4+。
将此包添加到您的 `
composer.json`
文件中作为依赖项
### Laravel
After installing via Composer, register Postgresify's ```DatabaseServiceProvider``` in your ```config/app.php```
configuration file like so:
```php
'providers' => [
// Other service providers...
Aejnsn\Postgresify\DatabaseServiceProvider::class,
],
```
### Basic Usage
If you would like code completion in your IDE for the PostgreSQL types made available via Postgresify, be sure your migrations (or other uses of Illuminate's Schema Builder) ```use``` the ```Aejnsn\Postgresify\Database\Schema\Blueprint``` class as in this example:
```php
<?php
use Aejnsn\Postgresify\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateHotelsTable extends Migration
{
public function up()
{
Schema::create('hotels', function (Blueprint $table) {
$table->dateRange('reservation');
// Build your schema using PostgreSQL's awesome types...
});
}
// ...
}
```
## Geometric Types
The geometric types in PostgreSQL can give you a ton of power while reducing complexity in your application.
> PostgreSQL's geometric types do not try to replace the need for [PostGIS](https://postgis.net.cn/) in cases where
> geographic calculations are performed. Remember this is geometric, not geographic--the Earth is not flat, or even a
> perfect sphere, it's an oblate spheroid (ellipsoid). That being said, don't use geometric types for heavy geographic
> work. Please use PostGIS if you determine a need, your accuracy depends on it. For those of you would-be geodesy
> aficionados, check out Charles F. F. Karney's work. Karney's algorithms are accurate to within 15 nm.
### Box
### Circle
### Line
### Line Segment
### Path
### Point
### Polygon
## Monetary Types
Methods of storing currency in an application/database make a hot topic for debate, and there is a ton of misinformation
on this topic. People start citing [GAAP](http://www.fasab.gov/accounting-standards/authoritative-source-of-gaap/), and
then it boils down to developers' non-standard preferences. There is too much uncertainty, and I just do not like it.
Let's look at some common methods for storing currency:
1. **Store as ```float```**. Don't do this, you'll have garbage for accuracy.
2. **Store as ```decimal```/```numeric```**. This is fine and handles the cases where you need to store fractions of a
cent. Decimal can be a hit to your performance in analytical operations.
3. **Store as ```integer``` using cents (or other currency's base unit) or use ```money```**. This is best, and works in
cases where you do **not** need fractions of a cent. PostgreSQL's ```money``` stores as an integer (of cents) but cleans
up the display and return of doing so. ```Money``` is more performant than ```decimal```. The range of ```money``` is
-92233720368547758.08 to +92233720368547758.07, so yeah, it will handle large amounts.
### Money
## Network Address Types
Network addresses can be a pain to work with. Imagine the use case where you would need to query all IP addresses of a
certain subnet mask. PostgreSQL has [Network Address Functions and Operators](https://postgresql.ac.cn/docs/9.4/static/functions-net.html)
for purposes like this.
> I made a [pull request](https://github.com/laravel/framework/pull/12884) for the IP and MAC address types in Laravel. So these two types will be in your initial Laravel installation (5.2.27+), and should work across all of Laravel's supported database systems. However, PostgreSQL, unlike other database systems, has its rich set of network address functions and operators built-in.
### IP Address
### MAC Address
### Netmask
## Range Types
Ranges are quite powerful. A range includes a lower-bound and an upper-bound, either of which can be inclusive or
exclusive. It would take four columns to build that functionality without a range type. Check out [PostgreSQL's Range
Functions and Operators documentation](https://postgresql.ac.cn/docs/9.4/static/functions-range.html).
### Date Range
### Integer Range
### Numeric Range
### Timestamp Range
### Timestamp Range w/ Timezone
## License
- This package is licensed under the [MIT license](https://raw.githubusercontent.com/aejnsn/postgresify/master/LICENSE).
## References
- [PostgreSQL 9.4 Manual](https://postgresql.ac.cn/docs/9.4/static/datatype.html)