Update   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getQuoting() 0 3 1
A generateValues() 0 29 6
A defineQueriesParts() 0 25 1
A obtainGenerateOrder() 0 12 1
1
<?php
2
3
namespace BfwSql\Queries;
4
5
use \Exception;
6
7
/**
8
 * Class to write UPDATE queries
9
 * 
10
 * @package bfw-sql
11
 * @author Vermeulen Maxime <[email protected]>
12
 * @version 2.0
13
 * 
14
 * @method \BfwSql\Queries\Update from(string|array $nameInfos, string|array|null $columns=null)
15
 * @method \BfwSql\Queries\Update set(array $columns)
16
 */
17
class Update extends AbstractQuery
18
{
19
    use JoinTrait;
20
    
21
    /**
22
     * @const ERR_GENERATE_VALUES_NO_DATAS Exception code if no data to update.
23
     */
24
    const ERR_GENERATE_VALUES_NO_DATAS = 2406001;
25
    
26
    /**
27
     * @var \BfwSql\Helpers\Quoting $quoting The quoting system
28
     */
29
    protected $quoting;
30
    
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected $requestType = 'update';
35
    
36
    /**
37
     * Constructor
38
     * 
39
     * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion
40
     * @param string $quoteStatus (default: QUOTE_ALL) Status to automatic
41
     *  quoted string value system.
42
     */
43
    public function __construct(
44
        \BfwSql\SqlConnect $sqlConnect,
45
        string $quoteStatus = \BfwSql\Helpers\Quoting::QUOTE_ALL
46
    ) {
47
        parent::__construct($sqlConnect);
48
        
49
        $this->quoting = new \BfwSql\Helpers\Quoting(
50
            $quoteStatus,
51
            $this->sqlConnect
52
        );
53
    }
54
    
55
    /**
56
     * Getter accessor to property quoting
57
     * 
58
     * @return \BfwSql\Helpers\Quoting
59
     */
60
    public function getQuoting(): \BfwSql\Helpers\Quoting
61
    {
62
        return $this->quoting;
63
    }
64
    
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function defineQueriesParts()
69
    {
70
        parent::defineQueriesParts();
71
        
72
        $parts     = &$this->queriesParts; //Yes, it's just to have < 80 chars
73
        $partTable = $parts['table'];
74
        
75
        $usedClass     = \BfwSql\UsedClass::getInstance();
76
        $joinListClass = $usedClass->obtainClassNameToUse('QueriesPartsJoinList');
77
        
78
        $this->queriesParts['from']      = $partTable;
79
        $this->queriesParts['set']       = &$partTable->getColumns();
80
        $this->queriesParts['join']      = new $joinListClass($this);
81
        $this->queriesParts['joinLeft']  = new $joinListClass($this);
82
        $this->queriesParts['joinRight'] = new $joinListClass($this);
83
        
84
        $this->joinDefinePrefix();
85
        $this->queriesParts['from']->setColumnsWithValue(true);
86
        $this->queriesParts['join']->setColumnsWithValue(true);
87
        $this->queriesParts['joinLeft']->setColumnsWithValue(true);
88
        $this->queriesParts['joinRight']->setColumnsWithValue(true);
89
        
90
        $this->queriesParts['table']->createColumnInstance();
91
        
92
        $this->querySgbd->disableQueriesParts($this->queriesParts);
93
    }
94
    
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function obtainGenerateOrder(): array
99
    {
100
        return [
101
            'table' => [
102
                'prefix'     => 'UPDATE',
103
                'canBeEmpty' => false,
104
                'callback'   => [$this, 'generateValues']
105
            ],
106
            'join' => [],
107
            'joinLeft' => [],
108
            'joinRight' => [],
109
            'where' => []
110
        ];
111
    }
112
    
113
    /**
114
     * Callback used by assembleRequestPart method
115
     * Generate the sql query part who contains columns list and their values
116
     * 
117
     * @return string
118
     * 
119
     * @throws \Exception
120
     */
121
    protected function generateValues(): string
122
    {
123
        $sqlSet = $this->queriesParts['table']->getColumns()->generate();
124
        
125
        $joinKeyList = ['join', 'joinLeft', 'joinRight'];
126
        foreach ($joinKeyList as $joinKeyName) {
127
            foreach ($this->queriesParts[$joinKeyName] as $join) {
128
                $joinSql = $join->getColumns()->generate();
129
                
130
                if (empty($joinSql)) {
131
                    continue;
132
                }
133
                
134
                if ($sqlSet !== '') {
135
                    $sqlSet .= ',';
136
                }
137
                
138
                $sqlSet .= $joinSql;
139
            }
140
        }
141
        
142
        if ($sqlSet === '') {
143
            throw new Exception(
144
                'Queries\Update : no datas to update.',
145
                self::ERR_GENERATE_VALUES_NO_DATAS
146
            );
147
        }
148
        
149
        return $this->queriesParts['table']->generate().' SET '.$sqlSet;
150
    }
151
}
152