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