1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* useful functions to get quick results |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait MySQLiByDanieGPtables |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use MySQLiByDanielGPstructures; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns an array with fields referenced by a Foreign key |
43
|
|
|
* |
44
|
|
|
* @param string $database |
45
|
|
|
* @param string $tblName |
46
|
|
|
* @param string|array $oCol Only column(s) considered |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
private function getForeignKeysToArray($database, $tblName, $oCol = '') |
50
|
|
|
{ |
51
|
|
|
if (!isset($this->advCache['tableFKs'][$database][$tblName])) { |
52
|
|
|
$this->setTableForeignKeyCache($database, $this->fixTableSource($tblName)); |
53
|
|
|
} |
54
|
|
|
$aRt = []; |
55
|
|
|
if (isset($this->advCache['tableFKs'][$database][$tblName])) { |
56
|
|
|
$cnm = ['COLUMN_NAME', 'full_array_key_numbered', 'REFERENCED_TABLE_SCHEMA', 'REFERENCED_TABLE_NAME']; |
57
|
|
|
foreach ($this->advCache['tableFKs'][$database][$tblName] as $val) { |
58
|
|
|
if ($val[$cnm[0]] == $oCol) { |
59
|
|
|
$vlQ = array_merge($val, ['LIMIT' => 2]); |
60
|
|
|
$tFd = $this->setMySQLquery2Server($this->getForeignKeysQuery($vlQ), $cnm[1])['result']; |
61
|
|
|
$tgtFld = '`' . ($tFd[0][$cnm[0]] == $val[$cnm[0]] ? $tFd[1][$cnm[0]] : $tFd[0][$cnm[0]]) . '`'; |
62
|
|
|
$aRt[$oCol] = [$this->glueDbTb($val[$cnm[2]], $val[$cnm[3]]), $val[$cnm[2]], $tgtFld]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return $aRt; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* create a Cache for given table to use it in many places |
71
|
|
|
* |
72
|
|
|
* @param string $tblSrc |
73
|
|
|
*/ |
74
|
|
|
protected function setTableCache($tblSrc) |
75
|
|
|
{ |
76
|
|
|
$dat = $this->establishDatabaseAndTable($tblSrc); |
77
|
|
|
if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) { |
78
|
|
|
$this->advCache['workingDatabase'] = $dat[0]; |
79
|
|
|
$this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([ |
80
|
|
|
'TABLE_SCHEMA' => $dat[0], |
81
|
|
|
'TABLE_NAME' => $dat[1], |
82
|
|
|
]); |
83
|
|
|
$this->setTableForeignKeyCache($dat[0], $dat[1]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* @param string $dbName |
90
|
|
|
* @param string $tblName |
91
|
|
|
*/ |
92
|
|
|
private function setTableForeignKeyCache($dbName, $tblName) |
93
|
|
|
{ |
94
|
|
|
$frgnKs = $this->getMySQLlistIndexes([ |
95
|
|
|
'TABLE_SCHEMA' => $dbName, |
96
|
|
|
'TABLE_NAME' => $tblName, |
97
|
|
|
'REFERENCED_TABLE_NAME' => 'NOT NULL', |
98
|
|
|
]); |
99
|
|
|
if (is_array($frgnKs)) { |
|
|
|
|
100
|
|
|
$this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs; |
101
|
|
|
$this->advCache['FKcol'][$dbName][$tblName] = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|