1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\database\struct; |
4
|
|
|
|
5
|
|
|
use suda\database\exception\SQLException; |
6
|
|
|
use suda\database\TableAccess; |
7
|
|
|
|
8
|
|
|
class ReadStatement extends \suda\database\statement\ReadStatement |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* 访问操作 |
12
|
|
|
* |
13
|
|
|
* @var TableAccess |
14
|
|
|
*/ |
15
|
|
|
protected $access; |
16
|
|
|
|
17
|
|
|
public function __construct(TableAccess $access) |
18
|
|
|
{ |
19
|
|
|
$this->access = $access; |
20
|
|
|
parent::__construct( |
21
|
|
|
$access->getSource()->write()->rawTableName($access->getStruct()->getName()), |
22
|
|
|
$access->getStruct(), |
23
|
|
|
$access->getMiddleware() |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 取1 |
29
|
|
|
* |
30
|
|
|
* @param string|null $class |
31
|
|
|
* @param array $args |
32
|
|
|
* @return mixed |
33
|
|
|
* @throws SQLException |
34
|
|
|
*/ |
35
|
|
|
public function one(?string $class = null, array $args = []) |
36
|
|
|
{ |
37
|
|
|
if ($this->isScroll() === false && $this->hasLimit() === false) { |
38
|
|
|
$this->limit(0, 1); |
39
|
|
|
} |
40
|
|
|
return $this->access->run($this->wantOne($class, $args)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 取一列 |
45
|
|
|
* @param string $name |
46
|
|
|
* @param mixed $default |
47
|
|
|
* @return mixed |
48
|
|
|
* @throws SQLException |
49
|
|
|
*/ |
50
|
|
|
public function field(string $name, $default = null) { |
51
|
|
|
$row = $this->one(); |
52
|
|
|
return $row[$name] ?? $default; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 取数组的一列 |
57
|
|
|
* @param string $name |
58
|
|
|
* @return array |
59
|
|
|
* @throws SQLException |
60
|
|
|
*/ |
61
|
|
|
public function allField(string $name) { |
62
|
|
|
$row = $this->all(); |
63
|
|
|
return array_column($row, $name); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
private function hasLimit() |
70
|
|
|
{ |
71
|
|
|
return strlen($this->limit) > 0; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* 取全部 |
76
|
|
|
* |
77
|
|
|
* @param string|null $class |
78
|
|
|
* @param array $args |
79
|
|
|
* @return array |
80
|
|
|
* @throws SQLException |
81
|
|
|
*/ |
82
|
|
|
public function all(?string $class = null, array $args = []): array |
83
|
|
|
{ |
84
|
|
|
return $this->access->run($this->wantAll($class, $args)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* 取1 |
89
|
|
|
* |
90
|
|
|
* @param string|null $class |
91
|
|
|
* @param array $args |
92
|
|
|
* @return mixed |
93
|
|
|
* @throws SQLException |
94
|
|
|
*/ |
95
|
|
|
public function fetch(?string $class = null, array $args = []) |
96
|
|
|
{ |
97
|
|
|
return $this->one($class, $args); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* 取全部 |
102
|
|
|
* |
103
|
|
|
* @param string|null $class |
104
|
|
|
* @param array $args |
105
|
|
|
* @return array |
106
|
|
|
* @throws SQLException |
107
|
|
|
*/ |
108
|
|
|
public function fetchAll(?string $class = null, array $args = []): array |
109
|
|
|
{ |
110
|
|
|
return $this->all($class, $args); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get 访问操作 |
115
|
|
|
* |
116
|
|
|
* @return TableAccess |
117
|
|
|
*/ |
118
|
|
|
public function getAccess(): TableAccess |
119
|
|
|
{ |
120
|
|
|
return $this->access; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|