Passed
Push — sudav3 ( 36433a...9b090b )
by 世昌
02:24
created

ReadStatement::page()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\orm\statement;
3
4
use suda\orm\TableStruct;
5
use suda\orm\statement\Query;
6
use suda\orm\statement\Statement;
7
use suda\orm\middleware\Middleware;
8
use suda\orm\exception\SQLException;
9
use suda\orm\statement\PrepareTrait;
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
     */
49
    public function __construct(string $rawTableName, TableStruct $struct, Middleware $middleware)
50
    {
51
        $this->struct = $struct;
52
        $this->table = $rawTableName;
53
        $this->type = self::READ;
54
        $this->fetch = self::FETCH_ONE;
55
        $this->middleware = $middleware;
0 ignored issues
show
Documentation Bug introduced by
It seems like $middleware of type suda\orm\middleware\Middleware is incompatible with the declared type suda\orm\statement\Middleware of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
    }
57
58
    /**
59
     * 单独去重复
60
     *
61
     * @return self
62
     */
63
    public function distinct()
64
    {
65
        $this->distinct = 'DISTINCT';
66
        return $this;
67
    }
68
69
    /**
70
     * 查询的列
71
     *
72
     * @param array|string $fields
73
     * @return self
74
     */
75
    public function read($fields)
76
    {
77
        if (\func_num_args() > 1) {
78
            $fields = \func_get_args();
79
        }
80
        if (is_array($fields)) {
81
            foreach ($fields as $index => $name) {
82
                $fields[$index] = $this->middleware->inputName($name);
83
            }
84
        }
85
        $this->select = $this->prepareReadFields($fields);
86
        return $this;
87
    }
88
89
    /**
90
     * 条件
91
     *
92
     * @param string|array $where
93
     * @param array $whereBinder
94
     * @return self
95
     */
96
    public function where($where, ...$args)
97
    {
98
        if (\is_array($where)) {
99
            $where = $this->aliasKeyField($where);
100
            $this->whereArray($where, $args[0] ?? []);
101
        } elseif (is_array($args[0])) {
102
            $this->whereStringArray($where, $args[0]);
103
        } else {
104
            list($string, $array) = $this->prepareQueryMark($where, $args);
105
            $this->whereStringArray($string, $array);
106
        }
107
        return $this;
108
    }
109
110
    /**
111
     * 处理输入的键
112
     *
113
     * @param array $fields
114
     * @return array
115
     */
116
    protected function aliasKeyField(array $fields)
117
    {
118
        $values = [];
119
        foreach ($fields as $name => $value) {
120
            $index = $this->middleware->inputName($name);
121
            $values[$index] = $value;
122
        }
123
        return $values;
124
    }
125
126
    protected function whereArray(array $where, array $binders)
127
    {
128
        list($where, $whereBinder) = $this->parepareWhere($where);
129
        $this->whereStringArray($where, array_merge($whereBinder, $binders));
130
    }
131
132
    protected function whereStringArray(string $where, array $whereBinder)
133
    {
134
        list($where, $whereBinder) = $this->parepareWhereString($where, $whereBinder);
135
        $this->where = 'WHERE '. $where;
136
        $this->binder = $this->mergeBinder($this->binder, $whereBinder);
137
    }
138
139
    /**
140
     * 分组
141
     *
142
     * @param string $what
143
     * @return self
144
     */
145
    public function groupBy(string $what)
146
    {
147
        $this->groupBy = 'GROUP BY '. $what;
148
        return $this;
149
    }
150
151
    /**
152
     * 含
153
     *
154
     * @param string|array $what
155
     * @param array $whereBinder
156
     * @return self
157
     */
158
    public function having($what, ...$args)
159
    {
160
        if (\is_array($what)) {
161
            $this->havingArray($what);
162
        } elseif (is_array($args[0])) {
163
            $this->havingStringArray($what, $args[0]);
164
        } else {
165
            list($string, $array) = $this->prepareQueryMark($what, $args);
166
            $this->havingStringArray($string, $array);
167
        }
168
        return $this;
169
    }
170
171
    protected function havingArray(array $want)
172
    {
173
        list($having, $havingBinder) = $this->parepareWhere($want);
174
        $this->havingStringArray($having, $havingBinder);
175
    }
176
177
    protected function havingStringArray(string $having, array $havingBinder)
178
    {
179
        
180
        list($having, $havingBinder) = $this->parepareWhereString($having, $havingBinder);
181
        $this->having = 'HAVING '. $having;
182
        $this->binder = $this->mergeBinder($this->binder, $havingBinder);
183
    }
184
185
    /**
186
     * 排序
187
     *
188
     * @param string $what
189
     * @param string $order
190
     * @return self
191
     */
192
    public function orderBy(string $what, string $order = 'ASC')
193
    {
194
        $this->orderBy = 'ORDER BY '. $what.' '. $order;
195
        return $this;
196
    }
197
198
    /**
199
     * 限制
200
     *
201
     * @param int $start
202
     * @param integer $length
203
     * @return self
204
     */
205
    public function limit(int $start, int $length = null)
206
    {
207
        $this->limit = 'LIMIT '. $start . ($length !== null ? ',' .$length :'');
208
        return $this;
209
    }
210
211
    /**
212
     * 分页
213
     *
214
     * @param integer $page
215
     * @param integer $length
216
     * @return self
217
     */
218
    public function page(int $page, int $length)
219
    {
220
        if ($page <= 0) {
221
            $page = 1;
222
        }
223
        $this->limit(($page - 1) * $length, $length);
224
        return $this;
225
    }
226
227
    /**
228
     * 获取字符串
229
     *
230
     * @return Query
231
     */
232
    protected function prepareQuery():Query
233
    {
234
        $where = [$this->where,$this->groupBy,$this->having,$this->orderBy,$this->limit];
235
        $condition = implode(' ', array_filter(array_map('trim', $where), 'strlen'));
236
        $select = [$this->distinct,$this->select];
237
        $selection = implode(' ', array_filter(array_map('trim', $select), 'strlen'));
238
        $string = "SELECT {$selection} FROM {$this->table} {$condition}";
239
        return new Query($string, $this->binder);
240
    }
241
}
242