|
1
|
|
|
<?php |
|
2
|
|
|
namespace Darya\Database; |
|
3
|
|
|
|
|
4
|
|
|
use Darya\Database\Error; |
|
5
|
|
|
use Darya\Database\Query; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Darya's database result representation. |
|
9
|
|
|
* |
|
10
|
|
|
* TODO: Make this iterable for result data. Maybe. |
|
11
|
|
|
* |
|
12
|
|
|
* @property array $data Result data |
|
13
|
|
|
* @property Query $query Query that produced this result |
|
14
|
|
|
* @property int $count Result count |
|
15
|
|
|
* @property Error $error Result error |
|
16
|
|
|
* @property array $fields Field names for each result data row |
|
17
|
|
|
* @property int $insertId Insert ID |
|
18
|
|
|
* @property int $affected Rows affected |
|
19
|
|
|
* |
|
20
|
|
|
* @author Chris Andrew <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Result { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The database query that produced this result. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Query |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $query; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Associative array of the result's data. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $data; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Number of rows affected by the query. Only applies to queries that modify |
|
40
|
|
|
* the database. |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $affected; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Number of rows in the result data. |
|
48
|
|
|
* |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $count; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set of fields available for each row in the result. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $fields; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Auto incremented primary key of an inserted row. |
|
62
|
|
|
* |
|
63
|
|
|
* @var int |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $insertId; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Error captured from the query that produced this result. |
|
69
|
|
|
* |
|
70
|
|
|
* @var Error |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $error; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Convert a string from snake_case to camelCase. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $string |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected static function snakeToCamel($string) { |
|
81
|
|
|
return preg_replace_callback('/_(.)/', function($matches) { |
|
82
|
|
|
return strtoupper($matches[1]); |
|
83
|
|
|
}, $string); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Instantiate a new database result. |
|
88
|
|
|
* |
|
89
|
|
|
* $info accepts the keys 'affected', 'count', 'insert_id' and 'fields'. |
|
90
|
|
|
* |
|
91
|
|
|
* @param Query $query |
|
92
|
|
|
* @param array $data [optional] |
|
93
|
|
|
* @param array $info [optional] |
|
94
|
|
|
* @param Error $error [optional] |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __construct(Query $query, array $data = array(), array $info = array(), Error $error = null) { |
|
97
|
|
|
$this->data = $data; |
|
98
|
|
|
$this->error = $error; |
|
99
|
|
|
$this->query = $query; |
|
100
|
|
|
|
|
101
|
|
|
$this->setInfo($info); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the result info. |
|
106
|
|
|
* |
|
107
|
|
|
* Accepts the keys 'affected', 'count', 'insert_id' and 'fields'. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $info |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
protected function setInfo(array $info) { |
|
|
|
|
|
|
112
|
|
|
$defaults = array( |
|
113
|
|
|
'count' => 0, |
|
114
|
|
|
'fields' => array(), |
|
115
|
|
|
'affected' => 0, |
|
116
|
|
|
'insert_id' => 0 |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($defaults as $key => $default) { |
|
120
|
|
|
$property = static::snakeToCamel($key); |
|
121
|
|
|
$this->$property = isset($info[$key]) ? $info[$key] : $default; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Dynamically retrieve the given property. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $property |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function __get($property) { |
|
132
|
|
|
return $this->$property; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
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.