1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Muffin\Queries\Snippets; |
4
|
|
|
|
5
|
|
|
use Muffin\Snippet; |
6
|
|
|
use Muffin\Type; |
7
|
|
|
use Muffin\Types; |
8
|
|
|
use Muffin\Traits\EscaperAware; |
9
|
|
|
use Muffin\Types\String; |
10
|
|
|
use Muffin\Traits\TypeGuesser; |
11
|
|
|
|
12
|
|
|
class Values implements Snippet |
13
|
|
|
{ |
14
|
|
|
use |
15
|
|
|
EscaperAware, |
16
|
|
|
TypeGuesser; |
17
|
|
|
|
18
|
|
|
private |
19
|
|
|
$values; |
20
|
|
|
|
21
|
13 |
|
public function __construct($values = null) |
22
|
|
|
{ |
23
|
13 |
|
$this->values = array(); |
24
|
|
|
|
25
|
13 |
|
if(! empty($values)) |
26
|
13 |
|
{ |
27
|
7 |
|
$this->values($values); |
28
|
7 |
|
} |
29
|
13 |
|
} |
30
|
|
|
|
31
|
12 |
|
public function values(array $values) |
|
|
|
|
32
|
|
|
{ |
33
|
12 |
|
$this->values[] = $values; |
34
|
|
|
|
35
|
12 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
13 |
|
public function toString() |
39
|
|
|
{ |
40
|
13 |
|
if(empty($this->values)) |
41
|
13 |
|
{ |
42
|
1 |
|
throw new \RuntimeException('No values to insert'); |
43
|
|
|
} |
44
|
|
|
|
45
|
12 |
|
$columnsNameList = array_filter(array_keys(reset($this->values))); |
46
|
|
|
|
47
|
12 |
|
$values = array(); |
48
|
12 |
|
foreach($this->values as $valuesSet) |
49
|
|
|
{ |
50
|
12 |
|
if($columnsNameList !== array_keys($valuesSet)) |
51
|
12 |
|
{ |
52
|
1 |
|
throw new \RuntimeException('Cannot insert different schema on the same table.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
$values[] = $this->buildValuesSetString($valuesSet); |
56
|
12 |
|
} |
57
|
|
|
|
58
|
11 |
|
return sprintf( |
59
|
11 |
|
'%s VALUES %s', |
60
|
11 |
|
$this->wrapWithParentheses(implode(', ', $columnsNameList)), |
61
|
11 |
|
implode(', ', $values) |
62
|
11 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
private function buildValuesSetString(array $values) |
66
|
|
|
{ |
67
|
12 |
|
$valuesSet = array(); |
68
|
12 |
|
foreach($values as $columnName => $value) |
69
|
|
|
{ |
70
|
12 |
|
if(! empty($columnName)) |
71
|
12 |
|
{ |
72
|
12 |
|
$type = $this->guessType($columnName, $value); |
73
|
|
|
|
74
|
12 |
|
$valuesSet[] = $this->formatValue($type, $value); |
75
|
12 |
|
} |
76
|
12 |
|
} |
77
|
|
|
|
78
|
12 |
|
return $this->wrapWithParentheses(implode(', ', $valuesSet)); |
79
|
|
|
} |
80
|
|
|
|
81
|
12 |
|
private function formatValue(Type $type, $value) |
82
|
|
|
{ |
83
|
12 |
|
if(is_null($value)) |
84
|
12 |
|
{ |
85
|
1 |
|
return 'NULL'; |
86
|
|
|
} |
87
|
|
|
|
88
|
12 |
|
return $this->escapeValue($type, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
View Code Duplication |
private function escapeValue(Type $type, $value) |
|
|
|
|
92
|
|
|
{ |
93
|
12 |
|
$value = $type->format($value); |
94
|
|
|
|
95
|
12 |
|
if($type->isEscapeRequired()) |
96
|
12 |
|
{ |
97
|
11 |
|
$value = $this->escaper->escape($value); |
98
|
11 |
|
} |
99
|
|
|
|
100
|
12 |
|
return $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
12 |
|
private function wrapWithParentheses($value) |
104
|
|
|
{ |
105
|
12 |
|
return sprintf('(%s)', $value); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|