|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BfwSql\Actions; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class to write INSERT INTO queries |
|
7
|
|
|
* |
|
8
|
|
|
* @package bfw-sql |
|
9
|
|
|
* @author Vermeulen Maxime <[email protected]> |
|
10
|
|
|
* @version 2.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class Insert extends AbstractActions |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor |
|
16
|
|
|
* |
|
17
|
|
|
* @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
|
18
|
|
|
* @param string $tableName The table name used for query |
|
19
|
|
|
* @param array $columns (default: null) Datas to add |
|
20
|
|
|
* Format is array('columnName' => 'value', ...); |
|
21
|
|
|
* @param string $quoteStatus (default: QUOTE_ALL) Status to automatic |
|
22
|
|
|
* quoted string value system. |
|
23
|
|
|
*/ |
|
24
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
25
|
|
|
\BfwSql\SqlConnect $sqlConnect, |
|
26
|
|
|
$tableName, |
|
27
|
|
|
$columns = null, |
|
28
|
|
|
$quoteStatus = \BfwSql\Actions\AbstractActions::QUOTE_ALL |
|
29
|
|
|
) { |
|
30
|
|
|
parent::__construct($sqlConnect); |
|
31
|
|
|
|
|
32
|
|
|
$this->quoteStatus = $quoteStatus; |
|
33
|
|
|
|
|
34
|
|
|
$prefix = $sqlConnect->getConnectionInfos()->tablePrefix; |
|
35
|
|
|
$this->tableName = $prefix.$tableName; |
|
36
|
|
|
|
|
37
|
|
|
if (is_array($columns)) { |
|
38
|
|
|
$this->addDatasForColumns($columns); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function assembleRequest() |
|
46
|
|
|
{ |
|
47
|
|
|
$lstColumns = ''; |
|
48
|
|
|
$lstValues = ''; |
|
49
|
|
|
$indexReadList = -1; |
|
50
|
|
|
|
|
51
|
|
|
foreach ($this->columns as $columnName => $columnValue) { |
|
52
|
|
|
$indexReadList++; |
|
53
|
|
|
if ($indexReadList > 0) { |
|
54
|
|
|
$lstColumns .= ','; |
|
55
|
|
|
$lstValues .= ','; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$lstColumns .= '`'.$columnName.'`'; |
|
59
|
|
|
|
|
60
|
|
|
if ($columnValue === null) { |
|
61
|
|
|
$lstValues .= 'null'; |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$lstValues .= $this->quoteValue($columnName, $columnValue); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->assembledRequest = 'INSERT INTO '.$this->tableName; |
|
69
|
|
|
|
|
70
|
|
|
if ($this->columns !== []) { |
|
71
|
|
|
$this->assembledRequest .= ' ('.$lstColumns.')' |
|
72
|
|
|
.' VALUES ('.$lstValues.')'; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.