-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use strategy design pattern instead of all database methods…
… in one file
- Loading branch information
1 parent
851b167
commit 29f5e3a
Showing
7 changed files
with
138 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy; | ||
|
||
abstract class BackupTablesStrategy | ||
{ | ||
abstract public function backup($newTableName, $table): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy; | ||
|
||
|
||
use Illuminate\Support\Facades\DB; | ||
use WatheqAlshowaiter\BackupTables\Constants; | ||
|
||
class MariaDbBackupStrategy extends BackupTablesStrategy | ||
{ | ||
public function backup($newTableName, $table): array | ||
{ | ||
DB::statement(/**@lang MariaDB */ "CREATE TABLE $newTableName AS SELECT * FROM $table"); | ||
|
||
return [ | ||
'response' => "Table '$table' completed backup successfully.", | ||
'newCreatedTables' => "Newly created table: $newTableName", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy; | ||
|
||
|
||
use Illuminate\Support\Facades\DB; | ||
use WatheqAlshowaiter\BackupTables\Constants; | ||
|
||
class MysqlBackupStrategy extends BackupTablesStrategy | ||
{ | ||
public function backup($newTableName, $table): array | ||
{ | ||
DB::statement(/**@lang MariaDB*/ "CREATE TABLE $newTableName AS SELECT * FROM $table"); | ||
|
||
return [ | ||
'response' => "Table '$table' completed backup successfully.", | ||
'newCreatedTables' => "Newly created table: $newTableName", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy; | ||
|
||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
class PostgresBackupStrategy extends BackupTablesStrategy | ||
{ | ||
public function backup($newTableName, $table): array | ||
{ | ||
DB::statement(/**@lang PostgreSQL */ "CREATE TABLE $newTableName AS SELECT * FROM $table"); | ||
|
||
|
||
return [ | ||
'response' => "Table '$table' completed backup successfully.", | ||
'newCreatedTables' => "Newly created table: $newTableName", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy; | ||
|
||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
class SqlServerBackupStrategy extends BackupTablesStrategy | ||
{ | ||
public function backup($newTableName, $table): array | ||
{ | ||
DB::statement(/**@lang TSQL*/ "SELECT * INTO $newTableName FROM $table"); | ||
|
||
return [ | ||
'response' => "Table '$table' completed backup successfully.", | ||
'newCreatedTables' => "Newly created table: $newTableName", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy; | ||
|
||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
class SqliteBackupStrategy extends BackupTablesStrategy | ||
{ | ||
|
||
public function backup($newTableName, $table): array | ||
{ | ||
DB::statement(/**@lang SQLite */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;"); | ||
DB::statement(/**@lang SQLite */ "INSERT INTO $newTableName SELECT * FROM $table"); | ||
|
||
return [ | ||
'response' => "Table '$table' completed backup successfully.", | ||
'newCreatedTables' => "Newly created table: $newTableName", | ||
]; | ||
} | ||
} |