Passed
Push — master ( 8a2cc4...a6995c )
by 世昌
02:15
created

ReadStatement::hasLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace suda\database\struct;
4
5
use suda\database\exception\SQLException;
6
use suda\database\TableAccess;
7
8
class ReadStatement extends \suda\database\statement\ReadStatement
9
{
10
    /**
11
     * 访问操作
12
     *
13
     * @var TableAccess
14
     */
15
    protected $access;
16
17
    public function __construct(TableAccess $access)
18
    {
19
        $this->access = $access;
20
        parent::__construct(
21
            $access->getSource()->write()->rawTableName($access->getStruct()->getName()),
22
            $access->getStruct(),
23
            $access->getMiddleware()
24
        );
25
    }
26
27
    /**
28
     * 取1
29
     *
30
     * @param string|null $class
31
     * @param array $args
32
     * @return mixed
33
     * @throws SQLException
34
     */
35
    public function one(?string $class = null, array $args = [])
36
    {
37
        if ($this->isScroll() === false && $this->hasLimit() === false) {
38
            $this->limit(0, 1);
39
        }
40
        return $this->access->run($this->wantOne($class, $args));
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    private function hasLimit()
47
    {
48
        return strlen($this->limit) > 0;
49
    }
50
51
    /**
52
     * 取全部
53
     *
54
     * @param string|null $class
55
     * @param array $args
56
     * @return array
57
     * @throws SQLException
58
     */
59
    public function all(?string $class = null, array $args = []): array
60
    {
61
        return $this->access->run($this->wantAll($class, $args));
62
    }
63
64
    /**
65
     * 取1
66
     *
67
     * @param string|null $class
68
     * @param array $args
69
     * @return mixed
70
     * @throws SQLException
71
     */
72
    public function fetch(?string $class = null, array $args = [])
73
    {
74
        return $this->one($class, $args);
75
    }
76
77
    /**
78
     * 取全部
79
     *
80
     * @param string|null $class
81
     * @param array $args
82
     * @return array
83
     * @throws SQLException
84
     */
85
    public function fetchAll(?string $class = null, array $args = []): array
86
    {
87
        return $this->all($class, $args);
88
    }
89
90
    /**
91
     * Get 访问操作
92
     *
93
     * @return  TableAccess
94
     */
95
    public function getAccess(): TableAccess
96
    {
97
        return $this->access;
98
    }
99
}
100