1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Gorlum 12.06.2017 15:29 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace DBAL; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Schema { |
10
|
|
|
/** |
11
|
|
|
* @var db_mysql $db |
12
|
|
|
*/ |
13
|
|
|
protected $db; |
14
|
|
|
/** |
15
|
|
|
* List of all table names |
16
|
|
|
* |
17
|
|
|
* @var string[] $tablesAll |
18
|
|
|
*/ |
19
|
|
|
protected $tablesAll = null; |
20
|
|
|
/** |
21
|
|
|
* @var string[] $tablesSn |
22
|
|
|
*/ |
23
|
|
|
protected $tablesSn = null; |
24
|
|
|
/** |
25
|
|
|
* @var TableSchema[] $tableSchemas |
26
|
|
|
*/ |
27
|
|
|
protected $tableSchemas = []; |
28
|
|
|
|
29
|
|
|
public function __construct(db_mysql $db) { |
30
|
|
|
$this->db = $db; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getDb() { |
34
|
|
|
return $this->db; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function clear() { |
38
|
|
|
$this->tablesAll = null; |
39
|
|
|
$this->tablesSn = null; |
40
|
|
|
$this->tableSchemas = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function loadTableNamesFromDb() { |
44
|
|
|
$this->clear(); |
45
|
|
|
$this->tablesAll = array(); |
46
|
|
|
$this->tablesSn = array(); |
47
|
|
|
|
48
|
|
|
$query = $this->db->mysql_get_table_list(); |
49
|
|
|
|
50
|
|
|
$prefix_length = strlen($this->db->db_prefix); |
51
|
|
|
|
52
|
|
|
while ($row = $this->db->db_fetch($query)) { |
|
|
|
|
53
|
|
|
foreach ($row as $table_name) { |
54
|
|
|
$this->tablesAll[$table_name] = $table_name; |
55
|
|
|
|
56
|
|
|
if (strpos($table_name, $this->db->db_prefix) === 0) { |
57
|
|
|
$table_name_sn = substr($table_name, $prefix_length); |
58
|
|
|
|
59
|
|
|
$this->tablesSn[$table_name_sn] = $table_name_sn; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get names of all tables in this DB |
67
|
|
|
* |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
|
|
public function getAllTables() { |
71
|
|
|
if (!isset($this->tablesAll)) { |
72
|
|
|
$this->loadTableNamesFromDb(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->tablesAll; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get un-prefixed table names potentially used by game |
80
|
|
|
* |
81
|
|
|
* @return string[] |
82
|
|
|
*/ |
83
|
|
|
public function getSnTables() { |
84
|
|
|
if (!isset($this->tablesSn)) { |
85
|
|
|
$this->loadTableNamesFromDb(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->tablesSn; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks if SN table exists |
93
|
|
|
* |
94
|
|
|
* @param $tableName |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isSnTableExists($tableName) { |
99
|
|
|
return isset($this->getSnTables()[$tableName]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $tableName |
104
|
|
|
* |
105
|
|
|
* @return TableSchema |
106
|
|
|
*/ |
107
|
|
|
public function getTableSchema($tableName) { |
108
|
|
|
if (empty($this->tableSchemas[$tableName])) { |
109
|
|
|
$this->tableSchemas[$tableName] = new TableSchema($tableName, $this); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->tableSchemas[$tableName]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $table |
117
|
|
|
* @param $index |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isIndexExists($table, $index) { |
122
|
|
|
return $this->isSnTableExists($table) && $this->getTableSchema($table)->isIndexExists($index); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function isFieldExists($table, $field) { |
126
|
|
|
return $this->isSnTableExists($table) && $this->getTableSchema($table)->isFieldExists($field); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function isConstrainExists($table, $constrain) { |
130
|
|
|
return $this->isSnTableExists($table) && $this->getTableSchema($table)->isConstrainExists($constrain); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|