1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rentgen\Database\Constraint; |
4
|
|
|
|
5
|
|
|
use Rentgen\Database\Table; |
6
|
|
|
|
7
|
|
|
class PrimaryKey implements ConstraintInterface |
8
|
|
|
{ |
9
|
|
|
private $columns; |
10
|
|
|
private $isAutoIncrement = true; |
11
|
|
|
private $autoCreateColumn = false; |
12
|
|
|
private $table; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
* |
17
|
|
|
* @param array|string $columns Column names. |
18
|
|
|
* @param Table $table Table instance. |
19
|
|
|
*/ |
20
|
|
|
public function __construct($columns = null, Table $table = null) |
21
|
|
|
{ |
22
|
|
|
$this->columns = is_string($columns) ? array($columns) : $columns; |
23
|
|
|
if (empty($this->columns)) { |
24
|
|
|
$this->autoCreateColumn = true; |
25
|
|
|
} |
26
|
|
|
$this->table = $table; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function getName() |
33
|
|
|
{ |
34
|
|
|
return $this->table->getName() . '_pkey'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// TODO hmmmmmm.... |
38
|
|
|
public function getColumns() |
39
|
|
|
{ |
40
|
|
|
return empty($this->columns) |
41
|
|
|
? $this->table->getName() . '_id' |
42
|
|
|
: implode(',', $this->columns); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return true if the primary key is auto create column. |
47
|
|
|
* |
48
|
|
|
* @return bool Auto create column |
49
|
|
|
*/ |
50
|
|
|
public function isAutoCreateColumn() |
51
|
|
|
{ |
52
|
|
|
return $this->autoCreateColumn; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return true if the primary key is multi. |
57
|
|
|
* |
58
|
|
|
* @return bool Multi columns. |
59
|
|
|
*/ |
60
|
|
|
public function isMulti() |
61
|
|
|
{ |
62
|
|
|
return count($this->columns) > 1; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Disable auto increment. |
67
|
|
|
* |
68
|
|
|
* @return PrimaryKey Self. |
69
|
|
|
*/ |
70
|
|
|
public function disableAutoIncrement() |
71
|
|
|
{ |
72
|
|
|
$this->isAutoIncrement = false; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return true if the primary key is auto increment. |
79
|
|
|
* |
80
|
|
|
* @return bool Auto increment. |
81
|
|
|
*/ |
82
|
|
|
public function isAutoIncrement() |
83
|
|
|
{ |
84
|
|
|
return true === $this->isAutoIncrement; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// TODO Remove this code |
88
|
|
|
public function __toString() |
89
|
|
|
{ |
90
|
|
|
return sprintf('CONSTRAINT %s PRIMARY KEY (%s)' |
91
|
|
|
, $this->getName() |
92
|
|
|
, $this->getColumns() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function getTable() |
100
|
|
|
{ |
101
|
|
|
return $this->table; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set table instance. |
106
|
|
|
* |
107
|
|
|
* @param Table $table Table instance. |
108
|
|
|
* |
109
|
|
|
* @return PrimaryKey Self. |
110
|
|
|
*/ |
111
|
|
|
public function setTable(Table $table) |
112
|
|
|
{ |
113
|
|
|
$this->table = $table; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|