Passed
Push — master ( 566a3a...fbef46 )
by 世昌
02:19
created

ReadStatement::havingStringArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\database\statement;
3
4
use function func_get_args;
5
use function func_num_args;
6
use function is_array;
7
use suda\database\struct\TableStruct;
8
use suda\database\middleware\Middleware;
9
use suda\database\exception\SQLException;
10
11
class ReadStatement extends QueryStatement
12
{
13
    use PrepareTrait;
14
15
    /**
16
     * 数据表结构
17
     *
18
     * @var TableStruct
19
     */
20
    protected $struct;
21
22
    /**
23
     * 数据原始表名
24
     *
25
     * @var string
26
     */
27
    protected $table;
28
29
    protected $distinct = '';
30
31
    protected $select = '*';
32
33
    protected $where = '';
34
35
    protected $groupBy = '';
36
37
    protected $having = '';
38
39
    protected $orderBy = '';
40
41
    protected $limit = '';
42
43
    /**
44
     * 创建写
45
     *
46
     * @param string $rawTableName
47
     * @param TableStruct $struct
48
     * @param Middleware $middleware
49
     */
50
    public function __construct(string $rawTableName, TableStruct $struct, Middleware $middleware)
51
    {
52
        parent::__construct('');
53
        $this->struct = $struct;
54
        $this->table = $rawTableName;
55
        $this->type = self::READ;
56
        $this->fetch = self::FETCH_ONE;
57
        $this->middleware = $middleware;
58
    }
59
60
    /**
61
     * 单独去重复
62
     *
63
     * @return $this
64
     */
65
    public function distinct()
66
    {
67
        $this->distinct = 'DISTINCT';
68
        return $this;
69
    }
70
71
    /**
72
     * 查询的列
73
     *
74
     * @param array|string $fields
75
     * @return $this
76
     */
77
    public function read($fields)
78
    {
79
        if (func_num_args() > 1) {
80
            $fields = func_get_args();
81
        }
82
        if (is_array($fields)) {
83
            foreach ($fields as $index => $name) {
84
                $fields[$index] = $this->middleware->inputName($name);
85
            }
86
        }
87
        $this->select = $this->prepareReadFields($fields);
88
        return $this;
89
    }
90
91
    /**
92
     * 条件
93
     * @param $where
94
     * @param mixed ...$args
95
     * @return $this
96
     * @throws SQLException
97
     */
98
    public function where($where, ...$args)
99
    {
100
        if (is_array($where)) {
101
            $where = $this->aliasKeyField($where);
102
            $this->whereArray($where, $args[0] ?? []);
103
        } elseif (is_array($args[0])) {
104
            $this->whereStringArray($where, $args[0]);
105
        } else {
106
            list($string, $array) = $this->prepareQueryMark($where, $args);
107
            $this->whereStringArray($string, $array);
108
        }
109
        return $this;
110
    }
111
112
    /**
113
     * 处理输入的键
114
     *
115
     * @param array $fields
116
     * @return array
117
     */
118
    protected function aliasKeyField(array $fields)
119
    {
120
        $values = [];
121
        foreach ($fields as $name => $value) {
122
            $index = $this->middleware->inputName($name);
123
            $values[$index] = $value;
124
        }
125
        return $values;
126
    }
127
128
    /**
129
     * @param array $where
130
     * @param array $binders
131
     * @throws SQLException
132
     */
133
    protected function whereArray(array $where, array $binders)
134
    {
135
        list($where, $whereBinder) = $this->parepareWhere($where);
136
        $this->whereStringArray($where, array_merge($whereBinder, $binders));
137
    }
138
139
    /**
140
     * @param string $where
141
     * @param array $whereBinder
142
     * @throws SQLException
143
     */
144
    protected function whereStringArray(string $where, array $whereBinder)
145
    {
146
        list($where, $whereBinder) = $this->prepareWhereString($where, $whereBinder);
147
        $this->where = 'WHERE '. $where;
148
        $this->binder = $this->mergeBinder($this->binder, $whereBinder);
149
    }
150
151
    /**
152
     * 分组
153
     *
154
     * @param string $what
155
     * @return $this
156
     */
157
    public function groupBy(string $what)
158
    {
159
        $this->groupBy = 'GROUP BY '. $what;
160
        return $this;
161
    }
162
163
    /**
164
     * 含
165
     *
166
     * @param string|array $what
167
     * @param array $args
168
     * @return $this
169
     * @throws SQLException
170
     */
171
    public function having($what, ...$args)
172
    {
173
        if (is_array($what)) {
174
            $this->havingArray($what);
175
        } elseif (is_array($args[0])) {
176
            $this->havingStringArray($what, $args[0]);
177
        } else {
178
            list($string, $array) = $this->prepareQueryMark($what, $args);
179
            $this->havingStringArray($string, $array);
180
        }
181
        return $this;
182
    }
183
184
    /**
185
     * @param array $want
186
     * @throws SQLException
187
     */
188
    protected function havingArray(array $want)
189
    {
190
        list($having, $havingBinder) = $this->parepareWhere($want);
191
        $this->havingStringArray($having, $havingBinder);
192
    }
193
194
    /**
195
     * @param string $having
196
     * @param array $havingBinder
197
     * @throws SQLException
198
     */
199
    protected function havingStringArray(string $having, array $havingBinder)
200
    {
201
202
        list($having, $havingBinder) = $this->prepareWhereString($having, $havingBinder);
203
        $this->having = 'HAVING '. $having;
204
        $this->binder = $this->mergeBinder($this->binder, $havingBinder);
205
    }
206
207
    /**
208
     * 排序
209
     *
210
     * @param string $what
211
     * @param string $order
212
     * @return $this
213
     */
214
    public function orderBy(string $what, string $order = 'ASC')
215
    {
216
        $this->orderBy = 'ORDER BY '. $what.' '. $order;
217
        return $this;
218
    }
219
220
    /**
221
     * 限制
222
     *
223
     * @param int $start
224
     * @param integer $length
225
     * @return $this
226
     */
227
    public function limit(int $start, int $length = null)
228
    {
229
        $this->limit = 'LIMIT '. $start . ($length !== null ? ',' .$length :'');
230
        return $this;
231
    }
232
233
    /**
234
     * 分页
235
     *
236
     * @param integer $page
237
     * @param integer $length
238
     * @return $this
239
     */
240
    public function page(int $page, int $length)
241
    {
242
        if ($page <= 0) {
243
            $page = 1;
244
        }
245
        $this->limit(($page - 1) * $length, $length);
246
        return $this;
247
    }
248
249
    /**
250
     * 获取字符串
251
     *
252
     * @return Query
253
     */
254
    protected function prepareQuery():Query
255
    {
256
        $where = [$this->where,$this->groupBy,$this->having,$this->orderBy,$this->limit];
257
        $condition = implode(' ', array_filter(array_map('trim', $where), 'strlen'));
258
        $select = [$this->distinct,$this->select];
259
        $selection = implode(' ', array_filter(array_map('trim', $select), 'strlen'));
260
        $string = sprintf("SELECT %s FROM %s %s", $selection, $this->table, $condition);
261
        return new Query($string, $this->binder);
262
    }
263
}
264