|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yentu; |
|
4
|
|
|
|
|
5
|
|
|
class DatabaseAssertor |
|
6
|
|
|
{ |
|
7
|
|
|
private $description; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct($description) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->description = $description; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function doesSchemaExist($details) |
|
15
|
|
|
{ |
|
16
|
|
|
return isset($this->description['schemata'][$details]); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function doesTableExist($details) |
|
20
|
|
|
{ |
|
21
|
|
|
if (is_string($details)) { |
|
22
|
|
|
$details = array( |
|
23
|
|
|
'schema' => false, |
|
24
|
|
|
'name' => $details |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $details['schema'] == false ? |
|
29
|
|
|
isset($this->description['tables'][$details['name']]) : |
|
30
|
|
|
isset($this->description['schemata'][$details['schema']]['tables'][$details['name']]); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function doesColumnExist($details) |
|
34
|
|
|
{ |
|
35
|
|
|
$table = $this->getTableDetails($details['schema'], $details['table']); |
|
36
|
|
|
return isset($table['columns'][$details['name']]) ? |
|
37
|
|
|
Parameters::wrap($table['columns'][$details['name']]) : false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function doesItemExist($details, $type) |
|
41
|
|
|
{ |
|
42
|
|
|
$table = $this->getTableDetails($details['schema'], $details['table']); |
|
43
|
|
|
if (isset($details['name'])) { |
|
44
|
|
|
return $table[$type][$details['name']] ?? false; |
|
45
|
|
|
} else if (!empty($details['columns'])) { |
|
46
|
|
|
$columns = array_map(fn($x) => $x, $details['columns']); |
|
47
|
|
|
sort($columns); |
|
48
|
|
|
return $table["flat_$type"][implode(':', $columns)] ?? false; |
|
49
|
|
|
} |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function doesForeignKeyExist($details) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->doesItemExist($details, 'foreign_keys'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function doesUniqueKeyExist($details) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->doesItemExist($details, 'unique_keys'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function doesPrimaryKeyExist($details) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->doesItemExist($details, 'primary_key'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function doesIndexExist($details) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->doesItemExist($details, 'indices'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function doesViewExist($details) |
|
74
|
|
|
{ |
|
75
|
|
|
if (is_string($details)) { |
|
76
|
|
|
$details = array( |
|
77
|
|
|
'schema' => false, |
|
78
|
|
|
'name' => $details |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// too complex |
|
83
|
|
|
if ($details['schema'] == false) { |
|
84
|
|
|
return isset($this->description['views'][$details['name']]) ? $this->description['views'][$details['name']]['definition'] : false; |
|
85
|
|
|
} else { |
|
86
|
|
|
return (isset($this->description['schemata'][$details['schema']]['views'][$details['name']]) ? |
|
87
|
|
|
$this->description['schemata'][$details['schema']]['views'][$details['name']]['definition'] : false); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getTableDetails($schema, $table) |
|
92
|
|
|
{ |
|
93
|
|
|
if ($schema) { |
|
94
|
|
|
return $this->description['schemata'][$schema]['tables'][$table]; |
|
95
|
|
|
} else { |
|
96
|
|
|
return $this->description['tables'][$table]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|