|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2021 Aleksandar Panic |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Sql\Query; |
|
19
|
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
|
21
|
|
|
use ArekX\PQL\Query; |
|
22
|
|
|
use ArekX\PQL\Sql\Query\Traits\ConfigureTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Represents an insert query |
|
26
|
|
|
* for adding new data |
|
27
|
|
|
*/ |
|
28
|
|
|
class Insert extends Query |
|
29
|
|
|
{ |
|
30
|
|
|
use ConfigureTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set the destination to insert data into. |
|
34
|
|
|
* |
|
35
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
|
36
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
|
37
|
|
|
* and escape query use Raw query. |
|
38
|
|
|
* |
|
39
|
|
|
* If a StructuredQuery is passed, it will be additionally parsed according to that type. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string|StructuredQuery $into Table or other destination to insert data into. |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
25 |
|
public function into(string|StructuredQuery $into): static |
|
45
|
|
|
{ |
|
46
|
25 |
|
$this->use('into', $into); |
|
47
|
25 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set columns to insert. |
|
52
|
|
|
* |
|
53
|
|
|
* **SQL Injection Warning**: Value in this function is not usually escaped in the driver |
|
54
|
|
|
* and should not be used to pass values from the user input to it. If you need to pass |
|
55
|
|
|
* and escape query use Raw query. |
|
56
|
|
|
* |
|
57
|
|
|
* Column count should match the value count if an array is passed. |
|
58
|
|
|
* |
|
59
|
|
|
* If a StructuredQuery is passed, it will be additionally parsed according to that type. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array|StructuredQuery|null $columns Columns to set. |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
26 |
|
public function columns(array|StructuredQuery|null $columns): static |
|
65
|
|
|
{ |
|
66
|
26 |
|
$this->use('columns', $columns); |
|
67
|
26 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Append values to insert. |
|
72
|
|
|
* |
|
73
|
|
|
* Value count should match the column count if an array is passed. |
|
74
|
|
|
* |
|
75
|
|
|
* Values are properly escaped making this method suitable to |
|
76
|
|
|
* accept user input. |
|
77
|
|
|
* |
|
78
|
|
|
* If a StructuredQuery is passed, it will be additionally parsed according to that type. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array|StructuredQuery|null $values |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
25 |
|
public function values(array|StructuredQuery|null $values): static |
|
84
|
|
|
{ |
|
85
|
25 |
|
$this->append('values', $values); |
|
86
|
25 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set columns and values from an associative array. |
|
91
|
|
|
* |
|
92
|
|
|
* Columns are parsed with columns() method and |
|
93
|
|
|
* values are parsed with values() method. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $data Associative array containing keys and values to insert. |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
21 |
|
public function data(array $data): static |
|
99
|
|
|
{ |
|
100
|
21 |
|
$this->columns(array_keys($data)); |
|
101
|
21 |
|
return $this->values(array_values($data)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|