Completed
Push — master ( fa85af...17ec4b )
by Adeniyi
8s
created

src/Database/DatabaseQuery.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This class handles building sql query statement and check
4
 * that the table exist in the database.
5
 *
6
 * @package Ibonly\PotatoORM\DatabaseQuery
7
 * @author  Ibraheem ADENIYI <[email protected]>
8
 * @license MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Ibonly\PotatoORM;
12
13
use PDOException;
14
use Ibonly\PotatoORM\DatabaseQueryInterface;
15
use Ibonly\PotatoORM\ColumnNotExistExeption;
16
use Ibonly\PotatoORM\InvalidConnectionException;
17
use Ibonly\PotatoORM\TableDoesNotExistException;
18
19
class DatabaseQuery implements DatabaseQueryInterface
20
{
21
    /**
22
     * connect Setup database connection
23
     */
24
    protected static function connect()
25
    {
26
        return new DBConfig();
27
    }
28
29
    /**
30
     * sanitize(argument) Removes unwanted characters
31
     *
32
     * @param  $value
33
     *
34
     * @return  string
35
     */
36
    protected static function sanitize($value)
37
    {
38
        $value = trim($value);
39
        $value = htmlentities($value);
40
        return $value;
41
    }
42
43
    /**
44
     * checkConnection
45
     *
46
     * @param  $con
47
     *
48
     * @return string
49
     */
50
    protected static function checkConnection($con)
51
    {
52
        if( $con === null )
53
        {
54
            $con = self::connect();
55
        }
56
        return $con;
57
    }
58
59
    /**
60
     * checkTableExist Check if table already in the database
61
     *
62
     * @param  $tablename
63
     * @param  $con
64
     *
65
     * @return bool
66
     */
67 View Code Duplication
    public function checkTableExist($table, $con=NULL)
68
    {
69
        $connection = $this->checkConnection($con);
70
        $query = $connection->query("SELECT 1 FROM {$table} LIMIT 1");
71
        if( $query !== false )
72
        {
73
            return true;
74
        }
75
    }
76
77
    /**
78
     * checkTableName Return the table name
79
     *
80
     * @param  $tablename
81
     * @param  $con
82
     *
83
     * @return string
84
     */
85 View Code Duplication
    public static function checkTableName($tableName, $con=NULL)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $connection = self::checkConnection($con);
88
89
        $query = $connection->query("SELECT 1 FROM {$tableName} LIMIT 1");
90
        if( $query !== false )
91
        {
92
            return $tableName;
93
        }
94
        throw new TableDoesNotExistException();
95
    }
96
97
    /**
98
     * checkColumn Check if column exist in table
99
     *
100
     * @param  $tableName
101
     * @param  $columnName
102
     * @param  $con
103
     *
104
     * @return string
105
     */
106
    protected static function checkColumn($tableName, $columnName, $con=NULL)
107
    {
108
        $connection = self::checkConnection($con);
109
110
            $result = $connection->prepare("SELECT {$columnName} FROM {$tableName}");
111
            $result->execute();
112
            if ( ! $result->columnCount() )
113
            {
114
                throw new ColumnNotExistExeption();
115
            }
116
            return $columnName;
117
    }
118
119
    /**
120
     * Get the variables declared in the Model
121
     * 
122
     * @return Array
123
     */
124
    public static function getParentClassVar()
125
    {
126
        return get_class_vars(get_called_class());
127
    }
128
129
    /**
130
     * Get the difference in variables between model and column definition
131
     * 
132
     * @param  $getClassVars
133
     * 
134
     * @return Array
135
     */
136
    public static function getColumns($getClassVars)
137
    {
138
        return array_diff($getClassVars, self::getParentClassVar());
139
    }
140
141
    /**
142
     * buildColumn  Build the column name
143
     *
144
     * @param  $data
145
     *
146
     * @return string
147
     */
148 View Code Duplication
    public static function buildColumn($getClassVars)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $counter = 0;
151
        $insertQuery = "";
152
        $columnNames = self::getColumns($getClassVars);
153
        $arraySize = count($columnNames);
154
155
        foreach ( $columnNames as $key => $value )
