Completed
Push — master ( 4ebe21...b06780 )
by Daniel
05:47
created

MySQLiByDanielGPstructures::getMySQLlistTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
41
    /**
42
     * Return the list of Tables from the MySQL server
43
     *
44
     * @return string
45
     */
46
    protected function getMySQLStatistics($filterArray = null)
47
    {
48
        return $this->getMySQLlistMultiple('Statistics', 'full_array_key_numbered', $filterArray);
49
    }
50
51
    /**
52
     * returns a list of MySQL databases
53
     *
54
     * @return array
55
     */
56
    protected function getMySQLactiveDatabases()
57
    {
58
        return $this->getMySQLlistDatabases(true);
59
    }
60
61
    /**
62
     * returns a list of active MySQL engines
63
     *
64
     * @return array
65
     */
66
    protected function getMySQLactiveEngines()
67
    {
68
        return $this->getMySQLlistEngines(true);
69
    }
70
71
    /**
72
     * returns the list of all MySQL generic informations
73
     *
74
     * @return array
75
     */
76
    protected function getMySQLgenericInformations()
77
    {
78
        if (is_null($this->mySQLconnection)) {
79
            return [];
80
        }
81
        return ['Info' => $this->mySQLconnection->server_info, 'Version' => $this->mySQLconnection->server_version];
82
    }
83
84
    /**
85
     * returns the list of all MySQL global variables
86
     *
87
     * @return array
88
     */
89
    protected function getMySQLglobalVariables()
90
    {
91
        return $this->getMySQLlistMultiple('VariablesGlobal', 'array_key_value');
92
    }
93
94
    /**
95
     * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column)
96
     *
97
     * @return array
98
     */
99
    protected function getMySQLlistColumns($filterArray = null)
100
    {
101
        return $this->getMySQLlistMultiple('Columns', 'full_array_key_numbered', $filterArray);
102
    }
103
104
    /**
105
     * returns a list of MySQL databases (w. choice of exclude/include the system ones)
106
     *
107
     * @return array
108
     */
109
    protected function getMySQLlistDatabases($excludeSystemDbs = true)
110
    {
111
        return $this->getMySQLlistMultiple('Databases', 'array_first_key_rest_values', $excludeSystemDbs);
112
    }
113
114
    /**
115
     * returns a list of MySQL engines (w. choice of return only the active ones)
116
     *
117
     * @return array
118
     */
119
    protected function getMySQLlistEngines($onlyActiveOnes = true)
120
    {
121
        return $this->getMySQLlistMultiple('Engines', 'array_first_key_rest_values', $onlyActiveOnes);
122
    }
123
124
    /**
125
     * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column)
126
     *
127
     * @return array
128
     */
129
    protected function getMySQLlistIndexes($filterArray = null)
130
    {
131
        return $this->getMySQLlistMultiple('Indexes', 'full_array_key_numbered', $filterArray);
132
    }
133
134
    /**
135
     * Return the list of Tables from the MySQL server
136
     *
137
     * @return string
138
     */
139
    protected function getMySQLlistTables($filterArray = null)
140
    {
141
        return $this->getMySQLlistMultiple('Tables', 'full_array_key_numbered', $filterArray);
142
    }
143
144
    /**
145
     * Returns maximum length for a given MySQL field
146
     *
147
     * @param array $fieldDetails
148
     * @param boolean $outputFormated
149
     * @return array
150
     */
151
    protected function setFieldNumbers($fieldDetails, $outputFormated = false)
152
    {
153
        $sRtrn = $this->setFieldSpecific($fieldDetails);
154
        if ($outputFormated) {
155
            if (is_array($sRtrn)) {
156
                foreach ($sRtrn as $key => $value) {
157
                    $sRtrn[$key] = $this->setNumberFormat($value);
158
                }
159
            }
160
        }
161
        return $sRtrn;
162
    }
163
164
    /**
165
     * Establishes numbers of fields
166
     *
167
     * @param array $fieldDetails
168
     * @return array
169
     */
170
    private function setFieldSpecific($fieldDetails)
171
    {
172
        if (in_array($fieldDetails['DATA_TYPE'], ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext'])) {
173
            return ['M' => $fieldDetails['CHARACTER_MAXIMUM_LENGTH']];
174
        } elseif (in_array($fieldDetails['DATA_TYPE'], ['decimal', 'numeric'])) {
175
            return ['M' => $fieldDetails['NUMERIC_PRECISION'], 'd' => $fieldDetails['NUMERIC_SCALE']];
176
        } elseif (in_array($fieldDetails['DATA_TYPE'], ['bigint', 'int', 'mediumint', 'smallint', 'tinyint'])) {
177
            return $this->setFldLmtsExact($fieldDetails['DATA_TYPE']);
178
        }
179
        return $this->setFieldSpecificElse($fieldDetails);
180
    }
181
182
    private function setFieldSpecificElse($fieldDetails)
183
    {
184
        $map = ['date' => 10, 'datetime' => 19, 'enum' => 65536, 'set' => 64, 'time' => 8, 'timestamp' => 19];
185
        if (array_key_exists($fieldDetails['DATA_TYPE'], $map)) {
186
            return ['M' => $map[$fieldDetails['DATA_TYPE']]];
187
        }
188
        return ['M' => '???'];
189
    }
190
191
    private function setFldLmtsExact($cTp)
192
    {
193
        $xct     = [
194
            'bigint'    => ['l' => -9223372036854775808, 'L' => 9223372036854775807, 's' => 21, 'sUS' => 20],
195
            'int'       => ['l' => -2147483648, 'L' => 2147483647, 's' => 11, 'sUS' => 10],
196
            'mediumint' => ['l' => -8388608, 'L' => 8388607, 's' => 9, 'sUS' => 8],
197
            'smallint'  => ['l' => -32768, 'L' => 32767, 's' => 6, 'sUS' => 5],
198
            'tinyint'   => ['l' => -128, 'L' => 127, 's' => 4, 'sUS' => 3],
199
        ];
200
        $sReturn = null;
201
        if (array_key_exists($cTp, $xct)) {
202
            $aReturn = ['m' => $xct[$cTp]['l'], 'M' => $xct[$cTp]['L'], 'l' => $xct[$cTp]['s']];
0 ignored issues
show
Unused Code introduced by
$aReturn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
203
            if (strpos($cTp, 'unsigned') !== false) {
204
                $aReturn = ['m' => 0, 'M' => ($xct[$cTp]['L'] - $xct[$cTp]['l']), 'l' => $xct[$cTp]['sUS']];
0 ignored issues
show
Unused Code introduced by
$aReturn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
205
            }
206
        }
207
        return $sReturn;
208
    }
209
}
210