Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezasalehizadeh committed Mar 27, 2023
1 parent e4f116f commit 7648350
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 68 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"license": "MIT",
"autoload": {
"psr-4": {
"Alirezasalehizadeh\\QuickMigration\\": "src/",
"Alirezasalehizadeh\\QuickMigration\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Alirezasalehizadeh\\QuickMigration\\Test\\": "test/"
}
},
Expand Down
52 changes: 52 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Alirezasalehizadeh\QuickMigration\Command;

use Alirezasalehizadeh\QuickMigration\Enums\Command as EnumsCommand;

class Command
{

private EnumsCommand $name;

private string $pattern = '';

private array $includes = [];


public function getName()
{
return $this->name->value;
}

public function setName(EnumsCommand $name)
{
$this->name = $name;

return $this;
}

public function getPattern()
{
return $this->pattern;
}

public function setPattern(string $pattern)
{
$this->pattern = $pattern;

return $this;
}

public function getIncludes()
{
return $this->includes;
}

public function setIncludes(array $includes)
{
$this->includes = $includes;

return $this;
}
}
14 changes: 0 additions & 14 deletions src/Command/CommandGenerator.php

This file was deleted.

5 changes: 4 additions & 1 deletion src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

namespace Alirezasalehizadeh\QuickMigration\Command;

use Alirezasalehizadeh\QuickMigration\Command\Command;

interface CommandInterface
{
public function generate() : string;
public function getCommand(): Command;
}
20 changes: 14 additions & 6 deletions src/Command/Commands/CreateTableCommand.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Command\Commands;

use Alirezasalehizadeh\QuickMigration\Command\CommandGenerator;
use Alirezasalehizadeh\QuickMigration\Command\Command;
use Alirezasalehizadeh\QuickMigration\Command\CommandInterface;
use Alirezasalehizadeh\QuickMigration\Enums\Command as EnumsCommand;

class CreateTableCommand extends CommandGenerator
class CreateTableCommand extends Command implements CommandInterface
{

private $database, $table, $sqlCommands;

protected $pattern = "CREATE TABLE `%s`.%s (%s)";
protected $pattern = "%s `%s`.`%s` (%s)";

public function __construct(string $database, string $table, array $sqlCommands)
{
Expand All @@ -17,9 +19,15 @@ public function __construct(string $database, string $table, array $sqlCommands)
$this->sqlCommands = $sqlCommands;
}

public function generate(): string
public function getCommand() : self
{
$sqlString = implode(" ,", $this->sqlCommands);
return sprintf($this->pattern, $this->database, $this->table, $sqlString);
return $this
->setName(EnumsCommand::Create)
->setPattern($this->pattern)
->setIncludes([
'database' => $this->database,
'table' => $this->table,
'sqlCommands' => $this->sqlCommands,
]);
}
}
18 changes: 13 additions & 5 deletions src/Command/Commands/DropIfExistsTableCommand.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Command\Commands;

use Alirezasalehizadeh\QuickMigration\Command\CommandGenerator;
use Alirezasalehizadeh\QuickMigration\Command\Command;
use Alirezasalehizadeh\QuickMigration\Command\CommandInterface;
use Alirezasalehizadeh\QuickMigration\Enums\Command as EnumsCommand;

class DropIfExistsTableCommand extends CommandGenerator
class DropIfExistsTableCommand extends Command implements CommandInterface
{

private $database, $table;

protected $pattern = "DROP TABLE IF EXISTS `%s`.`%s`";
protected $pattern = "%s `%s`.`%s`";

public function __construct(string $database, string $table)
{
$this->database = $database;
$this->table = $table;
}

public function generate(): string
public function getCommand() : self
{
return sprintf($this->pattern, $this->database, $this->table);
return $this
->setName(EnumsCommand::DropTableIfExists)
->setPattern($this->pattern)
->setIncludes([
'database' => $this->database,
'table' => $this->table,
]);
}
}
18 changes: 13 additions & 5 deletions src/Command/Commands/DropTableCommand.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Command\Commands;

use Alirezasalehizadeh\QuickMigration\Command\CommandGenerator;
use Alirezasalehizadeh\QuickMigration\Command\Command;
use Alirezasalehizadeh\QuickMigration\Command\CommandInterface;
use Alirezasalehizadeh\QuickMigration\Enums\Command as EnumsCommand;

class DropTableCommand extends CommandGenerator
class DropTableCommand extends Command implements CommandInterface
{

private $database, $table;

protected $pattern = "DROP TABLE `%s`.`%s`";
protected $pattern = "%s `%s`.`%s`";

public function __construct(string $database, string $table)
{
$this->database = $database;
$this->table = $table;
}

public function generate(): string
public function getCommand() :self
{
return sprintf($this->pattern, $this->database, $this->table);
return $this
->setName(EnumsCommand::Drop)
->setPattern($this->pattern)
->setIncludes([
'database' => $this->database,
'table' => $this->table,
]);
}
}
10 changes: 10 additions & 0 deletions src/Enums/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Alirezasalehizadeh\QuickMigration\Enums;

enum Command: string
{
case Create = "CREATE TABLE";
case Drop = "DROP TABLE";
case DropTableIfExists = "DROP TABLE IF EXISTS";
}
23 changes: 13 additions & 10 deletions src/Migration.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php
namespace Alirezasalehizadeh\QuickMigration;

