Issues (473)

src/Database/Query/Query.php (29 issues)

1
<?php
2
3
/**
4
 * This file is part of the alphaz Framework.
5
 *
6
 * @author Muhammad Umer Farooq (Malik) <[email protected]>
7
 *
8
 * @link https://github.com/alphazframework/framework
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 *
13
 * @license MIT
14
 */
15
16
namespace alphaz\Database\Query;
17
18
class Query
19
{
20
    /**
21
     * Prepare a query to insert into db.
22
     *
23
     * @param
24
     * $table Name of tabke
25
     * array array(); e.g:
26
     *           'name' => 'new name' or $comeformvariable
27
     *           'username' => 'new username' or $comeformvariable
28
     *
29
     * @return query
30
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $table at position 0 could not be parsed: Unknown type name '$table' at position 0 in $table.
Loading history...
31
    public function insert($params)
32
    {
33
        if (is_array($params)) {
34
            $count_rows = count($params['columns']);
35
            $increment = 1;
36
            foreach ($params['columns'] as $keys => $value) {
37
                for ($i = 1; $i <= $count_rows; $i++) {
38
                    $data[$keys] = $value;
39
                }
40
            }
41
            foreach ($data as $keys => $values) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
42
                if ($increment == $count_rows) {
43
                    $columns[] = "{$keys} = '{$values}'";
44
                } else {
45
                    $columns[] = "{$keys} = '{$values}'";
46
                }
47
                $increment++;
48
            }
49
            $columns = implode(' , ', $columns);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $columns seems to be defined by a foreach iteration on line 41. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
50
            $query = "INSERT INTO `{$params['table']}`SET {$columns}";
51
            if (isset($params['debug']) and strtolower($params['debug']) === 'on') {
52
                var_dump($query);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($query) looks like debug code. Are you sure you do not want to remove it?
Loading history...
53
            }
54
55
            return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
56
        } else {
57
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
58
        }
59
    }
60
61
    /**
62
     * Prepare a query to Update data in database.
63
     *
64
     * @param array $params; e.g:
65
     *                       'table' required name of table
66
     *                       'db_name' => Database name
67
     *                       'wheres' Specify id or else for updating records
68
     *                       'columns' => data e.g name=>new name
69
     *
70
     * @return query
71
     */
72
    public function update($params)
73
    {
74
        if (is_array($params)) {
75
            $count_rows = count($params['columns']);
76
            $increment = 1;
77
            foreach ($params['columns'] as $keys => $value) {
78
                for ($i = 1; $i <= $count_rows; $i++) {
79
                    $data[$keys] = $value;
80
                }
81
            }
82
            foreach ($data as $keys => $values) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
83
                if ($increment == $count_rows) {
84
                    $columns[] = "{$keys} = '{$values}'";
85
                } else {
86
                    $columns[] = "{$keys} = '{$values}'";
87
                }
88
                $increment++;
89
            }
90
            $columns = implode(' , ', $columns);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $columns seems to be defined by a foreach iteration on line 82. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
91
            $wheres = $this->prepareWhere($params['wheres']);
92
            $query = "UPDATE `{$params['table']}`SET {$columns} {$wheres}";
93
            if (isset($params['debug']) and strtolower($params['debug']) === 'on') {
94
                var_dump($query);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($query) looks like debug code. Are you sure you do not want to remove it?
Loading history...
95
            }
96
97
            return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
98
        } else {
99
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
100
        }
101
    }
102
103
    /**
104
     * Prepare a query to select data from database.
105
     *
106
     * @param array array();
107
     *           'table' Names of table
108
     *           'db_name' => Database name
109
     *           'params' Names of columns which you want to select
110
     *           'wheres' Specify a selection criteria to get required records
111
     *            'debug' If on var_dump sql query
112
     *
113
     * @return query
114
     */
115
    public function select($params)
