1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\database\struct; |
4
|
|
|
|
5
|
|
|
use suda\database\exception\SQLException; |
6
|
|
|
use suda\database\TableAccess; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class ReadStatement extends \suda\database\statement\ReadStatement |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* 访问操作 |
13
|
|
|
* |
14
|
|
|
* @var TableAccess |
15
|
|
|
*/ |
16
|
|
|
protected $access; |
17
|
|
|
|
18
|
|
|
public function __construct(TableAccess $access) |
19
|
|
|
{ |
20
|
|
|
$this->access = $access; |
21
|
|
|
parent::__construct( |
22
|
|
|
$access->getStruct()->getRealTableName($access->getSource()->write()), |
23
|
|
|
$access->getStruct(), |
24
|
|
|
$access->getMiddleware() |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 取1 |
30
|
|
|
* |
31
|
|
|
* @param string|null $class |
32
|
|
|
* @param array $args |
33
|
|
|
* @return mixed |
34
|
|
|
* @throws SQLException |
35
|
|
|
*/ |
36
|
|
|
public function one(?string $class = null, array $args = []) |
37
|
|
|
{ |
38
|
|
|
if ($this->isScroll() === false && $this->hasLimit() === false) { |
39
|
|
|
$this->limit(0, 1); |
40
|
|
|
} |
41
|
|
|
return $this->access->run($this->wantOne($class, $args)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 取一列 |
46
|
|
|
* @param string $name |
47
|
|
|
* @param mixed $default |
48
|
|
|
* @return mixed |
49
|
|
|
* @throws SQLException |
50
|
|
|
*/ |
51
|
|
|
public function field(string $name, $default = null) |
52
|
|
|
{ |
53
|
|
|
$row = $this->one(); |
54
|
|
|
return $row[$name] ?? $default; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 取数组的一列 |
59
|
|
|
* @param string $name |
60
|
|
|
* @return array |
61
|
|
|
* @throws SQLException |
62
|
|
|
*/ |
63
|
|
|
public function allField(string $name) |
64
|
|
|
{ |
65
|
|
|
$row = $this->all(); |
66
|
|
|
return array_column($row, $name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
private function hasLimit() |
73
|
|
|
{ |
74
|
|
|
return strlen($this->limit) > 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 取全部 |
79
|
|
|
* |
80
|
|
|
* @param string|null $class |
81
|
|
|
* @param array $args |
82
|
|
|
* @return array |
83
|
|
|
* @throws SQLException |
84
|
|
|
*/ |
85
|
|
|
public function all(?string $class = null, array $args = []): array |
86
|
|
|
{ |
87
|
|
|
return $this->access->run($this->wantAll($class, $args)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 取1 |
92
|
|
|
* |
93
|
|
|
* @param string|null $class |
94
|
|
|
* @param array $args |
95
|
|
|
* @return mixed |
96
|
|
|
* @throws SQLException |
97
|
|
|
*/ |
98
|
|
|
public function fetch(?string $class = null, array $args = []) |
99
|
|
|
{ |
100
|
|
|
return $this->one($class, $args); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* 取全部 |
105
|
|
|
* |
106
|
|
|
* @param string|null $class |
107
|
|
|
* @param array $args |
108
|
|
|
* @return array |
109
|
|
|
* @throws SQLException |
110
|
|
|
*/ |
111
|
|
|
public function fetchAll(?string $class = null, array $args = []): array |
112
|
|
|
{ |
113
|
|
|
return $this->all($class, $args); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 统计 |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
* @throws SQLException |
121
|
|
|
*/ |
122
|
|
|
public function count() { |
123
|
|
|
$query = clone $this; |
124
|
|
|
$query->orderBy = ''; |
125
|
|
|
$query->limit = ''; |
126
|
|
|
$fields = $this->struct->all(); |
127
|
|
|
if (count($fields) > 0) { |
128
|
|
|
$field = \array_shift($fields); |
129
|
|
|
$query->read([$field->getName()]); |
130
|
|
|
} |
131
|
|
|
$totalQuery = new QueryStatement($this->getAccess(), sprintf("SELECT count(*) as count from (%s) as total", $query->getString()), |
132
|
|
|
$this->getBinder()); |
133
|
|
|
$totalQuery->wantType(null); |
134
|
|
|
$data = $totalQuery->one(); |
135
|
|
|
return intval($data['count']); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get 访问操作 |
140
|
|
|
* |
141
|
|
|
* @return TableAccess |
142
|
|
|
*/ |
143
|
|
|
public function getAccess(): TableAccess |
144
|
|
|
{ |
145
|
|
|
return $this->access; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|