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

Statement::isWrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace suda\orm\statement;
3
4
use PDOStatement;
5
use suda\orm\Binder;
6
use suda\orm\statement\Query;
7
use suda\orm\statement\PrepareTrait;
8
9
abstract class Statement
10
{
11
    use PrepareTrait;
12
    
13
    const WRITE = 0;
14
    const READ = 1;
15
16
    const FETCH_ONE = 0;
17
    const FETCH_ALL = 1;
18
19
    /**
20
     * 语句类型
21
     *
22
     * @var int|null
23
     */
24
    protected $type = null;
25
26
    /**
27
     * 获取类型
28
     *
29
     * @var int
30
     */
31
    protected $fetch = null;
32
33
    /**
34
     * 滚动
35
     *
36
     * @var boolean|null
37
     */
38
    protected $scroll = null;
39
40
    /**
41
     * 绑定
42
     *
43
     * @var Binder[]
44
     */
45
    protected $binder = [];
46
47
    /**
48
     * SQL语句
49
     *
50
     * @var string
51
     */
52
    protected $string;
53
54
    /**
55
     * PDOStatement
56
     *
57
     * @var PDOStatement|null
58
     */
59
    protected $statement = null;
60
61
    /**
62
     * Query
63
     *
64
     * @var Query
65
     */
66
    protected $query;
67
68
    const RET_ROWS = 1;
69
    const RET_LAST_INSERT_ID = 2;
70
    const RET_BOOL = 3;
71
    /**
72
     * 返回类型
73
     *
74
     * @var int
75
     */
76
    protected $returnType = Statement::RET_BOOL;
77
78
    /**
79
     * 类
80
     *
81
     * @var string
82
     */
83
    protected $fetchClass;
84
85
    /**
86
     * 参数
87
     *
88
     * @var array
89
     */
90
    protected $fetchClassArgs = [];
91
92
    /**
93
     * 数据处理中间件
94
     *
95
     * @var Middleware
0 ignored issues
show
Bug introduced by
The type suda\orm\statement\Middleware was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
96
     */
97
    protected $middleware;
98
99
    public function __construct(string $sql, ...$args)
100
    {
101
        if (count($args) === 1 && \is_array($args[0])) {
102
            $this->create($sql, $args[0]);
103
        } else {
104
            list($this->string, $this->binder) = $this->prepareQueryMark($sql, $args);
105
        }
106
    }
107
    
108
    protected function create(string $sql, array $parameter)
109
    {
110
        $this->string = $sql;
111
        $this->binder = $this->mergeBinder($this->binder, $parameter);
112
    }
113
114
    public function isRead(bool $set = null):bool
115
    {
116
        if ($set !== null) {
117
            $this->type = self::READ;
118
        }
119
        return $this->type === self::READ;
120
    }
121
122
    public function isWrite(bool $set = null):bool
123
    {
124
        if ($set !== null) {
125
            $this->type = self::WRITE;
126
        }
127
        return $this->type === self::WRITE;
128
    }
129
130
    /**
131
     * 判断是否为一条
132
     *
133
     * @return boolean
134
     */
135
    public function isFetchOne(bool $set = null):bool
136
    {
137
        if ($set !== null) {
138
            $this->fetch = self::FETCH_ONE;
139
        }
140
        return $this->fetch === self::FETCH_ONE;
141
    }
142
143
    /**
144
     * 判断是否为一条
145
     *
146
     * @return boolean
147
     */
148
    public function isFetch():bool
149
    {
150
        return $this->fetch !== null;
151
    }
152
153
    /**
154
     * 判断是否获取多条
155
     *
156
     * @return boolean
157
     */
158
    public function isFetchAll(bool $set = null):bool
159
    {
160
        if ($set !== null) {
161
            $this->fetch = self::FETCH_ALL;
162
        }
163
        return $this->fetch === self::FETCH_ALL;
164
    }
165
    
166
    /**
167
     * 设置记录类
168
     *
169
     * @param string|null $class
170
     * @param array $args
171
     * @return self
172
     */
173
    public function setFetchType(?string $class = null, array $args = [])
174
    {
175
        $this->fetchClass = $class;
176
        $this->fetchClassArgs = $args;
177
        return $this;
178
    }
179
180
    /**
181
     * 获取取值类
182
     *
183
     * @return string|null
184
     */
185
    public function getFetchClass():?string
186
    {
187
        return $this->fetchClass ?? null;
188
    }
189
190
    /**
191
     * 是否滚动
192
     *
193
     * @return boolean|null
194
     */
195
    public function isScroll(bool $set = null):?bool
196
    {
197
        if ($set !== null) {
198
            $this->scroll = true;
199
        }
200
        return $this->scroll;
201
    }
202
203
    /**
204
     * 获取SQL字符串
205
     *
206
     * @return string
207
     */
208
    public function getString()
209
    {
210
        return $this->getQuery()->getQuery();
211
    }
212
213
    /**
214
     * 准备查询对象
215
     *
216
     * @return Query
217
     */
218
    protected function prepareQuery():Query
219
    {
220
        return new Query($this->string, $this->binder);
221
    }
222
223
    /**
224
     * 准备查询对象
225
     *
226
     * @return Query
227
     */
228
    public function prepare():Query
229
    {
230
        return $this->query = $this->prepareQuery();
231
    }
232
233
    /**
234
     * 获取查询对象
235
     *
236
     * @return \suda\orm\statement\Query
237
     */
238
    public function getQuery():Query
239
    {
240
        if ($this->query === null) {
241
            $this->query = $this->prepare();
242
        }
243
        return $this->query;
244
    }
245
246
    /**
247
     * 获取绑定信息
248
     *
249
     * @return Binder[]
250
     */
251
    public function getBinder()
252
    {
253
        return $this->binder;
254
    }
255
256
    public function __toString()
257
    {
258
        return $this->getString();
259
    }
260
    
261
262
    /**
263
     * Get PDOStatement
264
     *
265
     * @return  PDOStatement
266
     */
267
    public function getStatement():?PDOStatement
268
    {
269
        return $this->statement;
270
    }
271
272
    /**
273
     * Set PDOStatement
274
     *
275
     * @param  PDOStatement  $statement  PDOStatement
276
     *
277
     * @return  self
278
     */
279
    public function setStatement(PDOStatement $statement)
280
    {
281
        $this->statement = $statement;
282
283
        return $this;
284
    }
285
286
    
287
    /**
288
     * Get 返回类型
289
     *
290
     * @return  int
291
     */
292
    public function getReturnType()
293
    {
294
        return $this->returnType;
295
    }
296
297
    /**
298
     * Set 返回类型
299
     *
300
     * @param  int  $returnType  返回类型
301
     *
302
     * @return  self
303
     */
304
    public function setReturnType(int $returnType)
305
    {
306
        $this->returnType = $returnType;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Get 参数
313
     *
314
     * @return  array
315
     */
316
    public function getFetchClassArgs()
317
    {
318
        return $this->fetchClassArgs;
319
    }
320
321
    /**
322
     * Get 数据处理中间件
323
     *
324
     * @return  Middleware
325
     */ 
326
    public function getMiddleware()
327
    {
328
        return $this->middleware;
329
    }
330
331
    /**
332
     * Set 数据处理中间件
333
     *
334
     * @param  Middleware  $middleware  数据处理中间件
335
     *
336
     * @return  self
337
     */ 
338
    public function setMiddleware(Middleware $middleware)
339
    {
340
        $this->middleware = $middleware;
341
342
        return $this;
343
    }
344
}
345