This repository was archived by the owner on Mar 15, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +77
-4
lines changed
Expand file tree Collapse file tree 4 files changed +77
-4
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ composer require michael-rubel/laravel-sql-function-repository
3030
3131Publish 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
Original file line number Diff line number Diff line change 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];
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments