Completed
Branch master (260b19)
by smiley
02:54
created

DBDriverAbstract::getStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @filesource   DBDriverAbstract.php
4
 * @created      04.11.2015
5
 * @package      chillerlan\Database\Drivers
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2015 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\Database\Drivers;
12
13
use chillerlan\Database\DBOptions;
14
use chillerlan\Database\Querystats;
15
16
/**
17
 * Class DBDriverAbstract
18
 *
19
 * Implements common methods of DBDriverInterface - this class shall only be extended, never instanced
20
 *
21
 * @see http://blog.mclaughlinsoftware.com/2010/02/21/php-binding-a-wildcard/ LIKE %...% -> LIKE CONCAT('%',?,'%')
22
 */
23
abstract class DBDriverAbstract implements DBDriverInterface{
24
25
	/**
26
	 * Holds the database resource object
27
	 *
28
	 * @var resource
29
	 */
30
	protected $db;
31
32
	/**
33
	 * Holds the settings
34
	 *
35
	 * @var \chillerlan\Database\DBOptions
36
	 */
37
	protected $options;
38
39
	/**
40
	 * Holds an array of Querystats objects
41
	 *
42
	 * @var array
43
	 */
44
	protected $stats = [];
45
46
	/**
47
	 * Indicates wether query stats are being recorded or not.
48
	 * false by default
49
	 *
50
	 * @var bool
51
	 */
52
	protected $stats_enabled = false;
53
54
	/**
55
	 * Constructor.
56
	 *
57
	 * @param \chillerlan\Database\DBOptions $options
58
	 */
59
	public function __construct(DBOptions $options){
60
		$this->options = $options;
61
	}
62
63
	/**
64
	 * Returns the plain connection object
65
	 *
66
	 * @return resource the database resource object
67
	 */
68
	public function getDBResource(){
69
		return $this->db;
70
	}
71
72
	/**
73
	 * Returns the stats array
74
	 *
75
	 * @return array stats
76
	 */
77
	public function getStats(){
78
		return $this->stats;
79
	}
80
81
	/**
82
	 * Enables query stat recording
83
	 *
84
	 * @return $this
85
	 */
86
	public function enableStats(){
87
		$this->stats_enabled = true;
88
89
		return $this;
90
	}
91
92
	/**
93
	 * Disables  query stat recording
94
	 *
95
	 * @return $this
96
	 */
97
	public function disableStats(){
98
		$this->stats_enabled = false;
99
100
		return $this;
101
	}
102
103
	/**
104
	 * Clears the stats array
105
	 *
106
	 * @return $this
107
	 */
108
	public function clearStats(){
109
		$this->stats = [];
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Adds a new Querystats object to self::$_stats
116
	 *
117
	 * @param array $stats
118
	 */
119
	public function addStats(array $stats){
120
		if($this->stats_enabled){
121
			$stat = new Querystats;
122
123
			foreach($stat as $key => $value){
0 ignored issues
show
Bug introduced by
The expression $stat of type object<chillerlan\Database\Querystats> is not traversable.
Loading history...
124
				if(isset($stats[$key])){
125
					$stat->{$key} = $stats[$key];
126
				}
127
			}
128
129
			$this->stats[] = $stat;
130
		}
131
	}
132
133
}
134