|
1
|
|
|
<?php |
|
2
|
|
|
namespace Bolt\Storage\Database\Schema\Table; |
|
3
|
|
|
|
|
4
|
|
|
use Bolt\Exception\StorageException; |
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
6
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
|
7
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Base database table class for Bolt. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Gawain Lynch <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class BaseTable |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform */ |
|
18
|
|
|
protected $platform; |
|
19
|
|
|
/** @var \Doctrine\DBAL\Schema\Table */ |
|
20
|
|
|
protected $table; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $tablePrefix; |
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $tableName; |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $aliasName; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param AbstractPlatform $platform |
|
32
|
|
|
* @param string $tablePrefix |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(AbstractPlatform $platform, $tablePrefix) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->platform = $platform; |
|
37
|
|
|
$this->tablePrefix = $tablePrefix; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the table object. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws StorageException |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Doctrine\DBAL\Schema\Table |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getTable() |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->table === null) { |
|
50
|
|
|
throw new StorageException('Table not built.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->table; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the table's schema object. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Schema $schema |
|
60
|
|
|
* @param string $aliasName |
|
61
|
|
|
* @param string $charset |
|
62
|
|
|
* @param string $collate |
|
63
|
|
|
* |
|
64
|
|
|
* @return \Doctrine\DBAL\Schema\Table |
|
65
|
|
|
*/ |
|
66
|
|
|
public function buildTable(Schema $schema, $aliasName, $charset, $collate) |
|
67
|
|
|
{ |
|
68
|
|
|
$tableName = $this->tablePrefix . $aliasName; |
|
69
|
|
|
$this->table = $schema->createTable($tableName); |
|
70
|
|
|
$this->table->addOption('alias', $aliasName); |
|
71
|
|
|
$this->table->addOption('charset', $charset); |
|
72
|
|
|
$this->table->addOption('collate', $collate); |
|
73
|
|
|
$this->aliasName = $aliasName; |
|
74
|
|
|
$this->tableName = $this->table->getName(); |
|
75
|
|
|
$this->addColumns(); |
|
76
|
|
|
$this->addIndexes(); |
|
77
|
|
|
$this->setPrimaryKey(); |
|
78
|
|
|
$this->addForeignKeyConstraints(); |
|
79
|
|
|
|
|
80
|
|
|
return $this->table; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the table's name. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \RuntimeException |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getTableName() |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->tableName === null) { |
|
93
|
|
|
throw new \RuntimeException('Table ' . __CLASS__ . ' has not been built yet.'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->tableName; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the table's alias (short) name. |
|
101
|
|
|
* |
|
102
|
|
|
* @throws \RuntimeException |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getTableAlias() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->aliasName === null) { |
|
109
|
|
|
throw new \RuntimeException('Table ' . __CLASS__ . ' has not been built yet.'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this->aliasName; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Default value for TEXT fields, differs per platform. |
|
117
|
|
|
* |
|
118
|
|
|
* @return string|null |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getTextDefault() |
|
121
|
|
|
{ |
|
122
|
|
|
if ($this->platform instanceof SqlitePlatform || $this->platform instanceof PostgreSqlPlatform) { |
|
123
|
|
|
return ''; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Add columns to the table. |
|
131
|
|
|
*/ |
|
132
|
|
|
abstract protected function addColumns(); |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Define the columns that require indexing. |
|
136
|
|
|
*/ |
|
137
|
|
|
abstract protected function addIndexes(); |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set the table's primary key. |
|
141
|
|
|
*/ |
|
142
|
|
|
abstract protected function setPrimaryKey(); |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set the table's foreign key constraints. |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function addForeignKeyConstraints() |
|
148
|
|
|
{ |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|