1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 6/11/14 |
5
|
|
|
* Time: 1:51 AM. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryBuilder\Builder\Syntax; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder; |
14
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert; |
15
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class InsertWriter. |
19
|
|
|
*/ |
20
|
|
|
class InsertWriter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var GenericBuilder |
24
|
|
|
*/ |
25
|
|
|
private $writer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ColumnWriter |
29
|
|
|
*/ |
30
|
|
|
private $columnWriter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param GenericBuilder $writer |
34
|
|
|
* @param PlaceholderWriter $placeholder |
35
|
|
|
*/ |
36
|
|
|
public function __construct(GenericBuilder $writer, PlaceholderWriter $placeholder) |
37
|
|
|
{ |
38
|
|
|
$this->writer = $writer; |
39
|
|
|
$this->columnWriter = WriterFactory::createColumnWriter($this->writer, $placeholder); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Insert $insert |
44
|
|
|
* |
45
|
|
|
* @throws QueryException |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function write(Insert $insert) |
50
|
|
|
{ |
51
|
|
|
$columns = $insert->getColumns(); |
52
|
|
|
|
53
|
|
|
if (empty($columns)) { |
54
|
|
|
throw new QueryException('No columns were defined for the current schema.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$columns = $this->writeQueryColumns($columns); |
58
|
|
|
$values = $this->writeQueryValues($insert->getValues()); |
59
|
|
|
$table = $this->writer->writeTable($insert->getTable()); |
60
|
|
|
$comment = AbstractBaseWriter::writeQueryComment($insert); |
61
|
|
|
|
62
|
|
|
return $comment."INSERT INTO {$table} ($columns) VALUES ($values)"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $columns |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function writeQueryColumns($columns) |
71
|
|
|
{ |
72
|
|
|
return $this->writeCommaSeparatedValues($columns, $this->columnWriter, 'writeColumn'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $collection |
77
|
|
|
* @param $writer |
78
|
|
|
* @param string $method |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function writeCommaSeparatedValues($collection, $writer, $method) |
83
|
|
|
{ |
84
|
|
|
\array_walk( |
85
|
|
|
$collection, |
86
|
|
|
function (&$data) use ($writer, $method) { |
87
|
|
|
$data = $writer->$method($data); |
88
|
|
|
} |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return \implode(', ', $collection); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $values |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function writeQueryValues($values) |
100
|
|
|
{ |
101
|
|
|
return $this->writeCommaSeparatedValues($values, $this->writer, 'writePlaceholderValue'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|