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
|
|
|
class Update extends AbstractQuery |
15
|
|
|
{ |
16
|
|
|
use JoinTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @const ERR_GENERATE_VALUES_NO_DATAS Exception code if no data to update. |
20
|
|
|
*/ |
21
|
|
|
const ERR_GENERATE_VALUES_NO_DATAS = 2406001; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \BfwSql\Helpers\Quoting $quoting The quoting system |
25
|
|
|
*/ |
26
|
|
|
protected $quoting; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
* |
31
|
|
|
* @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
32
|
|
|
* @param string $quoteStatus (default: QUOTE_ALL) Status to automatic |
33
|
|
|
* quoted string value system. |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
\BfwSql\SqlConnect $sqlConnect, |
37
|
|
|
string $quoteStatus = \BfwSql\Helpers\Quoting::QUOTE_ALL |
38
|
|
|
) { |
39
|
|
|
parent::__construct($sqlConnect); |
40
|
|
|
|
41
|
|
|
$this->quoting = new \BfwSql\Helpers\Quoting( |
42
|
|
|
$quoteStatus, |
43
|
|
|
$this->sqlConnect |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Getter accessor to property quoting |
49
|
|
|
* |
50
|
|
|
* @return \BfwSql\Helpers\Quoting |
51
|
|
|
*/ |
52
|
|
|
public function getQuoting(): \BfwSql\Helpers\Quoting |
53
|
|
|
{ |
54
|
|
|
return $this->quoting; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function defineQueriesParts() |
61
|
|
|
{ |
62
|
|
|
parent::defineQueriesParts(); |
63
|
|
|
|
64
|
|
|
$this->queriesParts['from'] = $this->queriesParts['table']; |
65
|
|
|
$this->queriesParts['join'] = new Parts\JoinList($this); |
66
|
|
|
$this->queriesParts['joinLeft'] = new Parts\JoinList($this); |
67
|
|
|
$this->queriesParts['joinRight'] = new Parts\JoinList($this); |
68
|
|
|
|
69
|
|
|
$this->joinDefinePrefix(); |
70
|
|
|
$this->queriesParts['from']->setColumnsWithValue(true); |
71
|
|
|
$this->queriesParts['join']->setColumnsWithValue(true); |
72
|
|
|
$this->queriesParts['joinLeft']->setColumnsWithValue(true); |
73
|
|
|
$this->queriesParts['joinRight']->setColumnsWithValue(true); |
74
|
|
|
|
75
|
|
|
$this->queriesParts['table']->createColumnInstance(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected function obtainGenerateOrder(): array |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
'table' => [ |
85
|
|
|
'prefix' => 'UPDATE', |
86
|
|
|
'canBeEmpty' => false, |
87
|
|
|
'callback' => [$this, 'generateValues'] |
88
|
|
|
], |
89
|
|
|
'join' => [], |
90
|
|
|
'joinLeft' => [], |
91
|
|
|
'joinRight' => [], |
92
|
|
|
'where' => [] |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Callback used by assembleRequestPart method |
98
|
|
|
* Generate the sql query part who contains columns list and their values |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
* |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
protected function generateValues(): string |
105
|
|
|
{ |
106
|
|
|
$sqlSet = $this->queriesParts['table']->getColumns()->generate(); |
107
|
|
|
|
108
|
|
|
$joinKeyList = ['join', 'joinLeft', 'joinRight']; |
109
|
|
|
foreach ($joinKeyList as $joinKeyName) { |
110
|
|
|
foreach ($this->queriesParts[$joinKeyName] as $join) { |
111
|
|
|
$joinSql = $join->getColumns()->generate(); |
112
|
|
|
|
113
|
|
|
if (empty($joinSql)) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($sqlSet !== '') { |
118
|
|
|
$sqlSet .= ','; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$sqlSet .= $joinSql; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($sqlSet === '') { |
126
|
|
|
throw new Exception( |
127
|
|
|
'Queries\Update : no datas to update.', |
128
|
|
|
self::ERR_GENERATE_VALUES_NO_DATAS |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->queriesParts['table']->generate().' SET '.$sqlSet; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|