|
1
|
|
|
<?php declare (strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace EmmetBlue\Core\Builder\QueryBuilder; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* class InsertQueryBuilder. |
|
10
|
|
|
* Builds an insert query. |
|
11
|
|
|
* |
|
12
|
|
|
* {@see QueryBuilder} |
|
13
|
|
|
* |
|
14
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @since 28/05/2016 |
|
17
|
|
|
*/ |
|
18
|
|
|
class InsertQueryBuilder extends QueryBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \EmmetBlue\Core\Abstraction\QueryBuilder |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $queryBuilder; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string|null $tableName |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(string $tableName = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$insertKeyword = (is_null($tableName)) ? 'INSERT' : 'INSERT INTO '.$tableName; |
|
31
|
|
|
$this->queryBuilder = $this->build($insertKeyword); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $tableName |
|
38
|
|
|
* @param string[] $tableColumns Optional, provide this to specify the |
|
39
|
|
|
* columns that should be acted on |
|
40
|
|
|
* |
|
41
|
|
|
* @return \EmmetBlue\Core\Abstraction\InsertQueryBuilder |
|
42
|
|
|
*/ |
|
43
|
|
|
public function into(string $tableName, array $tableColumns = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$intoKeyword = 'INTO '.$tableName; |
|
46
|
|
|
|
|
47
|
|
|
if (!empty($tableColumns)) { |
|
48
|
|
|
$intoKeyword .= $this->wrapString(self::getImplodedString($tableColumns), '(', ')'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->queryBuilder = $this->queryBuilder->build($intoKeyword); |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
* |
|
59
|
|
|
* @param string[] $inputValues |
|
60
|
|
|
* |
|
61
|
|
|
* @return \EmmetBlue\Core\Abstraction\InsertQueryBuilder |
|
62
|
|
|
*/ |
|
63
|
|
|
public function values(array $inputValues) |
|
64
|
|
|
{ |
|
65
|
|
|
$valuesKeyword = 'VALUES '; |
|
66
|
|
|
$isMultidimentional = is_array($inputValues[0]); |
|
67
|
|
|
|
|
68
|
|
|
if (!$isMultidimentional) { |
|
69
|
|
|
$valuesKeyword .= $this->wrapString(self::getImplodedString($inputValues), '(', ')'); |
|
70
|
|
|
} else { |
|
71
|
|
|
$tempValuesKeywords = []; |
|
72
|
|
|
foreach ($inputValues as $inputValue) { |
|
73
|
|
|
$tempValuesKeywords[] = $this->wrapString(self::getImplodedString($inputValue), '(', ')'); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$valuesKeyword .= self::getImplodedString($tempValuesKeywords); |
|
77
|
|
|
unset($tempValuesKeywords); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->queryBuilder = $this->queryBuilder->build($valuesKeyword); |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..