Passed
Push — master ( ad15ea...7afef2 )
by Darío
02:13
created

AbstractDriver::getDbhost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Db\Driver;
12
13
use Drone\Error\Errno;
14
15
/**
16
 * AbstractDriver Class
17
 *
18
 * This class defines standard behavior for database drivers
19
 */
20
abstract class AbstractDriver
21
{
22
    use \Drone\Error\ErrorTrait;
23
24
    /**
25
     * Database host
26
     *
27
     * @var string
28
     */
29
    protected $dbhost;
30
31
    /**
32
     * Database user
33
     *
34
     * @var string
35
     */
36
    protected $dbuser;
37
38
    /**
39
     * Database password
40
     *
41
     * @var string
42
     */
43
    protected $dbpass;
44
45
    /**
46
     * Database name
47
     *
48
     * @var string
49
     */
50
    protected $dbname;
51
52
    /**
53
     * Database charset
54
     *
55
     * @var string
56
     */
57
    protected $dbchar;
58
59
    /**
60
     * Database port
61
     *
62
     * @var integer
63
     */
64
    protected $dbport;
65
66
    /**
67
     * Connection identifier
68
     *
69
     * @var object|resource
70
     */
71
    protected $dbconn;
72
73
    /**
74
     * Rows returned on execute() method
75
     *
76
     * @var integer
77
     */
78
    protected $numRows;
79
80
    /**
81
     * Fields returned on execute() method
82
     *
83
     * @var integer
84
     */
85
    protected $numFields;
86
87
    /**
88
     * Rows affected returned on execute() method
89
     *
90
     * @var integer
91
     */
92
    protected $rowsAffected;
93
94
    /**
95
     * Statement handle
96
     *
97
     * @var resource|mixed
98
     */
99
    protected $result;
100
101
    /**
102
     * Data stored in select statements
103
     *
104
     * @var array
105
     */
106
    protected $arrayResult;
107
108
    /**
109
     * Defines if consecutive querys are part of a transaction
110
     *
111
     * @var boolean
112
     */
113
    protected $transac_mode = false;
114
115
    /**
116
     * Latest result of a transaction process
117
     *
118
     * The first time would be a result of a query(), execute() or parse() methods
119
     * On subsequent times it's a boolean resulting of the boolean operation AND with the latest result.
120
     *
121
     * @var boolean
122
     */
123
    protected $transac_result = null;
124
125
    /**
126
     * Autocommit behavior
127
     *
128
     * @var boolean
129
     */
130
    protected $autocommit = true;
131
132
    /**
133
     * Returns the dbhost attribute
134
     *
135
     * @return string
136
     */
137
    public function getDbhost()
138
    {
139
        return $this->dbhost;
140
    }
141
142
    /**
143
     * Returns the dbuser attribute
144
     *
145
     * @return string
146
     */
147
    public function getDbuser()
148
    {
149
        return $this->dbuser;
150
    }
151
152
    /**
153
     * Returns the dbname attribute
154
     *
155
     * @return string
156
     */
157
    public function getDbname()
158
    {
159
        return $this->dbname;
160
    }
161
162
    /**
163
     * Returns the dbchar attribute
164
     *
165
     * @return string
166
     */
167
    public function getDbchar()
168
    {
169
        return $this->dbchar;
170
    }
171
172
    /**
173
     * Returns the dbport attribute
174
     *
175
     * @return string
176
     */
177
    public function getDbport()
178
    {
179
        return $this->dbport;
180
    }
181
182
    /**
183
     * Returns the numRows attribute
184
     *
185
     * @return integer
186
     */
187
    public function getNumRows()
188
    {
189
        return $this->numRows;
190
    }
191
192
    /**
193
     * Returns the numFields attribute
194
     *
195
     * @return integer
196
     */
197
    public function getNumFields()
198
    {
199
        return $this->numFields;
200
    }
201
202
    /**
203
     * Returns the rowsAffected attribute
204
     *
205
     * @return integer
206
     */
207
    public function getRowsAffected()
208
    {
209
        return $this->rowsAffected;
210
    }
211
212
    /**
213
     * Returns an array with all results of the last execute statement
214
     *
215
     * @return array
216
     */
217
    public function getArrayResult()
218
    {
219
        if (!empty($this->arrayResult))
220
            return $this->arrayResult;
221
222
        return $this->toArray();
223
    }
224
225
    /**
226
     * Returns the autoCommit attribute
227
     *
228
     * @return boolean
229
     */
230
    public function getAutoCommit()
231
    {
232
        return $this->autoCommit;
0 ignored issues
show
Bug introduced by
The property autoCommit does not exist on Drone\Db\Driver\AbstractDriver. Did you mean autocommit?
Loading history...
233
    }
234
235
    /**
236
     * Sets dbhost attribute
237
     *
238
     * @param string $value
239
     *
240
     * @return null
241
     */
242
    public function setDbhost($value)
243
    {
244
        $this->dbhost = $value;
245
    }
246
247
    /**
248
     * Sets dbuser attribute
249
     *
250
     * @param string $value
251
     *
252
     * @return null
253
     */
254
    public function setDbuser($value)
255
    {
256
        $this->dbuser = $value;
257
    }
258
259
    /**
260
     * Sets dbpass attribute
261
     *
262
     * @param string $value
263
     *
264
     * @return null
265
     */
266
    public function setDbpass($value)
267
    {
268
        $this->dbpass = $value;
269
    }
270
271
    /**
272
     * Sets dbname attribute
273
     *
274
     * @param string $value
275
     *
276
     * @return null
277
     */
278
    public function setDbname($value)
279
    {
280
        $this->dbname = $value;
281
    }
282
283
    /**
284
     * Sets dbchar attribute
285
     *
286
     * @param string $value
287
     *
288
     * @return null
289
     */
290
    public function setDbchar($value)
291
    {
292
        $this->dbchar = $value;
293
    }
294
295
    /**
296
     * Sets dbport attribute
297
     *
298
     * @param integer $value
299
     *
300
     * @return null
301
     */
302
    public function setDbport($value)
303
    {
304
        $this->dbport = $value;
305
    }
306
307
    /**
308
     * Sets autocommit behavior
309
     *
310
     * @param boolean $value
311
     *
312
     * @return null
313
     */
314
    public function autocommit($value)
315
    {
316
        if ($this->transac_mode)
317
            throw new \LogicException("You cannot change autocommit behavior during a transaction");
318
319
        $this->autocommit = $value;
320
    }
321
322
    /**
323
     * Driver Constructor
324
     *
325
     * All modifiable attributes (i.e. with setter method) can be passed as key
326
     *
327
     * @param array $options
328
     */
329
    public function __construct($options)
330
    {
331
        foreach ($options as $option => $value)
332
        {
333
            if (property_exists(__CLASS__, strtolower($option)) && method_exists($this, 'set'.$option))
334
                $this->{'set'.$option}($value);
335
        }
336
    }
337
338
    /**
339
     * Returns true if there is a stablished connection
340
     *
341
     * @return boolean
342
     */
343
    public function isConnected()
344
    {
345
        return (is_resource($this->dbconn) || is_object($this->dbconn));
346
    }
347
348
    /**
349
     * Abstract connect
350
     *
351
     * @throws RuntimeException
352
     *
353
     * @return resource|object
354
     */
355
    public abstract function connect();
356
357
    /**
358
     * Abstract execute
359
     *
360
     * @param string $sql
361
     * @param array $params to bind
362
     *
363
     * @throws RuntimeException
364
     *
365
     * @return resource|object
366
     */
367
    public abstract function execute($sql, Array $params = []);
368
369
    /**
370
     * Reconnects to the database
371
     *
372
     * @throws LogicException
373
     *
374
     * @return resource|object
375
     */
376
    public function reconnect()
377
    {
378
        if (!$this->isConnected())
379
            throw new \LogicException("Connection was not established");
380
381
        $this->disconnect();
382
        return $this->connect();
383
    }
384
385
    /**
386
     * Commit definition
387
     *
388
     * @return boolean
389
     */
390
    public abstract function commit();
391
392
    /**
393
     * Rollback definition
394
     *
395
     * @return boolean
396
     */
397
    public abstract function rollback();
398
399
    /**
400
     * Closes the connection
401
     *
402
     * @throws LogicException
403
     *
404
     * @return boolean
405
     */
406
    public function disconnect()
407
    {
408
        if (!$this->isConnected())
409
            throw new \LogicException("Connection was not established");
410
    }
411
412
    /**
413
     * Defines the start point of a transaction
414
     *
415
     * @throws LogicException if transaction was already started
416
     *
417
     * @return null
418
     */
419
    public function beginTransaction()
420
    {
421
        if (!$this->isConnected())
422
            $this->connect();
423
424
        if ($this->transac_mode)
425
            throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_STARTED]);
