Skip to content

Commit 3dc4c9a

Browse files
committed
Use DB::unprepared
1 parent 092a753 commit 3dc4c9a

File tree

8 files changed

+69
-19
lines changed

8 files changed

+69
-19
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace KitLoong\MigrationsGenerator\Migration\Blueprint;
4+
5+
use KitLoong\MigrationsGenerator\Migration\Blueprint\Support\MethodStringHelper;
6+
use KitLoong\MigrationsGenerator\Migration\Blueprint\Support\Stringable;
7+
8+
/**
9+
* Create migration lines with `DB::unprepared`.
10+
*
11+
* eg 1:
12+
* ```
13+
* DB::statement("CREATE PROCEDURE myProcedure() BEGIN SELECT * from table END");
14+
* ```
15+
*
16+
* eg 2:
17+
* ```
18+
* DB::connection('sqlite')->statement("CREATE PROCEDURE myProcedure() BEGIN SELECT * from table END");
19+
* ```
20+
*/
21+
class DBUnpreparedBlueprint implements WritableBlueprint
22+
{
23+
use Stringable;
24+
use MethodStringHelper;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $sql;
30+
31+
/**
32+
* DBStatementBlueprint constructor.
33+
*
34+
* @param string $sql The SQL statement.
35+
*/
36+
public function __construct(string $sql)
37+
{
38+
$this->sql = $sql;
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function toString(): string
45+
{
46+
$method = $this->connection('DB', 'unprepared');
47+
$query = $this->escapeDoubleQuote($this->sql);
48+
return "$method(\"$query\");";
49+
}
50+
}

src/Migration/ProcedureMigration.php

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

55
use Carbon\Carbon;
66
use Illuminate\Support\Collection;
7-
use KitLoong\MigrationsGenerator\Migration\Blueprint\DBStatementBlueprint;
7+
use KitLoong\MigrationsGenerator\Migration\Blueprint\DBUnpreparedBlueprint;
88
use KitLoong\MigrationsGenerator\Migration\Enum\MigrationFileType;
99
use KitLoong\MigrationsGenerator\Migration\Writer\MigrationWriter;
1010
use KitLoong\MigrationsGenerator\Migration\Writer\SquashWriter;
@@ -70,22 +70,22 @@ public function writeToTemp(Procedure $procedure): void
7070
* Generates `up` db statement for stored procedure.
7171
*
7272
* @param \KitLoong\MigrationsGenerator\Schema\Models\Procedure $procedure
73-
* @return \KitLoong\MigrationsGenerator\Migration\Blueprint\DBStatementBlueprint
73+
* @return \KitLoong\MigrationsGenerator\Migration\Blueprint\DBUnpreparedBlueprint
7474
*/
75-
private function up(Procedure $procedure): DBStatementBlueprint
75+
private function up(Procedure $procedure): DBUnpreparedBlueprint
7676
{
77-
return new DBStatementBlueprint($procedure->getDefinition());
77+
return new DBUnpreparedBlueprint($procedure->getDefinition());
7878
}
7979

8080
/**
8181
* Generates `down` db statement for stored procedure.
8282
*
8383
* @param \KitLoong\MigrationsGenerator\Schema\Models\Procedure $procedure
84-
* @return \KitLoong\MigrationsGenerator\Migration\Blueprint\DBStatementBlueprint
84+
* @return \KitLoong\MigrationsGenerator\Migration\Blueprint\DBUnpreparedBlueprint
8585
*/
86-
private function down(Procedure $procedure): DBStatementBlueprint
86+
private function down(Procedure $procedure): DBUnpreparedBlueprint
8787
{
88-
return new DBStatementBlueprint($procedure->getDropDefinition());
88+
return new DBUnpreparedBlueprint($procedure->getDropDefinition());
8989
}
9090

9191
/**

tests/Feature/MariaDB/MariaDBTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function dropAllProcedures(): void
7070
$procedures = DB::select("SHOW PROCEDURE STATUS where DB='" . config('database.connections.mariadb.database') . "'");
7171

7272
foreach ($procedures as $procedure) {
73-
DB::statement("DROP PROCEDURE IF EXISTS " . $procedure->Name);
73+
DB::unprepared("DROP PROCEDURE IF EXISTS " . $procedure->Name);
7474
}
7575
}
7676
}

tests/Feature/MySQL57/MySQL57TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function dropAllProcedures(): void
7171
$procedures = DB::select("SHOW PROCEDURE STATUS where DB='" . config('database.connections.mysql57.database') . "'");
7272

7373
foreach ($procedures as $procedure) {
74-
DB::statement("DROP PROCEDURE IF EXISTS " . $procedure->Name);
74+
DB::unprepared("DROP PROCEDURE IF EXISTS " . $procedure->Name);
7575
}
7676
}
7777
}

tests/Feature/MySQL8/MySQL8TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function dropAllProcedures(): void
7070
$procedures = DB::select("SHOW PROCEDURE STATUS where DB='" . config('database.connections.mysql8.database') . "'");
7171

7272
foreach ($procedures as $procedure) {
73-
DB::statement("DROP PROCEDURE IF EXISTS " . $procedure->Name);
73+
DB::unprepared("DROP PROCEDURE IF EXISTS " . $procedure->Name);
7474
}
7575
}
7676
}

tests/Feature/PgSQL/PgSQLTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function dropAllProcedures(): void
9696
);
9797

9898
foreach ($procedures as $procedure) {
99-
DB::statement("DROP PROCEDURE IF EXISTS " . $procedure->proname);
99+
DB::unprepared("DROP PROCEDURE IF EXISTS " . $procedure->proname);
100100
}
101101
}
102102
}

tests/Feature/SQLSrv/SQLSrvTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function dropAllProcedures(): void
9797
$procedures = $this->getAllProcedures();
9898

9999
foreach ($procedures as $procedure) {
100-
DB::statement("DROP PROCEDURE IF EXISTS " . $procedure->name);
100+
DB::unprepared("DROP PROCEDURE IF EXISTS " . $procedure->name);
101101
}
102102
}
103103

tests/resources/database/migrations/general/2020_03_21_000001_expected_create_quoted_name_db_proc.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ public function up()
1919
{
2020
switch (DB::getDriverName()) {
2121
case Driver::MYSQL():
22-
DB::statement(
22+
DB::unprepared(
2323
"CREATE PROCEDURE findNameWithHyphen[db]()
2424
BEGIN
25-
SELECT * from ".$this->quoteIdentifier('name-with-hyphen-[db]').";
25+
SELECT * from " . $this->quoteIdentifier('name-with-hyphen-[db]') . ";
2626
END"
2727
);
2828
break;
2929
case Driver::PGSQL():
30-
DB::statement(
30+
DB::unprepared(
3131
"CREATE PROCEDURE findNameWithHyphen[db]()
3232
language plpgsql
3333
as $$
3434
BEGIN
35-
SELECT * from ".$this->quoteIdentifier('name-with-hyphen-[db]').";
35+
SELECT * from " . $this->quoteIdentifier('name-with-hyphen-[db]') . ";
3636
END;$$"
3737
);
3838
break;
3939
case Driver::SQLSRV():
40-
DB::statement(
40+
DB::unprepared(
4141
"CREATE PROCEDURE findNameWithHyphen[db]
4242
AS
43-
SELECT * from ".$this->quoteIdentifier('name-with-hyphen-[db]').";"
43+
SELECT * from " . $this->quoteIdentifier('name-with-hyphen-[db]') . ";"
4444
);
4545
break;
4646
default:
@@ -54,6 +54,6 @@ public function up()
5454
*/
5555
public function down()
5656
{
57-
DB::statement("DROP PROCEDURE IF EXISTS findNameWithHyphen[db]");
57+
DB::unprepared("DROP PROCEDURE IF EXISTS findNameWithHyphen[db]");
5858
}
5959
}

0 commit comments

Comments
 (0)