Passed
Push — master ( 9155e7...72c61b )
by 世昌
02:37
created

QueryStatementBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A modifyQueryStatement() 0 16 6
A __construct() 0 4 1
A build() 0 4 1
A getQuery() 0 17 4
1
<?php
2
3
4
namespace suda\application\database;
5
6
use suda\application\exception\ConfigurationException;
7
use suda\orm\statement\QueryAccess;
8
use suda\orm\struct\QueryStatement;
9
10
/**
11
 * 语句读取
12
 * 从模块资源文件中读取SQL语句构建查询对象
13
 * @package suda\application\database
14
 */
15
class QueryStatementBuilder
16
{
17
    /**
18
     * 查询配置
19
     * @var array
20
     */
21
    protected $config;
22
23
    /**
24
     * 查询操作
25
     * @var QueryAccess
26
     */
27
    protected $access;
28
29
    /**
30
     * QueryStatementBuilder constructor.
31
     * @param QueryAccess $access
32
     * @param array $config
33
     */
34
    public function __construct(QueryAccess $access, array $config = [])
35
    {
36
        $this->config = $config;
37
        $this->access = $access;
38
    }
39
40
    /**
41
     * @return QueryStatement
42
     */
43
    public function build():QueryStatement
44
    {
45
        $query = new QueryStatement($this->access, $this->getQuery());
46
        return $this->modifyQueryStatement($query);
47
    }
48
49
    /**
50
     * 获取查询
51
     * @return string
52
     */
53
    protected function getQuery()
54
    {
55
        if (array_key_exists('query', $this->config)) {
56
            $query = $this->config['query'];
57
            if (is_array($query)) {
58
                $type = $this->access->getConnection()->getType();
59
                if (array_key_exists($type, $query)) {
60
                    return $query[$type];
61
                }
62
                throw new ConfigurationException(
63
                    sprintf("missing config query:%s", $type),
64
                    ConfigurationException::ERR_MISSING_CONFIG
65
                );
66
            }
67
            return $query;
68
        }
69
        throw new ConfigurationException('missing config query', ConfigurationException::ERR_MISSING_CONFIG);
70
    }
71
72
    /**
73
     * 修饰查询
74
     * @param QueryStatement $statement
75
     * @return QueryStatement
76
     */
77
    protected function modifyQueryStatement(QueryStatement $statement):QueryStatement
78
    {
79
        $type = $this->config['type'] ?? 'read';
80
        if (in_array($type, ['read','write'])) {
81
            $statement->setType($type === 'read'?QueryStatement::READ:QueryStatement::WRITE);
82
        }
83
        if (array_key_exists('return-type', $this->config)) {
84
            $statement->setReturnType($this->config['return-type']);
85
        }
86
        if (array_key_exists('with-key', $this->config)) {
87
            $statement->withKey($this->config['with-key']);
88
        }
89
        if (array_key_exists('scroll', $this->config)) {
90
            $statement->setScroll($this->config['scroll']);
91
        }
92
        return $statement;
93
    }
94
}
95