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