Completed
Push — master ( 8fada7...2eb9e0 )
by Daniel
02:34
created

MySQLiByDanielGPstructures   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 29
c 8
b 0
f 0
lcom 1
cbo 2
dl 0
loc 234
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A correctTableWithQuotesAsFieldPrefix() 0 7 2
A getMySQLlistColumns() 0 4 1
A getMySQLlistDatabases() 0 4 1
A getMySQLlistEngines() 0 4 1
A getMySQLlistIndexes() 0 4 1
A getMySQLlistMultiple() 0 10 3
A getMySQLlistMultipleFinal() 0 17 2
A getMySQLlistTables() 0 4 1
A getMySQLserverTime() 0 4 1
A getRowDataFromTable() 0 12 3
A setArrayLineArrayToFilter() 0 9 2
A setArrayLineToFilter() 0 8 3
A setArrayToFilterValues() 0 13 4
A getMySQLStatistics() 0 4 1
A getMySQLactiveDatabases() 0 4 1
A getMySQLactiveEngines() 0 4 1
A getMySQLglobalVariables() 0 4 1
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
 * Usefull functions to get quick MySQL content
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait MySQLiByDanielGPstructures
37
{
38
39
    use MySQLiByDanielGP,
40
        MySQLiByDanielGPqueries;
41
42
    /**
43
     * Ensures table has special quoes and DOT as final char
44
     * (if not empty, of course)
45
     *
46
     * @param string $referenceTable
47
     * @return string
48
     */
49
    private function correctTableWithQuotesAsFieldPrefix($referenceTable)
50
    {
51
        if ($referenceTable != '') {
52
            return '`' . str_replace('`', '', $referenceTable) . '`.';
53
        }
54
        return '';
55
    }
56
57
    /**
58
     * Return the list of Tables from the MySQL server
59
     *
60
     * @return string
61
     */
62
    protected function getMySQLStatistics($filterArray = null)
63
    {
64
        return $this->getMySQLlistMultiple('Statistics', 'full_array_key_numbered', $filterArray);
65
    }
66
67
    /**
68
     * returns a list of MySQL databases
69
     *
70
     * @return array
71
     */
72
    protected function getMySQLactiveDatabases()
73
    {
74
        return $this->getMySQLlistDatabases(true);
75
    }
76
77
    /**
78
     * returns a list of active MySQL engines
79
     *
80
     * @return array
81
     */
82
    protected function getMySQLactiveEngines()
83
    {
84
        return $this->getMySQLlistEngines(true);
85
    }
86
87
    /**
88
     * returns the list of all MySQL global variables
89
     *
90
     * @return array
91
     */
92
    protected function getMySQLglobalVariables()
93
    {
94
        return $this->getMySQLlistMultiple('VariablesGlobal', 'array_key_value');
95
    }
96
97
    /**
98
     * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column)
99
     *
100
     * @return array
101
     */
102
    protected function getMySQLlistColumns($filterArray = null)
103
    {
104
        return $this->getMySQLlistMultiple('Columns', 'full_array_key_numbered', $filterArray);
105
    }
106
107
    /**
108
     * returns a list of MySQL databases (w. choice of exclude/include the system ones)
109
     *
110
     * @return array
111
     */
112
    protected function getMySQLlistDatabases($excludeSystemDbs = true)
113
    {
114
        return $this->getMySQLlistMultiple('Databases', 'array_first_key_rest_values', $excludeSystemDbs);
115
    }
116
117
    /**
118
     * returns a list of MySQL engines (w. choice of return only the active ones)
119
     *
120
     * @return array
121
     */
122
    protected function getMySQLlistEngines($onlyActiveOnes = true)
123
    {
124
        return $this->getMySQLlistMultiple('Engines', 'array_first_key_rest_values', $onlyActiveOnes);
125
    }
126
127
    /**
128
     * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column)
129
     *
130
     * @return array
131
     */
132
    protected function getMySQLlistIndexes($filterArray = null)
133
    {
134
        return $this->getMySQLlistMultiple('Indexes', 'full_array_key_numbered', $filterArray);
135
    }
136
137
    /**
138
     * Return various informations (from predefined list) from the MySQL server
139
     *
140
     * @return int|array
141
     */
142
    private function getMySQLlistMultiple($returnChoice, $returnType, $additionalFeatures = null)
143
    {
144
        if (is_null($this->mySQLconnection)) {
145
            if ($returnType == 'value') {
146
                return null;
147
            }
148
            return [];
149
        }
150
        return $this->getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures);
151
    }
152
153
    /**
154
     * Return various informations (from predefined list) from the MySQL server
155
     *
156
     * @return array
157
     */
158
    private function getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures = null)
159
    {
160
        $queryByChoice = [
161
            'Columns'         => $this->sQueryMySqlColumns($additionalFeatures),
162
            'Databases'       => $this->sQueryMySqlActiveDatabases($additionalFeatures),
163
            'Engines'         => $this->sQueryMySqlActiveEngines($additionalFeatures),
164
            'Indexes'         => $this->sQueryMySqlIndexes($additionalFeatures),
165
            'ServerTime'      => $this->sQueryMySqlServerTime(),
166
            'Statistics'      => $this->sQueryMySqlStatistics($additionalFeatures),
167
            'Tables'          => $this->sQueryMySqlTables($additionalFeatures),
168
            'VariablesGlobal' => $this->sQueryMySqlGlobalVariables(),
169
        ];
170
        if (array_key_exists($returnChoice, $queryByChoice)) {
171
            return $this->setMySQLquery2Server($queryByChoice[$returnChoice], $returnType)['result'];
172
        }
173
        return [];
174
    }
175
176
    /**
177
     * Return the list of Tables from the MySQL server
178
     *
179
     * @return string
180
     */
181
    protected function getMySQLlistTables($filterArray = null)
182
    {
183
        return $this->getMySQLlistMultiple('Tables', 'full_array_key_numbered', $filterArray);
184
    }
185
186
    /**
187
     * Return the time from the MySQL server
188
     *
189
     * @return string
190
     */
191
    protected function getMySQLserverTime()
192
    {
193
        return $this->getMySQLlistMultiple('ServerTime', 'value');
194
    }
195
196
    /**
197
     * Reads data from table into REQUEST super global
198
     *
199
     * @param string $tableName
200
     * @param array $filtersArray
201
     */
202
    protected function getRowDataFromTable($tableName, $filtersArray)
203
    {
204
        $query   = $this->sQueryRowsFromTable([$tableName, $this->setArrayToFilterValues($filtersArray)]);
205
        $rawData = $this->setMySQLquery2Server($query, 'array_pairs_key_value')['result'];
206
        if (!is_null($rawData)) {
207
            $this->initializeSprGlbAndSession();
208
            foreach ($rawData as $key => $value) {
209
                $vToSet = str_replace(['\\\\"', '\\"', "\\\\'", "\\'"], ['"', '"', "'", "'"], $value);
210
                $this->tCmnRequest->request->set($key, $vToSet);
211
            }
212
        }
213
    }
214
215
    /**
216
     * Builds an filter string from pair of key and value, where value is array
217
     *
218
     * @param string $key
219
     * @param array $value
220
     * @param string $referenceTable
221
     * @return string
222
     */
223
    private function setArrayLineArrayToFilter($key, $value, $referenceTable)
224
    {
225
        $filters2 = implode(', ', array_diff($value, ['']));
226
        if ($filters2 != '') {
227
            return '(' . $referenceTable . '`' . $key . '` IN ("'
228
                    . str_replace(',', '","', str_replace(["'", '"'], '', $filters2)) . '"))';
229
        }
230
        return '';
231
    }
232
233
    /**
234
     * Builds an filter string from pair of key and value, none array
235
     *
236
     * @param string $key
237
     * @param int|float|string $value
238
     * @return string
239
     */
240
    private function setArrayLineToFilter($key, $value)
241
    {
242
        $fTemp = '=';
243
        if ((substr($value, 0, 1) == '%') && (substr($value, -1) == '%')) {
244
            $fTemp = 'LIKE';
245
        }
246
        return '(`' . $key . '` ' . $fTemp . '"' . $value . '")';
247
    }
248
249
    /**
250
     * Transforms an array into usable filters
251
     *
252
     * @param array $entryArray
253
     * @param string $referenceTable
254
     * @return array
255
     */
256
    private function setArrayToFilterValues($entryArray, $referenceTable = '')
257
    {
258
        $filters  = [];
259
        $refTable = $this->correctTableWithQuotesAsFieldPrefix($referenceTable);
260
        foreach ($entryArray as $key => $value) {
261
            if (is_array($value)) {
262
                $filters[] = $this->setArrayLineArrayToFilter($key, $value, $refTable);
263
            } elseif (!in_array($value, ['', '%%'])) {
264
                $filters[] = $this->setArrayLineToFilter($key, $value);
265
            }
266
        }
267
        return implode(' AND ', array_diff($filters, ['']));
268
    }
269
}
270