Passed
Push — master ( a6995c...cf10de )
by 世昌
03:27 queued 01:11
created

QueryStatement::field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\database\struct;
3
4
use suda\database\exception\SQLException;
5
use suda\database\statement\QueryAccess;
6
7
8
class QueryStatement extends \suda\database\statement\QueryStatement
9
{
10
    /**
11
     * 访问操作
12
     *
13
     * @var QueryAccess
14
     */
15
    protected $access;
16
17
    public function __construct(QueryAccess $access, string $query, ...$parameter)
18
    {
19
        $this->access = $access;
20
        parent::__construct($query, ...$parameter);
21
    }
22
23
    /**
24
     * 取1
25
     *
26
     * @param string|null $class
27
     * @param array $args
28
     * @return mixed
29
     * @throws SQLException
30
     */
31
    public function one(?string $class = null, array $args = [])
32
    {
33
        $this->setType(static::READ);
34
        $value = $this->access->run($this->wantOne($class, $args));
35
        if (is_array($value)) {
36
            return $value;
37
        }
38
        return null;
39
    }
40
41
    /**
42
     * 取全部
43
     *
44
     * @param string|null $class
45
     * @param array $args
46
     * @return array
47
     * @throws SQLException
48
     */
49
    public function all(?string $class = null, array $args = []):array
50
    {
51
        $this->setType(static::READ);
52
        return $this->access->run($this->wantAll($class, $args));
53
    }
54
55
    /**
56
     * 取一列
57
     * @param string $name
58
     * @param mixed $default
59
     * @return mixed
60
     * @throws SQLException
61
     */
62
    public function field(string $name, $default = null) {
63
        $row = $this->one();
64
        return $row[$name] ?? $default;
65
    }
66
67
    /**
68
     * 取数组的一列
69
     * @param string $name
70
     * @return array
71
     * @throws SQLException
72
     */
73
    public function allField(string $name)  {
74
        $row = $this->all();
75
        return array_column($row, $name);
76
    }
77
78
    /**
79
     * 取1
80
     *
81
     * @param string|null $class
82
     * @param array $args
83
     * @return mixed
84
     * @throws SQLException
85
     */
86
    public function fetch(?string $class = null, array $args = [])
87
    {
88
        return $this->one($class, $args);
89
    }
90
91
    /**
92
     * 取全部
93
     *
94
     * @param string|null $class
95
     * @param array $args
96
     * @return array
97
     * @throws SQLException
98
     */
99
    public function fetchAll(?string $class = null, array $args = []):array
100
    {
101
        return $this->all($class, $args);
102
    }
103
104
    /**
105
     * 返回影响行数
106
     *
107
     * @return int
108
     * @throws SQLException
109
     */
110
    public function rows():int
111
    {
112
        $this->returnType = WriteStatement::RET_ROWS;
113
        $this->setType(static::WRITE);
114
        return $this->access->run($this);
115
    }
116
117
    /**
118
     * 返回是否成功
119
     *
120
     * @return boolean
121
     * @throws SQLException
122
     */
123
    public function ok():bool
124
    {
125
        $this->returnType = WriteStatement::RET_BOOL;
126
        $this->setType(static::WRITE);
127
        return $this->access->run($this);
128
    }
129
130
    /**
131
     * 返回ID
132
     *
133
     * @return string
134
     * @throws SQLException
135
     */
136
    public function id():string
137
    {
138
        $this->returnType = WriteStatement::RET_LAST_INSERT_ID;
139
        $this->setType(static::WRITE);
140
        return $this->access->run($this);
141
    }
142
143
    /**
144
     * Get 访问操作
145
     *
146
     * @return  QueryAccess
147
     */
148
    public function getAccess():QueryAccess
149
    {
150
        return $this->access;
151
    }
152
}
153