1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Database\Schemas\Prototypes; |
10
|
|
|
|
11
|
|
|
use Spiral\Database\Schemas\AbstractTable; |
12
|
|
|
use Spiral\Database\Exceptions\SchemaException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Aggregates common functionality for columns, indexes and foreign key schemas. |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractElement |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Declaration flag used to create full table diff. |
21
|
|
|
* |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $declared = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Element name. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $name = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @invisible |
35
|
|
|
* |
36
|
|
|
* @var AbstractTable |
37
|
|
|
*/ |
38
|
|
|
protected $table = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param AbstractTable $table |
42
|
|
|
* @param string $name |
43
|
|
|
* @param mixed $schema Driver specific schema information. |
44
|
|
|
*/ |
45
|
|
|
public function __construct(AbstractTable $table, $name, $schema = null) |
46
|
|
|
{ |
47
|
|
|
$this->name = $name; |
48
|
|
|
$this->table = $table; |
49
|
|
|
|
50
|
|
|
if (!empty($schema)) { |
51
|
|
|
$this->resolveSchema($schema); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Associated table. |
57
|
|
|
* |
58
|
|
|
* @return AbstractTable |
59
|
|
|
*/ |
60
|
|
|
public function getTable() |
61
|
|
|
{ |
62
|
|
|
return $this->table; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set element name. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setName($name) |
73
|
|
|
{ |
74
|
|
|
$this->name = $name; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get element name. Can automatically quote such name. |
81
|
|
|
* |
82
|
|
|
* @param bool $quoted |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getName($quoted = false) |
87
|
|
|
{ |
88
|
|
|
if ($quoted) { |
89
|
|
|
return $this->table->driver()->identifier($this->name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isDeclared() |
99
|
|
|
{ |
100
|
|
|
return $this->declared; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Mark schema entity as declared, it will be kept in final diff. |
105
|
|
|
* |
106
|
|
|
* @param bool $declared |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function declared($declared = true) |
111
|
|
|
{ |
112
|
|
|
$this->declared = $declared; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Element creation/definition syntax. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
abstract public function sqlStatement(); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function __toString() |
128
|
|
|
{ |
129
|
|
|
return $this->sqlStatement(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Parse driver specific schema information and populate element data. |
134
|
|
|
* |
135
|
|
|
* @param mixed $schema |
136
|
|
|
* |
137
|
|
|
* @throws SchemaException |
138
|
|
|
*/ |
139
|
|
|
abstract protected function resolveSchema($schema); |
140
|
|
|
} |
141
|
|
|
|