1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Yolk - Gamer Network's PHP Framework. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2013 Gamer Network Ltd. |
6
|
|
|
* |
7
|
|
|
* Distributed under the MIT License, a copy of which is available in the |
8
|
|
|
* LICENSE file that was bundled with this package, or online at: |
9
|
|
|
* https://github.com/gamernetwork/yolk-database |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace yolk\database\query; |
13
|
|
|
|
14
|
|
|
use yolk\contracts\database\DatabaseConnection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Generic. |
18
|
|
|
*/ |
19
|
|
|
class Update extends BaseQuery { |
20
|
|
|
|
21
|
|
|
protected $table; |
22
|
|
|
protected $set; |
23
|
|
|
|
24
|
|
|
public function __construct( DatabaseConnection $db ) { |
25
|
|
|
parent::__construct($db); |
26
|
|
|
$this->table = ''; |
27
|
|
|
$this->set = []; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function table( $table ) { |
31
|
|
|
$this->table = $this->quoteIdentifier($table); |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function set( array $data, $replace = false ) { |
|
|
|
|
36
|
|
|
|
37
|
|
|
if( $replace ) |
38
|
|
|
$this->set = []; |
39
|
|
|
|
40
|
|
|
$this->set = array_merge($this->set, $data); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function execute() { |
47
|
|
|
return $this->db->execute( |
48
|
|
|
$this->__toString(), |
49
|
|
|
$this->params |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function compile() { |
54
|
|
|
|
55
|
|
|
return array_merge( |
56
|
|
|
[ |
57
|
|
|
"UPDATE {$this->table}", |
58
|
|
|
], |
59
|
|
|
$this->compileSet(), |
60
|
|
|
$this->compileWhere(), |
61
|
|
|
$this->compileOrderBy(), |
62
|
|
|
$this->compileLimit() |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function compileSet() { |
68
|
|
|
|
69
|
|
|
$sql = []; |
70
|
|
|
$end = -1; |
71
|
|
|
|
72
|
|
|
foreach( $this->set as $column => $value ) { |
73
|
|
|
$this->bindParam($column, $value); |
74
|
|
|
$sql[] = "{$column} = :{$column},"; |
75
|
|
|
$end++; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$sql[0] = 'SET '. $sql[0]; |
79
|
|
|
$sql[$end] = trim($sql[$end], ','); |
80
|
|
|
|
81
|
|
|
return $sql; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
protected function compileLimit() { |
|
|
|
|
86
|
|
|
|
87
|
|
|
$sql = []; |
88
|
|
|
|
89
|
|
|
if( $this->limit ) { |
|
|
|
|
90
|
|
|
$sql[] = "LIMIT :limit"; |
91
|
|
|
$this->params['limit'] = $this->limit; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $sql; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// EOF |
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.