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

QueryStatement::wantOne()   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\database\statement;
3
4
use suda\database\middleware\NullMiddleware;
5
6
class QueryStatement extends Statement
7
{
8
    use PrepareTrait;
9
10
    protected $withKey = null;
11
12
    /**
13
     * 创建写
14
     *
15
     * @param string $query
16
     * @param array $args
17
     */
18
    public function __construct(string $query, ...$args)
19
    {
20
        parent::__construct($query, ...$args);
21
        $this->type = self::READ;
22
        $this->fetch = self::FETCH_ONE;
23
        $this->middleware = new NullMiddleware;
24
    }
25
26
    /**
27
     * 设置取一条记录
28
     *
29
     * @param string|null $class
30
     * @param array $args
31
     * @return $this
32
     */
33
    public function wantOne(?string $class = null, array $args = [])
34
    {
35
        $this->fetch = self::FETCH_ONE;
36
        if ($class !== null) {
37
            $this->setFetchType($class, $args);
38
        }
39
        return $this;
40
    }
41
42
    /**
43
     * 设置取全部记录
44
     *
45
     * @param string|null $class
46
     * @param array $args
47
     * @return $this
48
     */
49
    public function wantAll(?string $class = null, array $args = [])
50
    {
51
        $this->fetch = self::FETCH_ALL;
52
        if ($class !== null) {
53
            $this->setFetchType($class, $args);
54
        }
55
        return $this;
56
    }
57
58
    /**
59
     * 设置取值类
60
     *
61
     * @param string|null $class
62
     * @param array $args
63
     * @return $this
64
     */
65
    public function wantType(?string $class = null, array $args = [])
66
    {
67
        $this->setFetchType($class, $args);
68
        return $this;
69
    }
70
    
71
    /**
72
     * 设置使用某个字段做Key
73
     *
74
     * @param string $key
75
     * @return $this
76
     */
77
    public function withKey(string $key)
78
    {
79
        $this->withKey = $key;
80
        $this->wantAll();
81
        return $this;
82
    }
83
84
    /**
85
     * Get the value of withKey
86
     */
87
    public function getWithKey()
88
    {
89
        return $this->withKey;
90
    }
91
    
92
    /**
93
     * 滚动获取
94
     *
95
     * @return $this
96
     */
97
    public function scroll()
98
    {
99
        $this->scroll = true;
100
        return $this;
101
    }
102
}
103