Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 2103c9e

Browse files
committed
refactoring
1 parent 7eccf5d commit 2103c9e

File tree

4 files changed

+97
-30
lines changed

4 files changed

+97
-30
lines changed

config/sql-function-repository.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
4+
35
return [
46

57
/*
68
|--------------------------------------------------------------------------
7-
| SQL Function Repository Configuration
9+
| Database Connection Name
10+
|--------------------------------------------------------------------------
11+
|
12+
| Database connection name to use by the repository.
13+
|
14+
| Default: `pgsql`
15+
|
16+
*/
17+
18+
'connection' => DB::getDefaultConnection(),
19+
20+
/*
21+
|--------------------------------------------------------------------------
22+
| Default select statement
823
|--------------------------------------------------------------------------
924
|
10-
| Database connection & base select.
25+
| Default: `select * from `
1126
|
1227
*/
1328

14-
'connection' => 'default',
15-
'select' => 'select * from public',
29+
'select' => 'select * from ',
1630

1731
];
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MichaelRubel\SqlFunctionRepository\Repositories;
6+
7+
use Illuminate\Support\Facades\DB;
8+
use MichaelRubel\SqlFunctionRepository\SqlFunctionRepository;
9+
10+
class PostgresqlFunctionRepository implements SqlFunctionRepository
11+
{
12+
/**
13+
* @param string|null $connection
14+
* @param string|null $select
15+
*/
16+
public function __construct(
17+
public ?string $connection = null,
18+
public ?string $select = null,
19+
) {
20+
if (is_null($this->connection)) {
21+
$this->connection = config('sql-function-repository.connection', DB::getDefaultConnection());
22+
}
23+
24+
if (is_null($this->select)) {
25+
$this->select = config('sql-function-repository.select', 'select * from ');
26+
}
27+
}
28+
29+
/**
30+
* Execute the database function.
31+
*
32+
* @param string $name
33+
* @param array $parameters
34+
*
35+
* @return array
36+
*/
37+
public function runDatabaseFunction(string $name, array $parameters = []): array
38+
{
39+
return DB::connection($this->connection)->select(
40+
$this->buildSqlFunction($name, $parameters),
41+
$parameters
42+
);
43+
}
44+
45+
/**
46+
* @param string $name
47+
* @param array $parameters
48+
*
49+
* @return string
50+
*/
51+
protected function buildSqlFunction(string $name, array $parameters): string
52+
{
53+
$prepareForBindings = collect($parameters)->implode(
54+
fn ($param) => '?, '
55+
);
56+
57+
return $this->select . $name . str($prepareForBindings)
58+
->replaceLast(', ', '')
59+
->start('(')
60+
->finish(')');
61+
}
62+
}

src/SqlFunctionRepositoryServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace MichaelRubel\SqlFunctionRepository;
66

7+
use MichaelRubel\SqlFunctionRepository\Repositories\PostgresqlFunctionRepository;
8+
use MichaelRubel\SqlFunctionRepository\Traits\ResolvesDatabaseDriver;
79
use Spatie\LaravelPackageTools\Package;
810
use Spatie\LaravelPackageTools\PackageServiceProvider;
911

1012
class SqlFunctionRepositoryServiceProvider extends PackageServiceProvider
1113
{
14+
use ResolvesDatabaseDriver;
15+
1216
/**
1317
* Configure the package.
1418
*
@@ -22,4 +26,17 @@ public function configurePackage(Package $package): void
2226
->name('laravel-sql-function-repository')
2327
->hasConfigFile();
2428
}
29+
30+
/**
31+
* Register the bindings.
32+
*
33+
* @return void
34+
*/
35+
public function packageRegistered(): void
36+
{
37+
$this->app->singleton(
38+
SqlFunctionRepository::class,
39+
PostgresqlFunctionRepository::class
40+
);
41+
}
2542
}

src/Traits/BuildsSqlFunctions.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)