Passed
Push — master ( 9664e3...41a2b4 )
by 世昌
01:58
created

Statement::getStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace suda\orm\statement;
4
5
use function is_array;
6
use PDOStatement;
7
use suda\orm\Binder;
8
use suda\orm\middleware\Middleware;
9
10
abstract class Statement
11
{
12
    use PrepareTrait;
13
14
    const WRITE = 0;
15
    const READ = 1;
16
17
    const FETCH_ONE = 0;
18
    const FETCH_ALL = 1;
19
20
    /**
21
     * 语句类型
22
     *
23
     * @var int|null
24
     */
25
    protected $type = null;
26
27
    /**
28
     * 获取类型
29
     *
30
     * @var int
31
     */
32
    protected $fetch = null;
33
34
    /**
35
     * 滚动
36
     *
37
     * @var boolean|null
38
     */
39
    protected $scroll = null;
40
41
    /**
42
     * 绑定
43
     *
44
     * @var Binder[]
45
     */
46
    protected $binder = [];
47
48
    /**
49
     * SQL语句
50
     *
51
     * @var string
52
     */
53
    protected $string;
54
55
    /**
56
     * PDOStatement
57
     *
58
     * @var PDOStatement|null
59
     */
60
    protected $statement = null;
61
62
    /**
63
     * Query
64
     *
65
     * @var Query
66
     */
67
    protected $query;
68
69
    const RET_ROWS = 1;
70
    const RET_LAST_INSERT_ID = 2;
71
    const RET_BOOL = 3;
72
73
    /**
74
     * 返回类型
75
     *
76
     * @var int
77
     */
78
    protected $returnType = Statement::RET_BOOL;
79
80
    /**
81
     * 类
82
     *
83
     * @var string
84
     */
85
    protected $fetchClass;
86
87
    /**
88
     * 参数
89
     *
90
     * @var array
91
     */
92
    protected $fetchClassArgs = [];
93
94
    /**
95
     * 数据处理中间件
96
     *
97
     * @var Middleware
98
     */
99
    protected $middleware;
100
101
    /**
102
     * 运行结果
103
     *
104
     * @var bool
105
     */
106
    protected $success;
107
108
    /**
109
     * Statement constructor.
110
     * @param string $sql
111
     * @param mixed ...$args
112
     */
113
    public function __construct(string $sql, ...$args)
114
    {
115
        if (count($args) === 1 && is_array($args[0])) {
116
            $this->create($sql, $args[0]);
117
        } else {
118
            list($this->string, $this->binder) = $this->prepareQueryMark($sql, $args);
119
        }
120
    }
121
122
    protected function create(string $sql, array $parameter)
123
    {
124
        $this->string = $sql;
125
        $this->binder = $this->mergeBinder($this->binder, $parameter);
126
    }
127
128
    public function isRead(bool $set = null): bool
129
    {
130
        if ($set !== null) {
131
            $this->type = self::READ;
132
        }
133
        return $this->type === self::READ;
134
    }
135
136
    public function isWrite(bool $set = null): bool
137
    {
138
        if ($set !== null) {
139
            $this->type = self::WRITE;
140
        }
141
        return $this->type === self::WRITE;
142
    }
143
144
    /**
145
     * 判断是否为一条
146
     *
147
     * @param bool|null $set
148
     * @return boolean
149
     */
150
    public function isFetchOne(bool $set = null): bool
151
    {
152
        if ($set !== null) {
153
            $this->fetch = self::FETCH_ONE;
154
        }
155
        return $this->fetch === self::FETCH_ONE;
156
    }
157
158
    /**
159
     * 判断是否为一条
160
     *
161
     * @return boolean
162
     */
163
    public function isFetch(): bool
164
    {
165
        return $this->fetch !== null;
166
    }
167
168
    /**
169
     * @param bool|null $scroll
170
     */
171
    public function setScroll(?bool $scroll): void
172
    {
173
        $this->scroll = $scroll;
174
    }
175
176
    /**
177
     * 判断是否获取多条
178
     *
179
     * @param bool|null $set
180
     * @return boolean
181
     */
182
    public function isFetchAll(bool $set = null): bool
183
    {
184
        if ($set !== null) {
185
            $this->fetch = self::FETCH_ALL;
186
        }
187
        return $this->fetch === self::FETCH_ALL;
188
    }
189
190
    /**
191
     * 设置记录类
192
     *
193
     * @param string|null $class
194
     * @param array $args
195
     * @return $this
196
     */
197
    public function setFetchType(?string $class = null, array $args = [])
198
    {
199
        $this->fetchClass = $class;
200
        $this->fetchClassArgs = $args;
201
        return $this;
202
    }
203
204
    /**
205
     * 获取取值类
206
     *
207
     * @return string|null
208
     */
209
    public function getFetchClass(): ?string
210
    {
211
        return $this->fetchClass ?? null;
212
    }
213
214
    /**
215
     * 是否滚动
216
     *
217
     * @param bool|null $set
218
     * @return boolean|null
219
     */
220
    public function isScroll(bool $set = null): ?bool
221
    {
222
        if ($set !== null) {
223
            $this->scroll = true;
224
        }
225
        return $this->scroll;
226
    }
227
228
    /**
229
     * 获取SQL字符串
230
     *
231
     * @return string
232
     */
233
    public function getString()
234
    {
235
        return $this->getQuery()->getQuery();
236
    }
237
238
    /**
239
     * 准备查询对象
240
     *
241
     * @return Query
242
     */
243
    protected function prepareQuery(): Query
244
    {
245
        return new Query($this->string, $this->binder);
246
    }
247
248
    /**
249
     * 准备查询对象
250
     *
251
     * @return Query
252
     */
253
    public function prepare(): Query
254
    {
255
        return $this->query = $this->prepareQuery();
256
    }
257
258
    /**
259
     * 获取查询对象
260
     *
261
     * @return Query
262
     */
263
    public function getQuery(): Query
264
    {
265
        if ($this->query === null) {
266
            $this->query = $this->prepare();
267
        }
268
        return $this->query;
269
    }
270
271
    /**
272
     * @param Query $query
273
     */
274
    public function setQuery(Query $query): void
275
    {
276
        $this->query = $query;
277
        $this->string = $query->getQuery();
278
        $this->binder = $query->getBinder();
279
    }
280
281
    /**
282
     * 获取绑定信息
283
     *
284
     * @return Binder[]
285
     */
286
    public function getBinder()
287
    {
288
        return $this->binder;
289
    }
290
291
    /**
292
     * @return string
293
     */
294
    public function __toString()
295
    {
296
        return $this->getString();
297
    }
298
299
300
    /**
301
     * Get PDOStatement
302
     *
303
     * @return  PDOStatement
304
     */
305
    public function getStatement(): ?PDOStatement
306
    {
307
        return $this->statement;
308
    }
309
310
    /**
311
     * Set PDOStatement
312
     *
313
     * @param PDOStatement $statement PDOStatement
314
     *
315
     * @return  $this
316
     */
317
    public function setStatement(PDOStatement $statement)
318
    {
319
        $this->statement = $statement;
320
321
        return $this;
322
    }
323
324
325
    /**
326
     * Get 返回类型
327
     *
328
     * @return  int
329
     */
330
    public function getReturnType()
331
    {
332
        return $this->returnType;
333
    }
334
335
    /**
336
     * Set 返回类型
337
     *
338
     * @param int $returnType 返回类型
339
     * @return  $this
340
     */
341
    public function setReturnType(int $returnType)
342
    {
343
        $this->returnType = $returnType;
344
        return $this;
345
    }
346
347
    /**
348
     * Get 参数
349
     *
350
     * @return  array
351
     */
352
    public function getFetchClassArgs()
353
    {
354
        return $this->fetchClassArgs;
355
    }
356
357
    /**
358
     * Get 数据处理中间件
359
     * @return  Middleware
360
     */
361
    public function getMiddleware()
362
    {
363
        return $this->middleware;
364
    }
365
366
    /**
367
     * Set 数据处理中间件
368
     *
369
     * @param Middleware $middleware 数据处理中间件
370
     * @return  $this
371
     */
372
    public function setMiddleware(Middleware $middleware)
373
    {
374
        $this->middleware = $middleware;
375
        return $this;
376
    }
377
378
    /**
379
     * @param Binder $binder
380
     * @return $this
381
     */
382
    public function addBinder(Binder $binder)
383
    {
384
        if (!in_array($binder, $this->binder)) {
385
            $this->binder[] = $binder;
386
        }
387
        return $this;
388
    }
389
390
    /**
391
     * 添加参数
392
     *
393
     * @param string $name
394
     * @param mixed $value
395
     * @param string|null $key
396
     * @return $this
397
     */
398
    public function addValue(string $name, $value, ?string $key = null)
399
    {
400
        return $this->addBinder(new Binder($name, $value, $key));
401
    }
402
403
    /**
404
     * @param int|null $type
405
     */
406
    public function setType(?int $type): void
407
    {
408
        $this->type = $type;
409
    }
410
411
    /**
412
     * @return int
413
     */
414
    public function getFetch(): int
415
    {
416
        return $this->fetch;
417
    }
418
419
    /**
420
     * @return int|null
421
     */
422
    public function getType(): ?int
423
    {
424
        return $this->type;
425
    }
426
427
    /**
428
     * @param int $fetch
429
     */
430
    public function setFetch(int $fetch): void
431
    {
432
        $this->fetch = $fetch;
433
    }
434
435
    /**
436
     * @return bool
437
     */
438
    public function isSuccess(): bool
439
    {
440
        return $this->success;
441
    }
442
443
    /**
444
     * @param bool $success
445
     */
446
    public function setSuccess(bool $success): void
447
    {
448
        $this->success = $success;
449
    }
450
}
451