1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaoi\Sql; |
4
|
|
|
|
5
|
|
|
use Yaoi\Database\Definition\Index; |
6
|
|
|
use Yaoi\Database\Definition\Table; |
7
|
|
|
use Yaoi\String\Quoter; |
8
|
|
|
|
9
|
|
|
abstract class CreateTable extends Batch |
10
|
|
|
{ |
11
|
|
|
/** @var Table */ |
12
|
|
|
protected $table; |
13
|
|
|
|
14
|
|
View Code Duplication |
protected function appendColumns() { |
|
|
|
|
15
|
|
|
$utility = $this->database()->getUtility(); |
16
|
|
|
|
17
|
|
|
foreach ($this->table->getColumns(true) as $column) { |
|
|
|
|
18
|
|
|
$this->createLines->commaExpr(' ? ' . $utility->getColumnTypeString($column), new Symbol($column->schemaName)); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
protected function appendIndexes() { |
25
|
|
|
foreach ($this->table->indexes as $index) { |
26
|
|
|
$columns = Symbol::prepareColumns($index->columns); |
27
|
|
|
|
28
|
|
|
if ($index->type === Index::TYPE_KEY) { |
29
|
|
|
$this->createLines->commaExpr(' KEY ? (?)', new Symbol($index->getName()), $columns); |
30
|
|
|
} |
31
|
|
|
elseif ($index->type === Index::TYPE_UNIQUE) { |
32
|
|
|
$this->createLines->commaExpr(' UNIQUE KEY ? (?)', new Symbol($index->getName()), $columns); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function appendForeignKeys() { |
38
|
|
|
if ($this->table->disableForeignKeys) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
foreach ($this->table->getForeignKeys() as $foreignKey) { |
42
|
|
|
$this->fkLines->commaExpr($this->database()->getUtility()->generateForeignKeyExpression($foreignKey)); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function appendPrimaryKey() { |
47
|
|
|
$columns = array(); |
48
|
|
|
foreach ($this->table->primaryKey as $column) { |
49
|
|
|
$columns []= new Symbol($column->schemaName); |
50
|
|
|
} |
51
|
|
|
$this->createLines->commaExpr(' PRIMARY KEY (:columns)', array('columns' => $columns)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** @var Expression */ |
56
|
|
|
protected $createLines; |
57
|
|
|
|
58
|
|
|
/** @var Expression */ |
59
|
|
|
protected $fkLines; |
60
|
|
|
|
61
|
|
|
public function __construct(Table $table) { |
62
|
|
|
$this->table = $table; |
63
|
|
|
$this->bindDatabase($table->database()); |
64
|
|
|
$this->createLines = new SimpleExpression(); |
65
|
|
|
$this->createLines->setOpComma(',' . PHP_EOL); |
66
|
|
|
$this->fkLines = new SimpleExpression(); |
67
|
|
|
$this->fkLines->setOpComma(',' . PHP_EOL); |
68
|
|
|
|
69
|
|
|
$createExpression = new SimpleExpression('CREATE TABLE ? (' . PHP_EOL, $this->table); |
70
|
|
|
$this->add($createExpression); |
71
|
|
|
|
72
|
|
|
$createExpression->appendExpr($this->createLines); |
73
|
|
|
$createExpression->appendExpr(PHP_EOL . ')'); |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->appendColumns(); |
77
|
|
|
$this->appendIndexes(); |
78
|
|
|
$this->createLines->commaExpr($this->fkLines); |
79
|
|
|
$this->appendForeignKeys(); |
80
|
|
|
$this->appendPrimaryKey(); |
81
|
|
|
|
82
|
|
|
if ($this->createLines->isEmpty()) { |
83
|
|
|
$createExpression->disable(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function extractForeignKeysStatement() |
88
|
|
|
{ |
89
|
|
|
$this->fkLines->disable(); |
90
|
|
|
return $this->database()->getUtility() |
91
|
|
|
->generateAlterTable(new Table(null, $this->database(), '_any_name'), $this->table) |
92
|
|
|
->extractForeignKeysStatement(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function isEmpty() |
96
|
|
|
{ |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.