1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\DataDb; |
4
|
|
|
|
5
|
|
|
use Graze\DataDb\Adapter\AdapterInterface; |
6
|
|
|
use Traversable; |
7
|
|
|
|
8
|
|
|
class QueryNode implements QueryNodeInterface |
9
|
|
|
{ |
10
|
|
|
/** @var AdapterInterface */ |
11
|
|
|
private $adapter; |
12
|
|
|
/** @var string */ |
13
|
|
|
private $sql; |
14
|
|
|
/** @var array */ |
15
|
|
|
private $bind = []; |
16
|
|
|
/** @var string[] */ |
17
|
|
|
private $columns = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* QueryNode constructor. |
21
|
|
|
* |
22
|
|
|
* @param AdapterInterface $adapter |
23
|
|
|
* @param string $sql |
24
|
|
|
* @param array $bind |
25
|
|
|
*/ |
26
|
|
|
public function __construct(AdapterInterface $adapter, $sql, array $bind = []) |
27
|
|
|
{ |
28
|
|
|
$this->adapter = $adapter; |
29
|
|
|
$this->sql = $sql; |
30
|
|
|
$this->bind = $bind; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
#region Properties |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return AdapterInterface |
37
|
|
|
*/ |
38
|
|
|
public function getAdapter() |
39
|
|
|
{ |
40
|
|
|
return $this->adapter; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param AdapterInterface $adapter |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public function setAdapter(AdapterInterface $adapter) |
49
|
|
|
{ |
50
|
|
|
$this->adapter = $adapter; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function getSql() |
58
|
|
|
{ |
59
|
|
|
return $this->sql; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $sql |
64
|
|
|
* |
65
|
|
|
* @return static |
66
|
|
|
*/ |
67
|
|
|
public function setSql($sql) |
68
|
|
|
{ |
69
|
|
|
$this->sql = $sql; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getBind() |
77
|
|
|
{ |
78
|
|
|
return $this->bind; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $bind |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
|
|
public function setBind(array $bind) |
87
|
|
|
{ |
88
|
|
|
$this->bind = $bind; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string[] |
94
|
|
|
*/ |
95
|
|
|
public function getColumns() |
96
|
|
|
{ |
97
|
|
|
return $this->columns; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string[] $columns |
102
|
|
|
* |
103
|
|
|
* @return static |
104
|
|
|
*/ |
105
|
|
|
public function setColumns(array $columns) |
106
|
|
|
{ |
107
|
|
|
$this->columns = $columns; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
#endregion |
112
|
|
|
|
113
|
|
|
#region Query |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function query() |
119
|
|
|
{ |
120
|
|
|
return $this->adapter->query($this->sql, $this->bind); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return Traversable |
125
|
|
|
*/ |
126
|
|
|
public function fetch() |
127
|
|
|
{ |
128
|
|
|
return $this->adapter->fetch($this->sql, $this->bind); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function fetchAll() |
135
|
|
|
{ |
136
|
|
|
return $this->adapter->fetchAll($this->sql, $this->bind); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function fetchRow() |
143
|
|
|
{ |
144
|
|
|
return $this->adapter->fetchRow($this->sql, $this->bind); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function fetchOne() |
151
|
|
|
{ |
152
|
|
|
return $this->adapter->fetchOne($this->sql, $this->bind); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
#endregion |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function __toString() |
161
|
|
|
{ |
162
|
|
|
return 'Query: ' . substr($this->sql, 0, 18) . '...'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return a clone of this object |
167
|
|
|
* |
168
|
|
|
* @return static |
169
|
|
|
*/ |
170
|
|
|
public function getClone() |
171
|
|
|
{ |
172
|
|
|
return clone $this; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|