|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BfwSql\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class to write UPDATE queries |
|
9
|
|
|
* |
|
10
|
|
|
* @package bfw-sql |
|
11
|
|
|
* @author Vermeulen Maxime <[email protected]> |
|
12
|
|
|
* @version 2.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Update extends AbstractActions |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @const ERR_ASSEMBLE_NO_DATAS Exception code if no data to update. |
|
18
|
|
|
*/ |
|
19
|
|
|
const ERR_ASSEMBLE_NO_DATAS = 2305001; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor |
|
23
|
|
|
* |
|
24
|
|
|
* @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
|
25
|
|
|
* @param string $tableName The table name used for query |
|
26
|
|
|
* @param array $columns (default: null) Datas to add |
|
27
|
|
|
* Format is array('columnName' => 'value', ...); |
|
28
|
|
|
* @param string $quoteStatus (default: QUOTE_ALL) Status to automatic |
|
29
|
|
|
* quoted string value system. |
|
30
|
|
|
*/ |
|
31
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
32
|
|
|
\BfwSql\SqlConnect $sqlConnect, |
|
33
|
|
|
$tableName, |
|
34
|
|
|
$columns = null, |
|
35
|
|
|
$quoteStatus = \BfwSql\Actions\AbstractActions::QUOTE_ALL |
|
36
|
|
|
) { |
|
37
|
|
|
parent::__construct($sqlConnect); |
|
38
|
|
|
|
|
39
|
|
|
$this->quoteStatus = $quoteStatus; |
|
40
|
|
|
|
|
41
|
|
|
$prefix = $sqlConnect->getConnectionInfos()->tablePrefix; |
|
42
|
|
|
$this->tableName = $prefix.$tableName; |
|
43
|
|
|
|
|
44
|
|
|
if (is_array($columns)) { |
|
45
|
|
|
$this->columns = $columns; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function assembleRequest() |
|
53
|
|
|
{ |
|
54
|
|
|
if (count($this->columns) === 0) { |
|
55
|
|
|
throw new Exception( |
|
56
|
|
|
'SqlUpdate : no datas to update.', |
|
57
|
|
|
self::ERR_ASSEMBLE_NO_DATAS |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$lstColumns = ''; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->columns as $columnName => $columnValue) { |
|
64
|
|
|
if ($lstColumns !== '') { |
|
65
|
|
|
$lstColumns .= ','; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($columnValue === null) { |
|
69
|
|
|
$fieldValue = 'null'; |
|
70
|
|
|
} else { |
|
71
|
|
|
$fieldValue = $this->quoteValue($columnName, $columnValue); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$lstColumns .= '`'.$columnName.'`='.$fieldValue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->assembledRequest = 'UPDATE '.$this->tableName |
|
78
|
|
|
.' SET '.$lstColumns |
|
79
|
|
|
.$this->generateWhere(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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.