426
427
        $this->transac_mode = true;
428
    }
429
430
    /**
431
     * Defines the end point of a transaction
432
     *
433
     * @throws LogicException if transaction has not been started or it's empty
434
     *
435
     * @return null
436
     */
437
    public function endTransaction()
438
    {
439
        if (!$this->transac_mode)
440
            throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_NOT_STARTED]);
441
442
        if (is_null($this->transac_result))
0 ignored issues
show
introduced by
The condition is_null($this->transac_result) is always false.
Loading history...
443
            throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_EMPTY]);
444
445
        if ($this->transac_result)
446
            $this->commit();
447
        else
448
            $this->rollback();
449
450
        $this->result = $this->transac_result;
451
452
        $this->transac_result = null;
453
        $this->transac_mode = false;
454
    }
455
456
    /**
457
     * Abstract result set
458
     *
459
     * By default all Drivers must be implement toArray() function.
460
     * The toArray() method must take the latest result from an execute statement
461
     * and convert it to an array. To get this array getArrayResult() has been implemented.
462
     *
463
     * @throws LogicException if execute() was not executed before this.
464
     *
465
     * @return array
466
     */
467
    protected abstract function toArray();
468
469
    /**
470
     * Excecutes multiple statements as transaction
471
     *
472
     * @param array $querys
473
     *
474
     * @return null
475
     */
476
    public function transaction(Array $querys)
477
    {
478
        $this->beginTransaction();
479
480
        foreach ($querys as $sql)
481
        {
482
            $this->execute($sql);
483
        }
484
485
        $this->endTransaction();
486
    }
487
}