Completed
Push — master ( 8dbe44...7cd8ac )
by Daniel
03:24
created

sQueryMySqlActiveDatabases()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

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.4285
cc 2
eloc 10
nc 2
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
 * Queries for the MySQL module
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait MySQLiByDanielGPqueriesBasic
37
{
38
39
    private function sCleanParameters(&$parameters)
40
    {
41
        if (is_array($parameters)) {
42
            $tmpArray = [];
43
            foreach ($parameters as &$value) {
44
                $tmpArray[] = filter_var($value, FILTER_SANITIZE_STRING);
45
            }
46
            $parameters = $tmpArray;
47
        } else {
48
            $parameters = filter_var($parameters, FILTER_SANITIZE_STRING);
49
        }
50
    }
51
52
    protected function sQueryGenericSelectKeyValue($parameters)
53
    {
54
        $this->sCleanParameters($parameters);
55
        return implode(' ', [
56
            'SELECT',
57
            $parameters[0] . ',' . $parameters[1],
58
            'FROM',
59
            $parameters[2],
60
            'GROUP BY',
61
            $parameters[1] . ';'
62
        ]);
63
    }
64
65
    /**
66
     * Query to list Databases
67
     *
68
     * @param type $excludeSystemDbs
69
     * @return type
70
     */
71
    protected function sQueryMySqlActiveDatabases($excludeSystemDbs = true)
72
    {
73
        $sDBs = 'WHERE '
74
                . '`SCHEMA_NAME` NOT IN ("'
75
                . implode('", "', ['information_schema', 'mysql', 'performance_schema', 'sys']) . '") ';
76
        return 'SELECT '
77
                . '`SCHEMA_NAME` As `Db`, `DEFAULT_CHARACTER_SET_NAME` AS `DbCharset`, '
78
                . '`DEFAULT_COLLATION_NAME` AS `DbCollation` '
79
                . 'FROM `information_schema`.`SCHEMATA` '
80
                . ($excludeSystemDbs ? $sDBs : '')
81
                . 'GROUP BY `SCHEMA_NAME`;';
82
    }
83
84
    /**
85
     * Query to list MySQL engines
86
     *
87
     * @param string $onlyActiveOnes
88
     * @return type
89
     */
90
    protected function sQueryMySqlActiveEngines($onlyActiveOnes = true)
91
    {
92
        return 'SELECT '
93
                . '`ENGINE` AS `Engine`, `SUPPORT` AS `Support`, `COMMENT` AS `Comment` '
94
                . 'FROM `information_schema`.`ENGINES` '
95
                . ($onlyActiveOnes ? 'WHERE (`SUPPORT` IN ("DEFAULT", "YES")) ' : '')
96
                . 'GROUP BY `ENGINE`;';
97
    }
98
99
    /**
100
     * Query to list Global Variables
101
     *
102
     * @return string
103
     */
104
    protected function sQueryMySqlGlobalVariables()
105
    {
106
        return 'SHOW GLOBAL VARIABLES;';
107
    }
108
109
    /**
110
     * The MySQL server time
111
     *
112
     * @return string
113
     */
114
    protected function sQueryMySqlServerTime()
115
    {
116
        return 'SELECT NOW();';
117
    }
118
119
    /**
120
     * Get all the row details from a table based on given filter
121
     *
122
     * @param array $parameters
123
     * @return string
124
     */
125
    protected function sQueryRowsFromTable($parameters)
126
    {
127
        $this->sCleanParameters($parameters);
128
        return 'SELECT * '
129
                . 'FROM `' . $parameters[0] . '` '
130
                . 'WHERE ' . $parameters[1] . ';';
131
    }
132
133
    protected function sQueryToDeleteSingleIdentifier($parameters)
134
    {
135
        $this->sCleanParameters($parameters);
136
        return 'DELETE '
137
                . 'FROM `' . $parameters[0] . '` '
138
                . 'WHERE `' . $parameters[1] . '` = "' . $parameters[2] . '";';
139
    }
140
}
141