156
        {
157
            $counter++;
158
            $insertQuery .= self::sanitize($key);
159
            if( $arraySize > $counter )
160
                $insertQuery .= ", ";
161
        }
162
163
        return $insertQuery;
164
    }
165
166
    /**
167
     * buildValues  Build the column values
168
     *
169
     * @param  $data
170
     *
171
     * @return string
172
     */
173 View Code Duplication
    public static function buildValues($getClassVars)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        $counter = 0;
176
        $insertQuery = "";
177
        $columnNames = self::getColumns($getClassVars);
178
        $arraySize = count($columnNames);
179
180
        foreach ( $columnNames as $key => $value )
181
        {
182
            $counter++;
183
            $insertQuery .= "'".self::sanitize($value) ."'";
184
            if( $arraySize > $counter )
185
                $insertQuery .= ", ";
186
        }
187
        return $insertQuery;
188
    }
189
190
    /**
191
     * buildClause  Build the clause value
192
     *
193
     * @param  $data
194
     *
195
     * @return string
196
     */
197 View Code Duplication
    protected static function buildClause($tableName, $data)
198
    {
199
        $counter = 0;
200
        $updateQuery = "";
201
        $arraySize = count($data);
202
203
        foreach ( $data as $key => $value )
204
        {
205
            $counter++;
206
            $columnName = self::checkColumn($tableName, self::sanitize($key));
207
            $updateQuery .= $columnName ." = '".self::sanitize($value)."'";
208
            if ( $arraySize > $counter )
209
            {
210
                $updateQuery .= ", ";
211
            }
212
        }
213
        return $updateQuery;
214
    }
215
216
    /**
217
     * selectAllQuery
218
     *
219
     * @return string
220
     */
221
    public static function selectAllQuery($tableName, $field)
222
    {
223
        return "SELECT {$field} FROM {$tableName}";
224
    }
225
226
    /**
227
     * whereAndClause
228
     *
229
     * @return string
230
     */
231 View Code Duplication
    public static function whereAndClause($tableName, $data, $condition)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        $where = "";
234
        $counter = 0;
235
        $arraySize = count($data);
236
237
        foreach ( $data as $key => $value )
238
        {
239
            $counter++;
240
            $columnName = self::checkColumn($tableName, self::sanitize($key));
241
            $where .= $columnName ." = '".self::sanitize($value)."'";
242
            if ( $arraySize > $counter )
243
            {
244
                $where .= " " . $condition . " ";
245
            }
246
        }
247
248
        return $where;
249
    }
250
251
    /**
252
     * selectQuery
253
     *
254
     * @return string
255
     */
256
    public static function selectQuery($tableName, $fields, $data, $condition, $connection)
257
    {
258
        $query = "";
259
        try
260
        {
261
            $arraySize = count($data);
262
            if( $arraySize > 1 && $condition == NULL)
263
            {
264
                $query = "Please Supply the condition";
265
            }
266
            else
267
            {
268
                $columnName = self::whereAndClause($tableName, $data, $condition);
269
                $query =  "SELECT $fields FROM $tableName WHERE $columnName";
270
            }
271
        } catch (PDOException $e) {
272
            $query = $e->getMessage();
273
        }
274
275
        return $query;
276
    }
277
278
    /**
279
     * insertQuery
280
     *
281
     * @return string
282
     */
283
    public function insertQuery($tableName)
284
    {
285
        $data = ( array )$this;
286
        array_shift($data);
287
288
        $columnNames = self::buildColumn($data);
289
        $values = self::buildValues($data);
290
291
        return "INSERT INTO $tableName ({$columnNames}) VALUES ({$values})";
292
    }
293
294
    /**
295
     * updateQuery
296
     *
297
     * @return string
298
     */
299
    public function updateQuery($tableName)
300
    {
301
        $data = ( array ) $this;
302
        $data = array_slice($data, 2);
303
304
        $values = self::buildClause($tableName, $data);
305
        $updateQuery = "UPDATE $tableName SET {$values} WHERE id = ". self::sanitize($this->id);
306
307
        return $updateQuery;
308
    }
309
}