1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\Database\Entities\Schemas\Prototypes; |
9
|
|
|
|
10
|
|
|
use Spiral\Core\Component; |
11
|
|
|
use Spiral\Database\Entities\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 extends Component |
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
|
|
|
* @var AbstractTable |
36
|
|
|
*/ |
37
|
|
|
protected $table = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param AbstractTable $table |
41
|
|
|
* @param string $name |
42
|
|
|
* @param mixed $schema Driver specific schema information. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(AbstractTable $table, $name, $schema = null) |
45
|
|
|
{ |
46
|
|
|
$this->name = $name; |
47
|
|
|
$this->table = $table; |
48
|
|
|
|
49
|
|
|
if (!empty($schema)) { |
50
|
|
|
$this->resolveSchema($schema); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Associated table. |
56
|
|
|
* |
57
|
|
|
* @return AbstractTable |
58
|
|
|
*/ |
59
|
|
|
public function table() |
60
|
|
|
{ |
61
|
|
|
return $this->table; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set element name. |
66
|
|
|
* |
67
|
|
|
* @param string $name |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setName($name) |
71
|
|
|
{ |
72
|
|
|
$this->name = $name; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get element name. Can automatically quote such name. |
79
|
|
|
* |
80
|
|
|
* @param bool $quoted |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getName($quoted = false) |
84
|
|
|
{ |
85
|
|
|
if ($quoted) { |
86
|
|
|
return $this->table->driver()->identifier($this->name); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function isDeclared() |
96
|
|
|
{ |
97
|
|
|
return $this->declared; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Mark schema entity as declared, it will be kept in final diff. |
102
|
|
|
* |
103
|
|
|
* @param bool $declared |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function declared($declared = true) |
107
|
|
|
{ |
108
|
|
|
$this->declared = $declared; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Element creation/definition syntax. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
abstract public function sqlStatement(); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function __toString() |
124
|
|
|
{ |
125
|
|
|
return $this->sqlStatement(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Parse driver specific schema information and populate element data. |
130
|
|
|
* |
131
|
|
|
* @param mixed $schema |
132
|
|
|
* @throws SchemaException |
133
|
|
|
*/ |
134
|
|
|
abstract protected function resolveSchema($schema); |
135
|
|
|
} |