use PDO;
use Alirezasalehizadeh\QuickMigration\MigrationInterface;
use Alirezasalehizadeh\QuickMigration\Command\Commands\DropTableCommand;
use Alirezasalehizadeh\QuickMigration\Command\Commands\CreateTableCommand;
use Alirezasalehizadeh\QuickMigration\Command\Commands\DropIfExistsTableCommand;
use Alirezasalehizadeh\QuickMigration\Command\Commands\DropTableCommand;
use Alirezasalehizadeh\QuickMigration\MigrationInterface;
use Alirezasalehizadeh\QuickMigration\Translation\TranslateManager;
use PDO;
use Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\ColumnTranslateManager;
use Alirezasalehizadeh\QuickMigration\Translation\CommandTranslator\Translators\DropTableCommandTranslator;
use Alirezasalehizadeh\QuickMigration\Translation\CommandTranslator\Translators\CreateTableCommandTranslator;
use Alirezasalehizadeh\QuickMigration\Translation\CommandTranslator\Translators\DropIfExistsTableCommandTranslator;

abstract class Migration implements MigrationInterface
{
Expand All @@ -28,13 +31,13 @@ public function set(): array

public function drop(string $table)
{
$sql = (new DropTableCommand($this->database, $table))->generate();
$sql = (new DropTableCommandTranslator((new DropTableCommand($this->database, $table))->getCommand()))->make();
$this->run($sql);
}

public function dropIfExists(string $table)
{
$sql = (new DropIfExistsTableCommand($this->database, $table))->generate();
$sql = (new DropIfExistsTableCommandTranslator((new DropIfExistsTableCommand($this->database, $table))->getCommand()))->make();
$this->run($sql);
}

Expand All @@ -45,10 +48,10 @@ public function migrate()
$attribute = $data[1];

// Translate column objects to sql string
$commands = (new TranslateManager($this->translator))->translate($columns);

// Generate `CREATE TABLE` sql command by `CommandGenerator` class
$sql = (new CreateTableCommand($this->database, $attribute['table'], $commands))->generate();
$commands = (new ColumnTranslateManager($this->translator))->translate($columns);
// Make `CREATE TABLE` sql command by `CreateTableCommand` object
$sql = (new CreateTableCommandTranslator((new CreateTableCommand($this->database, $attribute['table'], $commands))->getCommand()))->make();

// Run the sql string by PDO connection
$this->run($sql);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Alirezasalehizadeh\QuickMigration\Translation;
namespace Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator;

class TranslateManager
class ColumnTranslateManager
{

private $translator;
Expand All @@ -11,7 +11,7 @@ class TranslateManager

public function __construct($translator = null)
{
$translatorObj = new Translator;
$translatorObj = new ColumnTranslator;

$this->translator = in_array($translator, $translatorObj->availableTranslators)
? new $translatorObj->availableTranslators[$translator]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Translation;
namespace Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator;

use Alirezasalehizadeh\QuickMigration\Structure\Column;
use Alirezasalehizadeh\QuickMigration\Translation\Translators\MySqlTranslator;
use Alirezasalehizadeh\QuickMigration\Translation\Translators\PostgreSqlTranslator;
use Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\Translators\MySqlTranslator;
use Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\Translators\PostgreSqlTranslator;

class Translator implements TranslatorInterface
class ColumnTranslator implements ColumnTranslatorInterface
{
protected $pattern;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Translation;
namespace Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator;

use Alirezasalehizadeh\QuickMigration\Structure\Column;

interface TranslatorInterface
interface ColumnTranslatorInterface
{
public function setColumn(Column $column);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Translation\Translators;
namespace Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\Translators;

use Alirezasalehizadeh\QuickMigration\Enums\Index;
use Alirezasalehizadeh\QuickMigration\Enums\Type;
use Alirezasalehizadeh\QuickMigration\Structure\Column;
use Alirezasalehizadeh\QuickMigration\Translation\Translator;
use Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\ColumnTranslator;

class MySqlTranslator extends Translator
class MySqlTranslator extends ColumnTranslator
{

protected $column;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Translation\Translators;
namespace Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\Translators;

use Alirezasalehizadeh\QuickMigration\Enums\Index;
use Alirezasalehizadeh\QuickMigration\Enums\Type;
use Alirezasalehizadeh\QuickMigration\Structure\Column;
use Alirezasalehizadeh\QuickMigration\Translation\Translator;
use Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\ColumnTranslator;

class PostgreSqlTranslator extends Translator
class PostgreSqlTranslator extends ColumnTranslator
{

protected $column;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Alirezasalehizadeh\QuickMigration\Translation\CommandTranslator;


interface CommandTranslatorInterface
{
public function make(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Alirezasalehizadeh\QuickMigration\Translation\CommandTranslator\Translators;

use Alirezasalehizadeh\QuickMigration\Command\Command;
use Alirezasalehizadeh\QuickMigration\Translation\CommandTranslator\CommandTranslatorInterface;

class CreateTableCommandTranslator implements CommandTranslatorInterface
{

public function __construct(
private Command $command
){
$this->command = $command;
}

public function make():string
{
return sprintf(
$this->command->getPattern(),
$this->command->getName(),
$this->command->getIncludes()['database'],
$this->command->getIncludes()['table'],
implode(',', $this->command->getIncludes()['sqlCommands']),
);
}

}
Loading

0 comments on commit 7648350

Please sign in to comment.