1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql\Queries; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to write INSERT INTO queries |
7
|
|
|
* |
8
|
|
|
* @package bfw-sql |
9
|
|
|
* @author Vermeulen Maxime <[email protected]> |
10
|
|
|
* @version 2.0 |
11
|
|
|
*/ |
12
|
|
|
class Insert extends AbstractQuery |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \BfwSql\Helpers\Quoting $quoting The quoting system |
16
|
|
|
*/ |
17
|
|
|
protected $quoting; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor |
21
|
|
|
* |
22
|
|
|
* @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
23
|
|
|
* @param string $quoteStatus (default: QUOTE_ALL) Status to automatic |
24
|
|
|
* quoted string value system. |
25
|
|
|
*/ |
26
|
|
|
public function __construct( |
27
|
|
|
\BfwSql\SqlConnect $sqlConnect, |
28
|
|
|
$quoteStatus = \BfwSql\Helpers\Quoting::QUOTE_ALL |
29
|
|
|
) { |
30
|
|
|
parent::__construct($sqlConnect); |
31
|
|
|
|
32
|
|
|
$this->quoting = new \BfwSql\Helpers\Quoting( |
33
|
|
|
$quoteStatus, |
34
|
|
|
$this->sqlConnect |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Getter accessor to property quoting |
40
|
|
|
* |
41
|
|
|
* @return \BfwSql\Helpers\Quoting |
42
|
|
|
*/ |
43
|
|
|
public function getQuoting(): \BfwSql\Helpers\Quoting |
44
|
|
|
{ |
45
|
|
|
return $this->quoting; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function defineQueriesParts() |
52
|
|
|
{ |
53
|
|
|
parent::defineQueriesParts(); |
54
|
|
|
unset($this->queriesParts['where']); |
55
|
|
|
|
56
|
|
|
$parts = &$this->queriesParts; //Yes, it's just to have < 80 chars |
57
|
|
|
$partTable = $parts['table']; |
58
|
|
|
$partTable->setColumnsWithValue(true); |
59
|
|
|
$partTable->createColumnInstance(); |
60
|
|
|
|
61
|
|
|
$parts['into'] = $partTable; |
62
|
|
|
$parts['values'] = $partTable->getColumns(); |
63
|
|
|
$parts['select'] = new Select($this->sqlConnect, 'object'); |
64
|
|
|
$parts['onDuplicate'] = new Parts\ColumnValueList($this, $partTable); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function obtainGenerateOrder(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
'table' => [ |
74
|
|
|
'prefix' => 'INSERT INTO', |
75
|
|
|
'canBeEmpty' => false |
76
|
|
|
], |
77
|
|
|
'values' => [ |
78
|
|
|
'callback' => [$this, 'generateValues'], |
79
|
|
|
'usePartPrefix' => false |
80
|
|
|
], |
81
|
|
|
'select' => [ |
82
|
|
|
'callback' => [$this, 'generateSelect'], |
83
|
|
|
'usePartPrefix' => false |
84
|
|
|
], |
85
|
|
|
'onDuplicate' => [ |
86
|
|
|
'prefix' => 'ON DUPLICATE KEY UPDATE', |
87
|
|
|
'callback' => [$this, 'generateOnDuplicate'] |
88
|
|
|
] |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Callback used by assembleRequestPart method |
94
|
|
|
* Generate the sql query part who contains columns list and their values |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function generateValues(): string |
99
|
|
|
{ |
100
|
|
|
$listNames = ''; |
101
|
|
|
$listValues = ''; |
102
|
|
|
|
103
|
|
|
foreach ($this->queriesParts['values'] as $colIndex => $column) { |
104
|
|
|
|
105
|
|
|
if ($colIndex > 0) { |
106
|
|
|
$listNames .= ','; |
107
|
|
|
$listValues .= ','; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$listNames .= '`'.$column->getName().'`'; |
111
|
|
|
$listValues .= $column->obtainValue(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($listNames === '') { |
115
|
|
|
return ''; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$select = $this->queriesParts['select']; |
119
|
|
|
if ($select->getQueriesParts()['table']->getName() === '') { |
120
|
|
|
return '('.$listNames.') VALUES ('.$listValues.')'; |
121
|
|
|
} else { |
122
|
|
|
return '('.$listNames.')'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Callback used by assembleRequestPart method |
128
|
|
|
* Generate the sql query for the SELECT part |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function generateSelect(): string |
133
|
|
|
{ |
134
|
|
|
$select = $this->queriesParts['select']; |
135
|
|
|
if ($select->getQueriesParts()['table']->getName() === '') { |
136
|
|
|
return ''; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $select->assemble(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Callback used by assembleRequestPart method |
144
|
|
|
* Generate the sql query for the ON DUPLICATE KEY UPDATE part |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function generateOnDuplicate(): string |
149
|
|
|
{ |
150
|
|
|
$listColumns = $this->queriesParts['onDuplicate']; |
151
|
|
|
$sqlPart = ''; |
152
|
|
|
|
153
|
|
|
foreach ($listColumns as $colIndex => $column) { |
154
|
|
|
if ($colIndex > 0) { |
155
|
|
|
$sqlPart .= ','; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$sqlPart .= '`'.$column->getName().'`='.$column->obtainValue(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $sqlPart; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|