Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:sunaoka/laravel-postgres-extenti…
Browse files Browse the repository at this point in the history
…on into develop
  • Loading branch information
sunaoka committed Oct 20, 2020
2 parents 668989a + d4464ed commit 38d0b10
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Installation

```bash
composer require sunaoka/laravel-postgres-extention
composer require sunaoka/laravel-postgres-extension
```

## Features
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "sunaoka/laravel-postgres-extention",
"name": "sunaoka/laravel-postgres-extension",
"description": "Extended PostgreSQL driver for Laravel.",
"keywords": ["laravel", "PostgreSQL", "Eloquent", "Model", "driver"],
"type": "library",
Expand All @@ -12,6 +12,7 @@
],
"require": {
"php": "^7.3",
"ext-json": "*",
"illuminate/database": "^8.0"
},
"require-dev": {
Expand All @@ -37,6 +38,8 @@
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
Expand Down
6 changes: 6 additions & 0 deletions config/postgres-extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'json_encode_options' => JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION,
'information_schema_caching' => true,
];
16 changes: 1 addition & 15 deletions src/Eloquent/Casts/RangeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,10 @@ public function get($model, string $key, $value, array $attributes)
public function set($model, string $key, $value, array $attributes)
{
return [
$key => $this->serialize($value),
$key => ($value !== null) ? (string)$value : null,
];
}

/**
* @param Range|null $range
*
* @return string|null
*/
protected function serialize(?Range $range): ?string
{
if ($range === null) {
return null;
}

return (string)$range;
}

/**
* @param mixed $value
*
Expand Down
21 changes: 21 additions & 0 deletions src/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Sunaoka\LaravelPostgres\Eloquent\Concerns;

use Illuminate\Support\Facades\Config;

trait HasAttributes
{
/**
* Encode the given value as JSON.
*
* @param mixed $value
* @return string
*/
protected function asJson($value)
{
return json_encode($value, Config::get('postgres-extension.json_encode_options'));
}
}
2 changes: 2 additions & 0 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Model extends \Illuminate\Database\Eloquent\Model
{
use Concerns\HasAttributes;

public function newEloquentBuilder($query)
{
return new Builder($query);
Expand Down
16 changes: 16 additions & 0 deletions src/PostgresServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@ class PostgresServiceProvider extends ServiceProvider
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/postgres-extension.php', 'postgres-extension'
);

Connection::resolverFor('pgsql', function ($connection, $database, $prefix, $config) {
return new PostgresConnection($connection, $database, $prefix, $config);
});
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/postgres-extension.php' => config_path('postgres-extension.php'),
], 'postgres-extension');
}
}
24 changes: 24 additions & 0 deletions src/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Sunaoka\LaravelPostgres\Query\Grammars;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;

class PostgresGrammar extends \Illuminate\Database\Query\Grammars\PostgresGrammar
{
Expand Down Expand Up @@ -42,6 +44,28 @@ public function compileUpdate(Builder $query, array $values)
return trim($sql);
}

/**
* Prepare the bindings for an update statement.
*
* @param array $bindings
* @param array $values
* @return array
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
$values = collect($values)->map(function ($value, $column) {
return is_array($value) || ($this->isJsonSelector($column) && ! $this->isExpression($value))
? json_encode($value, Config::get('postgres-extension.json_encode_options'))
: $value;
})->all();

$cleanBindings = Arr::except($bindings, 'select');

return array_values(
array_merge($values, Arr::flatten($cleanBindings))
);
}

/**
* Compile a delete statement into SQL.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sunaoka\LaravelPostgres\Schema;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

class PostgresBuilder extends \Illuminate\Database\Schema\PostgresBuilder
{
Expand All @@ -16,6 +17,10 @@ class PostgresBuilder extends \Illuminate\Database\Schema\PostgresBuilder
*/
public function getColumnListing($table)
{
if (Config::get('postgres-extension.information_schema_caching') !== true) {
return parent::getColumnListing($table);
}

return Cache::rememberForever(
__METHOD__ . $table,
function () use ($table) {
Expand Down

0 comments on commit 38d0b10

Please sign in to comment.