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

Commit d54ee53

Browse files
committed
alpha version of the code
1 parent 74d1cd3 commit d54ee53

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ composer require michael-rubel/laravel-sql-function-repository
3030

3131
Publish the config:
3232
```bash
33-
php artisan vendor:publish --tag="package-template-config"
33+
php artisan vendor:publish --tag="sql-function-repository-config"
3434
```
3535

3636
## Testing

config/sql-function-repository.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
/*
66
|--------------------------------------------------------------------------
7-
| Package Configuration
7+
| SQL Function Repository Configuration
88
|--------------------------------------------------------------------------
99
|
10-
| Your config here.
10+
| Database connection & base select.
1111
|
1212
*/
1313

14-
'key' => 'value',
14+
'connection' => 'default',
15+
'select' => 'select * from',
1516

1617
];

src/SqlFunctionRepository.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MichaelRubel\SqlFunctionRepository;
6+
7+
use Illuminate\Support\Facades\DB;
8+
use MichaelRubel\SqlFunctionRepository\Traits\BuildsSqlFunctions;
9+
10+
class SqlFunctionRepository
11+
{
12+
use BuildsSqlFunctions;
13+
14+
/**
15+
* @param string|null $connection
16+
* @param string|null $select
17+
*/
18+
public function __construct(
19+
public ?string $connection = null,
20+
public ?string $select = null,
21+
) {
22+
if (is_null($this->connection)) {
23+
$this->connection = config('sql-function-repository.connection', 'default');
24+
}
25+
26+
if (is_null($this->select)) {
27+
$this->select = config('sql-function-repository.select', 'select * from');
28+
}
29+
}
30+
31+
/**
32+
* Execute the database function.
33+
*
34+
* @param string $name
35+
* @param array $parameters
36+
*
37+
* @return array
38+
*/
39+
public function runDatabaseFunction(string $name, array $parameters): array
40+
{
41+
return DB::connection($this->connection)->select(
42+
$this->buildSqlFunction($name, $parameters),
43+
$parameters
44+
);
45+
}
46+
}

src/Traits/BuildsSqlFunctions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MichaelRubel\SqlFunctionRepository\Traits;
6+
7+
trait BuildsSqlFunctions
8+
{
9+
/**
10+
* @param string $name
11+
* @param array $parameters
12+
*
13+
* @return string
14+
*/
15+
private function buildSqlFunction(string $name, array $parameters): string
16+
{
17+
$prepareForBindings = collect($parameters)->implode(
18+
fn ($param) => '?, '
19+
);
20+
21+
return $this->select . ' ' . $name . str($prepareForBindings)
22+
->replaceLast(', ', '')
23+
->start('(')
24+
->finish(')');
25+
}
26+
}

0 commit comments

Comments
 (0)