Db::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
rs 9.4285
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Db.php
9
 */
10
namespace JaegerApp;
11
12
use JaegerApp\Exceptions\DbException;
13
14
/**
15
 * Jaeger - Database Object
16
 *
17
 * Wrapper for a simple database interface
18
 *
19
 * @package Database
20
 * @author Eric Lamb <[email protected]>
21
 */
22
class Db
23
{
24
    /**
25
     * The external db object
26
     * 
27
     * @var Db\DbInterface
28
     */
29
    protected $db = null;
30
31
    /**
32
     * The database connection details
33
     * 
34
     * @var array
35
     */
36
    protected $credentials = array();
37
38
    /**
39
     * The columns we want
40
     * 
41
     * @var array
42
     */
43
    protected $columns = array();
44
45
    /**
46
     * The WHERE for the SQL
47
     * 
48
     * @var array|string
49
     */
50
    protected $where = '1=1';
51
52
    /**
53
     * The table name we're working with
54
     * 
55
     * @var string
56
     */
57
    protected $table = false;
58
    
59
    /**
60
     * How we want to access the database engine
61
     * mysqli is the default
62
     * @var string
63
     */
64
    protected $access_type = 'mysqli';
65
    
66
    /**
67
     * Sets the method of accessing the database
68
     * @param string $type
69
     * @return JaegerApp\Db
70
     */
71
    public function setAccessType($type)
72
    {
73
        $this->access_type = $type;
74
        return $this;
75
    }
76
    
77
    /**
78
     * Returns the access type for how we're interacting with the database
79
     * @return string
80
     */
81
    public function getAccessType()
82
    {
83
        return $this->access_type;
84
    }
85
86
    /**
87
     * Set the columns we want to return
88
     * 
89
     * @param array $columns            
90
     * @return \JaegerApp\Db
91
     */
92
    public function select(array $columns = array())
93
    {
94
        $this->columns = $columns;
95
        return $this;
96
    }
97
98
    /**
99
     * Sets the main table we're pulling from
100
     * 
101
     * @param string $table            
102
     * @return \JaegerApp\Db
103
     */
104
    public function from($table)
105
    {
106
        $this->table = $table;
107
        return $this;
108
    }
109
110
    /**
111
     * Sets the SQL WHERE
112
     *
113
     * Can be either a string or an array of key=>value pairs
114
     * 
115
     * @param array $where            
116
     * @return \JaegerApp\Db
117
     */
118
    public function where($where = array())
119
    {
120
        $this->where = $where;
121
        return $this;
122
    }
123
124
    /**
125
     * Returns the SQL WHERE
126
     * 
127
     * @return array|string
128
     */
129
    public function getWhere()
130
    {
131
        return $this->where;
132
    }
133
134
    /**
135
     * Returns the table
136
     * 
137
     * @return string
138
     */
139
    public function getTable()
140
    {
141
        return $this->table;
142
    }
143
144
    /**
145
     * Set the credentials for accessing the database
146
     * 
147
     * @param array $credentials            
148
     * @return \JaegerApp\Db
149
     */
150
    public function setCredentials(array $credentials)
151
    {
152
        $this->credentials = $credentials;
153
        return $this;
154
    }
155
156
    /**
157
     * Returns the credentials
158
     * 
159
     * @return \JaegerApp\array
160
     */
161
    public function getCredentials()
162
    {
163
        return $this->credentials;
164
    }
165
166
    /**
167
     * Executes and performs a query
168
     * 
169
     * @return array
170
     */
171
    public function get()
172
    {
173
        return $this->getDb()->select($this->getTable(), $this->getWhere())->get();
174
    }
175
176
    /**
177
     * Inserts data into the $table
178
     * 
179
     * @param string $table            
180
     * @param data $data        
181
     * @return false|int|string false on error
182
     */
183
    public function insert($table, $data = array())
184
    {
185
        return $this->getDb()->insert($table, $data);
186
    }
187
188
    /**
189
     * Updates a table
190
     * 
191
     * @param string $table            
192
     * @param array $data            
193
     * @param string $where            
194
     */
195
    public function update($table, $data = array(), $where = '1=1')
196
    {
197
        if (! $this->getDb()->update($table, $data, $where)) {
198
            throw new DbException("Failed updating " . $table);
199
        }
200
        
201
        return true;
202
    }
203
204
    /**
205
     * Truncates a given table
206
     * 
207
     * @param string $table            
208
     */
209
    public function emptyTable($table)
210
    {
211
        return $this->getDb()->query("TRUNCATE " . $table);
212
    }
213
214
    /**
215
     * Executes a query and, optionally, returns the result
216
     * 
217
     * @param string $sql            
218
     * @param bool $return            
219
     * @return array|void
220
     */
221
    public function query($sql, $return = false)
222
    {
223
        $query = $this->getDb()->query($sql, true);
224
        if ($return) {
225
            return $query;
226
        }
227
    }
228
229
    /**
230
     * Returns an instance of the database object
231
     * 
232
     * @todo Update engine selection to be 
233
     *          polymorphic instead of conditional
234
     * @return Db\DbInterface
235
     * @throws Exceptions\DbException
236
     */
237
    public function getDb()
238
    {
239
        if (is_null($this->db)) {
240
            
241
            //try explicit type setting
242
            if( $this->getAccessType() == 'mysqli' && function_exists('mysqli_select_db') ){
243
                $this->db = new Db\Mysqli();
244
            }
245
            else 
246
            {
247
                if ( $this->getAccessType() == 'pdo' && class_exists('PDO') ){
248
                    $this->db = new Db\Pdo();
249
                }
250
            }
251
            
252
            //fuck it; we're just gonna choose one then
253
            if(is_null($this->db)){
254
                if( class_exists('PDO') ){
255
                    $this->db = new Db\Pdo(); 
256
                }
257
                elseif ( function_exists('mysqli_select_db') ) {
258
                    $this->db = new Db\Mysqli();
259
                }
260
                else {
261
                    throw new DbException('Database engine not available! Must be either PDO or mysqli');
262
                }
263
            }
264
            
265
            $this->db->setCredentials($this->credentials);
266
        }
267
        
268
        return $this->db;
269
    }
270
271
    /**
272
     * Returns all the available tables
273
     * 
274
     * @return array
275
     */
276
    public function getTables()
277
    {
278
        $tables = $this->getDb()->getAllTables();
279
        $return = array();
280
        foreach ($tables as $name => $table) {
281
            foreach ($table as $key => $value) {
282
                $return[$table[$key]] = $table[$key];
283
            }
284
        }
285
        
286
        return $return;
287
    }
288
289
    /**
290
     * Returns the details for all the tables
291
     * 
292
     * @return array
293
     */
294
    public function getTableStatus()
295
    {
296
        $tables = $this->getDb()->getTableStatus();
297
        return $tables;
298
    }
299
300
    /**
301
     * Returns the CREATE TABLE statement for the given $table
302
     * 
303
     * @param string $table
304
     *            The name of the table to create a statement for
305
     * @param bool $if_not_exists
306
     *            If set to true, the statement will append IF NOT EXISTS
307
     * @return Ambigous <boolean, mixed>
308
     */
309
    public function getCreateTable($table, $if_not_exists = false)
310
    {        
311
        return $this->getDb()->getCreateTable($table, $if_not_exists);
312
    }
313
314
    /**
315
     * Escapes a string for db use
316
     * 
317
     * @param string $string            
318
     * @return Ambigous <\voku\db\array, \voku\db\bool, \voku\db\float, \voku\db\int, \voku\db\string>
319
     */
320
    public function escape($string)
321
    {
322
        return $this->getDb()->escape($string);
323
    }
324
325
    /**
326
     * Changes the database to $db_name
327
     * 
328
     * @param string $db_name
329
     *            The name of the database we want to change to
330
     * @return \JaegerApp\Db
331
     */
332
    public function setDbName($db_name)
333
    {
334
        $this->getDb()->setDbName($db_name);
335
        return $this;
336
    }
337
    
338
    /**
339
     * Clears any records in memory associated with a result set.
340
     * It allows calling with no select query having occurred.
341
     *
342
     * @access public
343
     */
344
    public function clear()
345
    {
346
        $this->getDb()->clear();
347
    }
348
    
349
    /**
350
     * Returns the total number of rows in a given table without filtering
351
     * @param string $table The name of the table
352
     * @return number
353
     */
354
    public function totalRows($table)
355
    {
356
        return $this->getDb()->totalRows($table);
357
    }
358
    
359
    /**
360
     * Returns the columns on a given table
361
     * @param string $table
362
     * @return array
363
     */
364
    public function getColumns($table)
365
    {
366
        return $this->getDb()->getColumns($table);
367
    }
368
    
369
    /**
370
     * Determines whether a given database exists
371
     * @param string $name
372
     * @return boolean
373
     */
374
    public function checkDbExists($name)
375
    {
376
        $data = $this->query("SELECT COUNT(*) AS total FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".$this->escape($name)."'", true);
377
        if( isset($data['0']['total']) && $data['0']['total'] == '1' )
378
        {
379
            return true;    
380
        }
381
        
382
        return false;
383
    }
384
    
385
}