116
    {
117
        if (is_array($params)) {
118
            if (!isset($params['params'])) {
119
                $columns = '*';
120
            } else {
121
                $columns = implode(', ', array_values($params['params']));
122
            }
123
            if (isset($params['distinct'])) {
124
                $distinct = ' DISTINCT ';
125
            } else {
126
                $distinct = '';
127
            }
128
            $wheres = (isset($params['wheres'])) ? $this->prepareWhere($params['wheres']) : '';
129
            if (isset($params['joins'])) {
130
                if (!empty($params['joins'])) {
131
                    if (!isset($params['joins']['using'])) {
132
                        $join = ' JOIN '.$params['joins']['table2'].' ON '.$params['joins']['column1'].' = '.$params['joins']['column2'];
133
                    } else {
134
                        $join = ' JOIN '.$params['joins']['table2'].' Using '.$params['joins']['using'];
135
                    }
136
                }
137
            } else {
138
                $join = '';
139
            }
140
            if (isset($params['limit'])) {
141
                if (!empty($params['limit'])) {
142
                    $limit = ' LIMIT '.$params['limit']['start'].' OFFSET '.$params['limit']['end'];
143
                } else {
144
                    $limit = '';
145
                }
146
            } else {
147
                $limit = '';
148
            }
149
            if (isset($params['order_by'])) {
150
                if (!empty($params['order_by'])) {
151
                    $order_by = ' ORDER BY '.$params['order_by'];
152
                } else {
153
                    $order_by = '';
154
                }
155
            } else {
156
                $order_by = '';
157
            }
158
            $query = "SELECT {$distinct} {$columns} FROM {$params['table']} {$join} {$wheres} {$order_by} {$limit} ;";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $join does not seem to be defined for all execution paths leading up to this point.
Loading history...
159
            if (isset($params['debug']) and strtolower($params['debug']) === 'on') {
160
                var_dump($query);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($query) looks like debug code. Are you sure you do not want to remove it?
Loading history...
161
            }
162
163
            return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
164
        } else {
165
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
166
        }
167
    }
168
169
    /**
170
     * Prepare a query to delete data from database.
171
     *
172
     * @param $params array array();
173
     *                 'table' Names of table
174
     *                 'db_name' => Database name
175
     *                 'wheres' Specify a selection criteria to get required records
176
     *
177
     * @return query
178
     */
179
    public function delete($params)
180
    {
181
        if (is_array($params)) {
182
            if (!empty($params['wheres'])) {
183
                $wheres = $this->prepareWhere($params['wheres']);
184
            } else {
185
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
186
            }
187
            $query = "DELETE FROM `{$params['table']}` {$wheres};";
188
            if (isset($params['debug']) and strtolower($params['debug']) === 'on') {
189
                var_dump($query);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($query) looks like debug code. Are you sure you do not want to remove it?
Loading history...
190
            }
191
192
            return $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
193
        } else {
194
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
195
        }
196
    }
197
198
    /**
199
     * prepare the where statement.
200
     *
201
     * @param 'wheres' Specify a selection criteria to get required records
0 ignored issues
show
The type alphaz\Database\Query\Specify was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
202
     *
203
     * @return query
204
     */
205
    public function prepareWhere($wheres = null)
206
    {
207
        if (isset($wheres)) {
208
            if (!empty($wheres)) {
209
                $wheres = ' WHERE '.implode(' and ', array_values($wheres));
210
            } else {
211
                $wheres = '';
212
            }
213
        } else {
214
            $wheres = '';
215
        }
216
217
        return $wheres;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $wheres returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
218
    }
219
220
    /**
221
     * Prepare the use statement.
222
     *
223
     * @param $name name of database
0 ignored issues
show
The type alphaz\Database\Query\name was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
224
     *
225
     * @return query
226
     */
227
    public function useQuery($name)
228
    {
229
        return "USE `{$name}`";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'USE `'.$name.'`' returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
230
    }
231
232
    /**
233
     * Creating database query if not exists.
234
     *
235
     * @param $name name of database
236
     *
237
     * @return query
238
     */
239
    public function createDb($name)
240
    {
241
        if (isset($name) && !empty(trim($name))) {
242
            $sql = "CREATE DATABASE IF NOT EXISTS `{$name}`";
243
244
            return $sql;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sql returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
245
        } else {
246
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
247
        }
248
    }
249
250
    /**
251
     * Deleting database query if not exists.
252
     *
253
     * @param $name name of database
254
     *
255
     * @return query
256
     */
257
    public function deleteDb($name)
258
    {
259
        if (isset($name) && !empty(trim($name))) {
260
            $sql = "DROP DATABASE `{$name}` ";
261
262
            return $sql;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sql returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
263
        } else {
264
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
265
        }
266
    }
267
268
    /**
269
     * Deleting table  query if not exists.
270
     *
271
     * @param $dbname name of database
272
     *                 $table => $table name
273
     *
274
     * @return query
275
     */
276
    public function deleteTbl($table)
277
    {
278
        if (isset($table) && !empty(trim($table))) {
279
            $sql = "DROP TABLE `{$table}` ";
280
281
            return $sql;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sql returns the type string which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
282
        } else {
283
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type alphaz\Database\Query\query.
Loading history...
284
        }
285
    }
286
}
287