|
1
|
|
|
<?php |
|
2
|
|
|
namespace yentu\database; |
|
3
|
|
|
|
|
4
|
|
|
use yentu\database\ItemType; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Table extends DatabaseItem |
|
8
|
|
|
{ |
|
9
|
|
|
private Begin|Schema $schema; |
|
10
|
|
|
private array $primaryKeyColumns; |
|
11
|
|
|
private bool $isReference; |
|
12
|
|
|
public string $name; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(string $name, Begin|Schema $schema) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->name = $name; |
|
17
|
|
|
$this->schema = $schema; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
#[\Override] |
|
21
|
|
|
public function init() |
|
22
|
|
|
{ |
|
23
|
|
|
if(!$this->getDriver()->doesTableExist($this->buildDescription())) { |
|
|
|
|
|
|
24
|
|
|
$this->getDriver()->addTable($this->buildDescription()); |
|
|
|
|
|
|
25
|
|
|
$this->new = true; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setIsReference($isReference) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->isReference = $isReference; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function isReference() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->isReference; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function column($name) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->factory->create(ItemType::Column, $name, $this); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function drop() |
|
45
|
|
|
{ |
|
46
|
|
|
$table = $this->getDriver()->getDescription()->getTable( |
|
|
|
|
|
|
47
|
|
|
array( |
|
48
|
|
|
'table' => $this->name, |
|
49
|
|
|
'schema' => $this->schema->getName() |
|
50
|
|
|
) |
|
51
|
|
|
); |
|
52
|
|
|
$this->getDriver()->dropTable($table); |
|
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getName() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getSchema() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->schema; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function primaryKey(string ...$args) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->primaryKeyColumns = $args; |
|
69
|
|
|
return $this->factory->create(ItemType::PrimaryKey, $args, $this); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function index() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->create('index', func_get_args(), $this); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function unique() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->create('unique_key', func_get_args(), $this); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function foreignKey(string ... $args) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->factory->create(ItemType::ForeignKey, $args, $this); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function table($name) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->factory->create(ItemType::Table, $name, $this->schema); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function view($name) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->create('view', $name, $this->schema); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
#[\Override] |
|
98
|
|
|
protected function buildDescription() { |
|
99
|
|
|
return array( |
|
100
|
|
|
'name' => $this->name, |
|
101
|
|
|
'schema' => $this->schema->getName() |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|