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

MySQLiByDanielGPnumbers::getFieldValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 3
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 MySQLiByDanielGPnumbers
37
{
38
39
    use DomComponentsByDanielGP;
40
41
    /**
42
     * Returns given value for a field from REQUEST global variable
43
     *
44
     * @param array $details
45
     * @return string
46
     */
47
    protected function getFieldValue($details)
48
    {
49
        $this->initializeSprGlbAndSession();
50
        $rqCN = $this->tCmnRequest->request->get($details['COLUMN_NAME']);
51
        if (!is_null($rqCN)) {
52
            if (($details['IS_NULLABLE'] == 'YES') && ($rqCN == '')) {
53
                return 'NULL';
54
            }
55
            return $rqCN;
56
        }
57
        return $this->getFieldValueWithoutUserInput($details);
58
    }
59
60
    /**
61
     * Handles field value ignoring any input from the user
62
     *
63
     * @param array $details
64
     * @return string
65
     */
66
    private function getFieldValueWithoutUserInput($details)
67
    {
68
        if ($details['COLUMN_DEFAULT'] === null) {
69
            if ($details['IS_NULLABLE'] == 'YES') {
70
                return 'NULL';
71
            }
72
            return '';
73
        }
74
        return $details['COLUMN_DEFAULT'];
75
    }
76
77
    /**
78
     * Returns maximum length for a given MySQL field
79
     *
80
     * @param array $fieldDetails
81
     * @param boolean $outputFormated
82
     * @return array
83
     */
84
    protected function setFieldNumbers($fieldDetails, $outputFormated = false)
85
    {
86
        $sRtrn = $this->setFieldSpecific($fieldDetails);
87
        if ($outputFormated) {
88
            if (is_array($sRtrn)) {
89
                foreach ($sRtrn as $key => $value) {
90
                    $sRtrn[$key] = $this->setNumberFormat($value);
91
                }
92
            }
93
        }
94
        return $sRtrn;
95
    }
96
97
    /**
98
     * Establishes numbers of fields
99
     *
100
     * @param array $fieldDetails
101
     * @return array
102
     */
103
    private function setFieldSpecific($fieldDetails)
104
    {
105
        if (in_array($fieldDetails['DATA_TYPE'], ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext'])) {
106
            return ['M' => $fieldDetails['CHARACTER_MAXIMUM_LENGTH']];
107
        } elseif (in_array($fieldDetails['DATA_TYPE'], ['decimal', 'numeric'])) {
108
            return ['M' => $fieldDetails['NUMERIC_PRECISION'], 'd' => $fieldDetails['NUMERIC_SCALE']];
109
        } elseif (in_array($fieldDetails['DATA_TYPE'], ['bigint', 'int', 'mediumint', 'smallint', 'tinyint'])) {
110
            return $this->setFldLmtsExact($fieldDetails['DATA_TYPE']);
111
        }
112
        return $this->setFieldSpecificElse($fieldDetails);
113
    }
114
115
    private function setFieldSpecificElse($fieldDetails)
116
    {
117
        $map = ['date' => 10, 'datetime' => 19, 'enum' => 65536, 'set' => 64, 'time' => 8, 'timestamp' => 19];
118
        if (array_key_exists($fieldDetails['DATA_TYPE'], $map)) {
119
            return ['M' => $map[$fieldDetails['DATA_TYPE']]];
120
        }
121
        return ['M' => '???'];
122
    }
123
124
    private function setFldLmtsExact($cTp)
125
    {
126
        $xct     = [
127
            'bigint'    => ['l' => -9223372036854775808, 'L' => 9223372036854775807, 's' => 21, 'sUS' => 20],
128
            'int'       => ['l' => -2147483648, 'L' => 2147483647, 's' => 11, 'sUS' => 10],
129
            'mediumint' => ['l' => -8388608, 'L' => 8388607, 's' => 9, 'sUS' => 8],
130
            'smallint'  => ['l' => -32768, 'L' => 32767, 's' => 6, 'sUS' => 5],
131
            'tinyint'   => ['l' => -128, 'L' => 127, 's' => 4, 'sUS' => 3],
132
        ];
133
        $aReturn = null;
134
        if (array_key_exists($cTp, $xct)) {
135
            $aReturn = ['m' => $xct[$cTp]['l'], 'M' => $xct[$cTp]['L'], 'l' => $xct[$cTp]['s']];
136
            if (strpos($cTp, 'unsigned') !== false) {
137
                $aReturn = ['m' => 0, 'M' => ($xct[$cTp]['L'] - $xct[$cTp]['l']), 'l' => $xct[$cTp]['sUS']];
138
            }
139
        }
140
        return $aReturn;
141
    }
142
143
    protected function setMySQLqueryValidateInputs($prm)
144
    {
145
        $rMap = $this->setMySQLqueryValidationMap();
146
        if (array_key_exists($prm['returnType'], $rMap)) {
147
            $elC = [$prm['NoOfRows'], $rMap[$prm['returnType']]['r'][0], $rMap[$prm['returnType']]['r'][1]];
148
            if (filter_var($elC[0], FILTER_VALIDATE_INT, ['min_range' => $elC[1], 'max_range' => $elC[2]]) === false) {
149
                $msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected' . $rMap[$prm['returnType']][2]);
150
                return [false, sprintf($msg, $prm['NoOfColumns'])];
151
            }
152
            $elR = [$prm['NoOfColumns'], $rMap[$prm['returnType']]['c'][0], $rMap[$prm['returnType']]['c'][1]];
153
            if (filter_var($elR[0], FILTER_VALIDATE_INT, ['min_range' => $elR[1], 'max_range' => $elR[2]])) {
154
                return [true, ''];
155
            }
156
            $msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected' . $rMap[$prm['returnType']][1]);
157
            return [false, sprintf($msg, $prm['NoOfColumns'])];
158
        }
159
        return [false, $prm['returnType'] . ' is not defined!'];
160
    }
161
162
    private function setMySQLqueryValidationMap()
163
    {
164
        $lngKey = 'full_array_key_numbered_with_record_number_prefix';
165
        return [
166
            'array_first_key_rest_values'         => ['r' => [1, 999999], 'c' => [2, 99], 'AtLeast2ColsResultedOther'],
167
            'array_key_value'                     => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'],
168
            'array_key_value2'                    => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'],
169
            'array_key2_value'                    => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'],
170
            'array_numbered'                      => ['r' => [1, 999999], 'c' => [1, 1], '1ColumnResultedOther'],
171
            'array_pairs_key_value'               => ['r' => [1, 1], 'c' => [1, 99], '1RowManyColumnsResultedOther'],
172
            'full_array_key_numbered'             => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'],
173
            'full_array_key_numbered_with_prefix' => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'],
174
            $lngKey                               => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'],
175
            'value'                               => ['r' => [1, 1], 'c' => [1, 1], '1ResultedOther'],
176
        ];
177
    }
178
}
179