1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @see http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace JAQB\Query; |
13
|
|
|
|
14
|
|
|
use JAQB\Operations\Executable; |
15
|
|
|
use JAQB\Statement\FromStatement; |
16
|
|
|
use JAQB\Statement\ValuesStatement; |
17
|
|
|
|
18
|
|
|
class InsertQuery extends AbstractQuery |
19
|
|
|
{ |
20
|
|
|
use Executable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var FromStatement |
24
|
|
|
*/ |
25
|
|
|
protected $table; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ValuesStatement |
29
|
|
|
*/ |
30
|
|
|
protected $insertValues; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $values = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $onDuplicateKeyUpdate; |
41
|
|
|
|
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->table = new FromStatement(FromStatement::INSERT); |
45
|
|
|
$this->insertValues = new ValuesStatement(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets the table for the query. |
50
|
|
|
* |
51
|
|
|
* @param string $table table name |
52
|
|
|
* |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
|
|
public function into($table) |
56
|
|
|
{ |
57
|
|
|
$this->table->addTable($table); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the values for the query. |
64
|
|
|
* |
65
|
|
|
* @param array $values |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function values(array $values) |
70
|
|
|
{ |
71
|
|
|
$this->insertValues->addValues($values); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the table for the query. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getInto() |
82
|
|
|
{ |
83
|
|
|
return $this->table; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the insert values for the query. |
88
|
|
|
* |
89
|
|
|
* @return ValuesStatement |
90
|
|
|
*/ |
91
|
|
|
public function getInsertValues() |
92
|
|
|
{ |
93
|
|
|
return $this->insertValues; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Adds an ON DUPLICATE KEY UPDATE clause. |
98
|
|
|
* |
99
|
|
|
* @param string $sql |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
|
|
public function onDuplicateKeyUpdate($sql) |
104
|
|
|
{ |
105
|
|
|
$this->onDuplicateKeyUpdate = $sql; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets the ON DUPLICATE KEY UPDATE clause. |
112
|
|
|
* |
113
|
|
|
* @return string|null |
114
|
|
|
*/ |
115
|
|
|
public function getOnDuplicateKeyUpdate() |
116
|
|
|
{ |
117
|
|
|
return $this->onDuplicateKeyUpdate; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Generates the raw SQL string for the query. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function build() |
126
|
|
|
{ |
127
|
|
|
$sql = [ |
128
|
|
|
$this->table->build(), |
129
|
|
|
$this->insertValues->build(), |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
if ($this->onDuplicateKeyUpdate) { |
133
|
|
|
$sql[] = 'ON DUPLICATE KEY UPDATE '.$this->onDuplicateKeyUpdate; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->values = array_values($this->insertValues->getValues()); |
137
|
|
|
|
138
|
|
|
return implode(' ', array_filter($sql)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function __clone() |
142
|
|
|
{ |
143
|
|
|
$this->table = clone $this->table; |
144
|
|
|
$this->insertValues = clone $this->insertValues; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|