Passed
Push — master ( 176828...fe6fe2 )
by Daniel
02:39
created

MySQLiByDanielGPtables::setTableCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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 MySQLiByDanielGPtables
37
{
38
39
    use MySQLiByDanielGPstructures;
40
41
    /**
42
     * Returns a Numeric field 2 use in a form
43
     *
44
     * @param string $tblSrc
45
     * @param array $value
46
     * @param array $iar
47
     * @return string
48
     */
49
    private function getFieldOutputNumeric($tblSrc, $value, $iar = [])
50
    {
51
        if ($value['EXTRA'] == 'auto_increment') {
52
            return $this->getFieldOutputNumericAI($value, $iar);
53
        }
54
        $fkArray = $this->getForeignKeysToArray($this->advCache['workingDatabase'], $tblSrc, $value['COLUMN_NAME']);
55
        if ($fkArray === []) {
56
            $fldNos = $this->setFieldNumbers($value);
57
            return $this->getFieldOutputTT($value, min(50, (array_key_exists('l', $fldNos) ? $fldNos['l'] : 99)), $iar);
58
        }
59
        return $this->getFieldOutputNumericNonFK($fkArray, $value, $iar);
60
    }
61
62
    /**
63
     * Handles creation of Auto Increment numeric field type output
64
     *
65
     * @param array $value
66
     * @param array $iar
67
     * @return string
68
     */
69
    private function getFieldOutputNumericAI($value, $iar = [])
70
    {
71
        if ($this->getFieldValue($value) == '') {
72
            $spF = ['id' => $value['COLUMN_NAME'], 'style' => 'font-style:italic;'];
73
            return $this->setStringIntoTag('auto-numar', 'span', $spF);
74
        }
75
        $inAdtnl = [
76
            'type'  => 'hidden',
77
            'name'  => $value['COLUMN_NAME'],
78
            'id'    => $value['COLUMN_NAME'],
79
            'value' => $this->getFieldValue($value),
80
        ];
81
        if ($iar !== []) {
82
            $inAdtnl = array_merge($inAdtnl, $iar);
83
        }
84
        return '<b>' . $this->getFieldValue($value) . '</b>' . $this->setStringIntoShortTag('input', $inAdtnl);
85
    }
86
87
    /**
88
     * Builds field output type for numeric types if not FK
89
     *
90
     * @param array $fkArray
91
     * @param array $value
92
     * @param array $iar
93
     * @return string
94
     */
95
    private function getFieldOutputNumericNonFK($fkArray, $value, $iar = [])
96
    {
97
        $query         = $this->sQueryGenericSelectKeyValue([
98
            '`' . $value['COLUMN_NAME'] . '`',
99
            $fkArray[$value['COLUMN_NAME']][2],
100
            $fkArray[$value['COLUMN_NAME']][0],
101
        ]);
102
        $selectOptions = $this->setMySQLquery2Server($query, 'array_key_value')['result'];
103
        $selectValue   = $this->getFieldValue($value);
104
        $inAdtnl       = ['size' => 1];
105
        if ($value['IS_NULLABLE'] == 'YES') {
106
            $inAdtnl = array_merge($inAdtnl, ['include_null']);
107
        }
108
        if ($iar !== []) {
109
            $inAdtnl = array_merge($inAdtnl, $iar);
110
        }
111
        return $this->setArrayToSelect($selectOptions, $selectValue, $value['COLUMN_NAME'], $inAdtnl);
112
    }
113
114
    /**
115
     * Prepares the text output fields
116
     *
117
     * @param string $tbl
118
     * @param array $value
119
     * @return array
120
     */
121
    private function getFieldOutputTextPrerequisites($tbl, $value)
122
    {
123
        $foreignKeysArray = [];
124
        if (($tbl != 'user_rights') && ($value['COLUMN_NAME'] != 'eid')) {
125
            $database = $this->advCache['workingDatabase'];
126
            if (strpos($tbl, '`.`')) {
127
                $database = substr($tbl, 0, strpos($tbl, '`.`'));
128
            }
129
            $foreignKeysArray = $this->getForeignKeysToArray($database, $tbl, $value['COLUMN_NAME']);
130
        }
131
        return $foreignKeysArray;
132
    }
133
134
    /**
135
     * Returns an array with fields referenced by a Foreign key
136
     *
137
     * @param string $database
138
     * @param string $tblName
139
     * @param string|array $oCol Only column(s) considered
140
     * @return array
141
     */
142
    private function getForeignKeysToArray($database, $tblName, $oCol = '')
143
    {
144
        if (!isset($this->advCache['tableFKs'][$database][$tblName])) {
145
            $this->setTableForeignKeyCache($database, $this->fixTableSource($tblName));
146
        }
147
        $aRt = [];
148
        if (isset($this->advCache['tableFKs'][$database][$tblName])) {
149
            $cnm = ['COLUMN_NAME', 'full_array_key_numbered', 'REFERENCED_TABLE_SCHEMA', 'REFERENCED_TABLE_NAME'];
150
            foreach ($this->advCache['tableFKs'][$database][$tblName] as $val) {
151
                if ($val[$cnm[0]] == $oCol) {
152
                    $vlQ        = array_merge($val, ['LIMIT' => 2]);
153
                    $tFd        = $this->setMySQLquery2Server($this->getForeignKeysQuery($vlQ), $cnm[1])['result'];
154
                    $tgtFld     = '`' . ($tFd[0][$cnm[0]] == $val[$cnm[0]] ? $tFd[1][$cnm[0]] : $tFd[0][$cnm[0]]) . '`';
155
                    $aRt[$oCol] = [$this->glueDbTb($val[$cnm[2]], $val[$cnm[3]]), $val[$cnm[2]], $tgtFld];
156
                }
157
            }
158
        }
159
        return $aRt;
160
    }
161
162
    /**
163
     * create a Cache for given table to use it in many places
164
     *
165
     * @param string $tblSrc
166
     */
167
    protected function setTableCache($tblSrc)
168
    {
169
        $dat = $this->establishDatabaseAndTable($tblSrc);
170
        if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) {
171
            $this->advCache['workingDatabase']                       = $dat[0];
172
            $this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([
173
                'TABLE_SCHEMA' => $dat[0],
174
                'TABLE_NAME'   => $dat[1],
175
            ]);
176
            $this->setTableForeignKeyCache($dat[0], $dat[1]);
177
        }
178
    }
179
180
    /**
181
     *
182
     * @param string $dbName
183
     * @param string $tblName
184
     */
185
    private function setTableForeignKeyCache($dbName, $tblName)
186
    {
187
        $frgnKs = $this->getMySQLlistIndexes([
188
            'TABLE_SCHEMA'          => $dbName,
189
            'TABLE_NAME'            => $tblName,
190
            'REFERENCED_TABLE_NAME' => 'NOT NULL',
191
        ]);
192
        if (is_array($frgnKs)) {
0 ignored issues
show
introduced by
The condition is_array($frgnKs) is always true.
Loading history...
193
            $this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs;
194
            $this->advCache['FKcol'][$dbName][$tblName]    = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME');
195
        }
196
    }
197
198
}
199