MySQLiByDanielGPtypes::getMySQLqueryType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 17
rs 9.7998
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
 * Handles the size of field limits
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait MySQLiByDanielGPtypes
37
{
38
39
    /**
40
     * Returns the Query language type by scanning the 1st keyword from a given query
41
     *
42
     * @param string $sQuery
43
     */
44
    protected function getMySQLqueryType($sQuery)
45
    {
46
        $queryPieces    = explode(' ', $sQuery);
47
        $statementTypes = $this->listOfMySQLqueryStatementType($queryPieces[0]);
48
        if (in_array($queryPieces[0], $statementTypes['keys'])) {
49
            $type    = $statementTypes['value']['Type'];
50
            $ar1     = ['1st Keyword Within Query' => $queryPieces[0]];
51
            $lnT     = $this->listOfMySQLqueryLanguageType($type);
52
            $aReturn = array_merge($ar1, $lnT, $statementTypes['value']);
53
            ksort($aReturn);
54
            return $aReturn;
55
        }
56
        return [
57
            'detected1stKeywordWithinQuery' => $queryPieces[0],
58
            'unknown'                       => ['standsFor' => 'unknown', 'description' => 'unknown'],
59
            'Type'                          => 'unknown',
60
            'Description'                   => 'unknown',
61
        ];
62
    }
63
64
    /**
65
     * Just to keep a list of type of language as array
66
     *
67
     * @return array
68
     */
69
    private function listOfMySQLqueryLanguageType($qType)
70
    {
71
        $keyForReturn = 'Type ' . $qType . ' stands for';
72
        $vMap         = ['DCL', 'DDL', 'DML', 'DQL', 'DTL'];
73
        if (in_array($qType, $vMap)) {
74
            $valForReturn = $this->readTypeFromJsonFile('MySQLiLanguageTypes')[$qType];
75
            return [$keyForReturn => $valForReturn[0] . ' (' . $valForReturn[1] . ')'];
76
        }
77
        return [$keyForReturn => 'unknown'];
78
    }
79
80
    /**
81
     * Just to keep a list of statement types as array
82
     *
83
     * @param string $firstKwordWQuery
84
     * @return array
85
     */
86
    private function listOfMySQLqueryStatementType($firstKwordWQuery)
87
    {
88
        $statmentsArray = $this->readTypeFromJsonFile('MySQLiStatementTypes');
89
        return [
90
            'keys'  => array_keys($statmentsArray),
91
            'value' => [
92
                'Description' => $statmentsArray[$firstKwordWQuery][1],
93
                'Type'        => $statmentsArray[$firstKwordWQuery][0],
94
            ],
95
        ];
96
    }
97
98
    /**
99
     *
100
     * @param string $fileBaseName
101
     * @return array
102
     */
103
    private function readTypeFromJsonFile($fileBaseName)
104
    {
105
        $fName = __DIR__ . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . $fileBaseName . '.min.json';
106
        $fJson = fopen($fName, 'r');
107
        if ($fJson === false) {
108
            throw new \Exception('Unable to read file ' . $fName);
109
        }
110
        $jSonContent = fread($fJson, filesize($fName));
111
        fclose($fJson);
112
        return json_decode($jSonContent, true);
113
    }
114
}
115