|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Migratio\GrammarStructure\Mysql\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait QueryStack |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @return mixed |
|
9
|
|
|
*/ |
|
10
|
|
|
public function connection() |
|
11
|
|
|
{ |
|
12
|
|
|
return $this->schema->getConnection(); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
*/ |
|
18
|
|
|
public function showTables() |
|
19
|
|
|
{ |
|
20
|
|
|
$tables = $this->connection()->query('SHOW TABLES')->fetchAll(); |
|
21
|
|
|
|
|
22
|
|
|
$list = []; |
|
23
|
|
|
|
|
24
|
|
|
foreach ($tables as $key=>$table) { |
|
25
|
|
|
$list[] = $table[0]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return $list; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $table |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
public function showColumnsFrom($table) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->connection()->query('SHOW FULL COLUMNS FROM '.$table)->fetchAll(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* get charset for collation |
|
42
|
|
|
* |
|
43
|
|
|
* @param $collation |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getCharsetForCollation($collation) |
|
47
|
|
|
{ |
|
48
|
|
|
$collation = $this->connection()->query('SHOW COLLATION LIKE \''.$collation.'%\'')->fetchAll(); |
|
49
|
|
|
|
|
50
|
|
|
if(isset($collation[0])){ |
|
51
|
|
|
return $collation[0]['Charset']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return 'utf8'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* get table status |
|
59
|
|
|
* |
|
60
|
|
|
* @param $table |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getTableStatus($table) |
|
64
|
|
|
{ |
|
65
|
|
|
$status = $this->connection()->query('show table status like \''.$table.'\'')->fetchAll(); |
|
66
|
|
|
|
|
67
|
|
|
if(isset($status[0])){ |
|
68
|
|
|
return $status[0]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* get show indexes |
|
76
|
|
|
* |
|
77
|
|
|
* @param $table |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function showIndexes($table) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->connection()->query('SHOW INDEXES FROM '.$table)->fetchAll(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* get foreign keys from table |
|
87
|
|
|
* |
|
88
|
|
|
* @param $table |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getForeignKeys($table) |
|
92
|
|
|
{ |
|
93
|
|
|
$config = $this->getConfig(); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$database = (isset($config['database'])) ? $config['database'] : null; |
|
96
|
|
|
|
|
97
|
|
|
$query = $this->connection()->query('SELECT rc.MATCH_OPTION,rc.UPDATE_RULE,rc.DELETE_RULE,r.* |
|
98
|
|
|
FROM information_schema.REFERENTIAL_CONSTRAINTS as rc |
|
99
|
|
|
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE as r ON r.TABLE_NAME=rc.TABLE_NAME |
|
100
|
|
|
WHERE rc.CONSTRAINT_SCHEMA = \''.$database.'\' |
|
101
|
|
|
AND rc.TABLE_NAME = \''.$table.'\' AND r.REFERENCED_TABLE_NAME is not null')->fetchAll(); |
|
102
|
|
|
|
|
103
|
|
|
if(isset($query[0])){ |
|
104
|
|
|
return $query[0]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return []; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* set query basic |
|
112
|
|
|
* |
|
113
|
|
|
* @param $query |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setQueryBasic($query) |
|
117
|
|
|
{ |
|
118
|
|
|
try { |
|
119
|
|
|
|
|
120
|
|
|
$query =$this->connection()->query($query); |
|
121
|
|
|
|
|
122
|
|
|
return [ |
|
123
|
|
|
'result'=>true, |
|
124
|
|
|
'query'=>$query, |
|
125
|
|
|
'message'=>null, |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
catch (\PDOException $exception){ |
|
129
|
|
|
|
|
130
|
|
|
return [ |
|
131
|
|
|
'result'=>false, |
|
132
|
|
|
'query'=>$query, |
|
133
|
|
|
'message'=>$exception->getMessage(), |
|
134
|
|
|
]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|