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 insert(array $columns, array $items) |
93
|
|
|
{ |
94
|
|
|
$driver = $this->getDriver(); |
95
|
|
|
$query = sprintf( |
96
|
|
|
"INSERT INTO %s (%s) VALUES (%s)", |
97
|
|
|
"{$driver->quoteIdentifier($this->schema->getName())}.{$driver->quoteIdentifier($this->name)}", |
|
|
|
|
98
|
|
|
implode(", ", array_map(fn($x) => $driver->quoteIdentifier($x), $columns)), |
99
|
|
|
implode(", ", array_fill(0, count($items[0]), "?")) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
foreach($items as $row) { |
103
|
|
|
$this->getDriver()->query($query, $row); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function view($name) |
110
|
|
|
{ |
111
|
|
|
return $this->create('view', $name, $this->schema); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
#[\Override] |
115
|
|
|
protected function buildDescription() { |
116
|
|
|
return array( |
117
|
|
|
'name' => $this->name, |
118
|
|
|
'schema' => $this->schema->getName() |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|