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