253 lines
7.1 KiB
PHP
253 lines
7.1 KiB
PHP
<?php
|
||
|
||
namespace Dipper\Foundation\Database;
|
||
|
||
use Closure;
|
||
use Illuminate\Support\Arr;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Database\Connection;
|
||
use Illuminate\Support\ServiceProvider;
|
||
use Dipper\Foundation\Database\MySqlGrammar;
|
||
use Dipper\Foundation\Database\PostgresGrammar;
|
||
use Illuminate\Database\Connectors\ConnectionFactory;
|
||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||
use Illuminate\Database\Schema\Blueprint as SchemaBlueprint;
|
||
|
||
class DatabaseServiceProvider extends ServiceProvider
|
||
{
|
||
/**
|
||
* Register the modified database connection
|
||
*
|
||
* @return void
|
||
*/
|
||
public function register()
|
||
{
|
||
DB::extend('pgsql', function ($config, $name) {
|
||
$factory = new ConnectionFactory($this->app);
|
||
$connection = $factory->make($config, $name);
|
||
$connection->setSchemaGrammar(new PostgresSchemaGrammar);
|
||
$connection->setQueryGrammar(new PostgresGrammar);
|
||
return $connection;
|
||
});
|
||
|
||
DB::extend('mysql', function ($config, $name) {
|
||
$factory = new ConnectionFactory($this->app);
|
||
$connection = $factory->make($config, $name);
|
||
$connection->setSchemaGrammar(new MysqlSchemaGrammar);
|
||
$connection->setQueryGrammar(new MysqlGrammar);
|
||
return $connection;
|
||
});
|
||
|
||
QueryBuilder::macro('upsert', $this->macroUpsert());
|
||
QueryBuilder::macro('replace', $this->macroReplace());
|
||
QueryBuilder::macro('insertUpdate', $this->macroInsertUpdate());
|
||
QueryBuilder::macro('createOrIgnore', $this->macroCreateOrIgnore());
|
||
QueryBuilder::macro('createNotExist', $this->macroCreateNotExist());
|
||
QueryBuilder::macro('updateBatch', $this->macroUpdateBatch());
|
||
SchemaBlueprint::macro('comment', $this->macroSchemaComment());
|
||
SchemaBlueprint::macro('setIncrement', $this->macroSchemaSetIncrement());
|
||
}
|
||
|
||
/**
|
||
* macro an replace into statement into SQL.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroReplace()
|
||
{
|
||
return function (array $values) {
|
||
if (empty($values)) {
|
||
return false;
|
||
}
|
||
|
||
if (!is_array(reset($values))) {
|
||
$values = [$values];
|
||
} else {
|
||
foreach ($values as $key => $value) {
|
||
ksort($value);
|
||
|
||
$values[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $this->connection->affectingStatement(
|
||
$this->grammar->compileReplace($this, $values),
|
||
$this->cleanBindings(Arr::flatten($values, 1))
|
||
);
|
||
};
|
||
}
|
||
|
||
/**
|
||
* macro an insert ON DUPLICATE KEY UPDATE statement into SQL
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroInsertUpdate()
|
||
{
|
||
return function (array $values) {
|
||
if (empty($values)) {
|
||
return false;
|
||
}
|
||
|
||
if (!is_array(reset($values))) {
|
||
$values = [$values];
|
||
} else {
|
||
foreach ($values as $key => $value) {
|
||
ksort($value);
|
||
|
||
$values[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $this->connection->affectingStatement(
|
||
$this->grammar->compileInsertUpdate($this, $values),
|
||
$this->cleanBindings(Arr::flatten($values, 1))
|
||
);
|
||
};
|
||
}
|
||
|
||
/**
|
||
* macro an insert ignore statement into SQL.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroCreateOrIgnore()
|
||
{
|
||
return function (array $values) {
|
||
if (empty($values)) {
|
||
return false;
|
||
}
|
||
|
||
if (!is_array(reset($values))) {
|
||
$values = [$values];
|
||
} else {
|
||
foreach ($values as $key => $value) {
|
||
ksort($value);
|
||
|
||
$values[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $this->connection->affectingStatement(
|
||
$this->grammar->compileCreateOrIgnore($this, $values),
|
||
$this->cleanBindings(Arr::flatten($values, 1))
|
||
);
|
||
};
|
||
}
|
||
|
||
/**
|
||
* macro an insert ignore statement into SQL.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroCreateNotExist()
|
||
{
|
||
return function (array $values, array $where) {
|
||
if (empty($values)) {
|
||
return false;
|
||
}
|
||
|
||
if (empty($where)) {
|
||
return false;
|
||
}
|
||
|
||
if (!is_array(reset($values))) {
|
||
$values = [$values];
|
||
} else {
|
||
foreach ($values as $key => $value) {
|
||
ksort($value);
|
||
|
||
$values[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $this->connection->affectingStatement(
|
||
$this->grammar->compileCreateNotExist($this, $values, $where),
|
||
$this->cleanBindings(Arr::flatten($values, 1))
|
||
);
|
||
};
|
||
}
|
||
|
||
/**
|
||
* macro an update batch statement into SQL.
|
||
* 批量更新
|
||
* 默认以ID为条件更新,如果没有ID则以第一个字段为条件
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroUpsert()
|
||
{
|
||
return function (array $values, $fields = ['id'], $doNoting = false) {
|
||
if (empty($values)) {
|
||
return false;
|
||
}
|
||
|
||
if (!is_array(reset($values))) {
|
||
$values = [$values];
|
||
} else {
|
||
foreach ($values as $key => $value) {
|
||
$values[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $this->connection->affectingStatement(
|
||
$this->grammar->compileUpsert($this, $values, $fields, $doNoting),
|
||
$this->cleanBindings(Arr::flatten($values, 1))
|
||
);
|
||
};
|
||
}
|
||
|
||
/**
|
||
* macro an update batch statement into SQL.
|
||
* 批量更新
|
||
* 默认以ID为条件更新,如果没有ID则以第一个字段为条件
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroUpdateBatch()
|
||
{
|
||
return function (array $values, $filed = 'id') {
|
||
if (empty($values)) {
|
||
return false;
|
||
}
|
||
|
||
if (!is_array(reset($values))) {
|
||
$values = [$values];
|
||
} else {
|
||
foreach ($values as $key => $value) {
|
||
$values[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $this->connection->affectingStatement(
|
||
$this->grammar->compileUpdateBatch($this, $values, $filed),
|
||
$this->cleanBindings(Arr::flatten($values, 1))
|
||
);
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 表备注
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroSchemaComment()
|
||
{
|
||
return function ($comment) {
|
||
return $this->addCommand('comment', compact('comment'));
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 自增开始
|
||
*
|
||
* @return void
|
||
*/
|
||
public function macroSchemaSetIncrement()
|
||
{
|
||
return function ($number) {
|
||
return $this->addCommand('setIncrement', compact('number'));
|
||
};
|
||
}
|
||
}
|