1
|
|
|
<?php |
2
|
|
|
namespace suda\database\struct; |
3
|
|
|
|
4
|
|
|
use suda\database\exception\SQLException; |
5
|
|
|
use suda\database\statement\QueryAccess; |
6
|
|
|
|
7
|
|
|
class QueryStatement extends \suda\database\statement\QueryStatement |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* 访问操作 |
11
|
|
|
* |
12
|
|
|
* @var QueryAccess |
13
|
|
|
*/ |
14
|
|
|
protected $access; |
15
|
|
|
|
16
|
|
|
public function __construct(QueryAccess $access, string $query, ...$parameter) |
17
|
|
|
{ |
18
|
|
|
$this->access = $access; |
19
|
|
|
parent::__construct($query, ...$parameter); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 取1 |
24
|
|
|
* |
25
|
|
|
* @param string|null $class |
26
|
|
|
* @param array $args |
27
|
|
|
* @return mixed |
28
|
|
|
* @throws SQLException |
29
|
|
|
*/ |
30
|
|
|
public function one(?string $class = null, array $args = []) |
31
|
|
|
{ |
32
|
|
|
$this->setType(static::READ); |
33
|
|
|
$value = $this->access->run($this->wantOne($class, $args)); |
34
|
|
|
if (is_array($value)) { |
35
|
|
|
return $value; |
36
|
|
|
} |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 取全部 |
42
|
|
|
* |
43
|
|
|
* @param string|null $class |
44
|
|
|
* @param array $args |
45
|
|
|
* @return array |
46
|
|
|
* @throws SQLException |
47
|
|
|
*/ |
48
|
|
|
public function all(?string $class = null, array $args = []):array |
49
|
|
|
{ |
50
|
|
|
$this->setType(static::READ); |
51
|
|
|
return $this->access->run($this->wantAll($class, $args)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 取一列 |
56
|
|
|
* @param string $name |
57
|
|
|
* @param mixed $default |
58
|
|
|
* @return mixed |
59
|
|
|
* @throws SQLException |
60
|
|
|
*/ |
61
|
|
|
public function field(string $name, $default = null) |
62
|
|
|
{ |
63
|
|
|
$row = $this->one(); |
64
|
|
|
return $row[$name] ?? $default; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 取数组的一列 |
69
|
|
|
* @param string $name |
70
|
|
|
* @return array |
71
|
|
|
* @throws SQLException |
72
|
|
|
*/ |
73
|
|
|
public function allField(string $name) |
74
|
|
|
{ |
75
|
|
|
$row = $this->all(); |
76
|
|
|
return array_column($row, $name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 取1 |
81
|
|
|
* |
82
|
|
|
* @param string|null $class |
83
|
|
|
* @param array $args |
84
|
|
|
* @return mixed |
85
|
|
|
* @throws SQLException |
86
|
|
|
*/ |
87
|
|
|
public function fetch(?string $class = null, array $args = []) |
88
|
|
|
{ |
89
|
|
|
return $this->one($class, $args); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 取全部 |
94
|
|
|
* |
95
|
|
|
* @param string|null $class |
96
|
|
|
* @param array $args |
97
|
|
|
* @return array |
98
|
|
|
* @throws SQLException |
99
|
|
|
*/ |
100
|
|
|
public function fetchAll(?string $class = null, array $args = []):array |
101
|
|
|
{ |
102
|
|
|
return $this->all($class, $args); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 返回影响行数 |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
* @throws SQLException |
110
|
|
|
*/ |
111
|
|
|
public function rows():int |
112
|
|
|
{ |
113
|
|
|
$this->returnType = WriteStatement::RET_ROWS; |
114
|
|
|
$this->setType(static::WRITE); |
115
|
|
|
return $this->access->run($this); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* 返回是否成功 |
120
|
|
|
* |
121
|
|
|
* @return boolean |
122
|
|
|
* @throws SQLException |
123
|
|
|
*/ |
124
|
|
|
public function ok():bool |
125
|
|
|
{ |
126
|
|
|
$this->returnType = WriteStatement::RET_BOOL; |
127
|
|
|
$this->setType(static::WRITE); |
128
|
|
|
return $this->access->run($this); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* 返回ID |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
* @throws SQLException |
136
|
|
|
*/ |
137
|
|
|
public function id():string |
138
|
|
|
{ |
139
|
|
|
$this->returnType = WriteStatement::RET_LAST_INSERT_ID; |
140
|
|
|
$this->setType(static::WRITE); |
141
|
|
|
return $this->access->run($this); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 统计 |
146
|
|
|
* |
147
|
|
|
* @return int |
148
|
|
|
* @throws SQLException |
149
|
|
|
*/ |
150
|
|
|
public function count() { |
151
|
|
|
$query = $this->getString(); |
152
|
|
|
$query = preg_replace('/LIMIT\s+\d+(\s*,\s*\d+)?\s*$/ims', '', $query); |
153
|
|
|
$totalQuery = new QueryStatement($this->getAccess(), sprintf("SELECT count(*) as count from (%s) as total", $query), |
154
|
|
|
$this->getBinder()); |
155
|
|
|
$totalQuery->wantType(null); |
156
|
|
|
$data = $totalQuery->one(); |
157
|
|
|
return intval($data['count']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get 访问操作 |
162
|
|
|
* |
163
|
|
|
* @return QueryAccess |
164
|
|
|
*/ |
165
|
|
|
public function getAccess():QueryAccess |
166
|
|
|
{ |
167
|
|
|
return $this->access; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|