Completed
Push — master ( 20edd5...7d7f4e )
by smiley
02:32
created

UpdateAbstract::closeBracket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class UpdateAbstract
4
 *
5
 * @filesource   UpdateAbstract.php
6
 * @created      03.06.2017
7
 * @package      chillerlan\Database\Query\Dialects
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query;
14
15
use chillerlan\Database\Query\Traits\WhereTrait;
16
17
abstract class UpdateAbstract extends StatementAbstract implements UpdateInterface{
18
	use WhereTrait;
19
20
	/**
21
	 * @var string
22
	 */
23
	protected $table;
24
25
	/**
26
	 * @var array
27
	 */
28
	protected $set = [];
29
30
	/**
31
	 * @return string
32
	 * @throws \chillerlan\Database\Query\QueryException
33
	 */
34
	public function sql():string{
35
36
		if(empty($this->table)){
37
			throw new QueryException('no table specified');
38
		}
39
40
		if(empty($this->set)){
41
			throw new QueryException('no fields to update specified');
42
		}
43
44
		$sql  = 'UPDATE ';
45
		$sql .= $this->quote($this->table);
46
		$sql .= ' SET ';
47
		$sql .= implode(', ', $this->set);
48
		$sql .= $this->_getWhere();
49
50
		return $sql;
51
	}
52
53
	/**
54
	 * @param string|null $tablename
55
	 *
56
	 * @return \chillerlan\Database\Query\UpdateInterface
57
	 */
58
	public function table(string $tablename):UpdateInterface{
59
		$this->table = $tablename;
60
61
		return $this;
62
	}
63
64
	/**
65
	 * @param array $set
66
	 * @param bool  $bind
67
	 *
68
	 * @return \chillerlan\Database\Query\UpdateInterface
69
	 */
70
	public function set(array $set, bool $bind = true):UpdateInterface{
71
72
		foreach($set as $k => $v){
73
74
			if($v instanceof StatementInterface){
75
				$this->set[] = $this->quote($k).' = ('.$v->sql().')';
76
				$this->bindValues = array_merge($this->bindValues, $v->bindValues());
77
			}
78
			elseif(is_array($v)){
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
79
				// [expr, bindval1, bindval2, ...]
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
			}
81
			else{
82
				if($bind){
83
					$this->set[] = $this->quote($k).' = ?';
84
					$this->bindValues[] = $v;
85
				}
86
				else{
87
					$this->set[] = $this->quote($k).' = '.$v;
88
				}
89
			}
90
		}
91
92
		return $this;
93
	}
94
95
	/**
96
	 * @param        $val1
97
	 * @param        $val2
98
	 * @param string $operator
99
	 * @param bool   $bind
100
	 * @param string $join
101
	 *
102
	 * @return \chillerlan\Database\Query\UpdateInterface
103
	 */
104
	public function where($val1, $val2, $operator = '=', $bind = true, $join = 'AND'):UpdateInterface{
105
		return $this->_addWhere($val1, $val2, $operator, $bind, $join);
106
	}
107
108
	/**
109
	 * @param null $join
110
	 *
111
	 * @return \chillerlan\Database\Query\UpdateInterface
112
	 */
113
	public function openBracket($join = null):UpdateInterface{
114
		return $this->_openBracket($join); // @codeCoverageIgnore
115
	}
116
117
	/**
118
	 * @return \chillerlan\Database\Query\UpdateInterface
119
	 */
120
	public function closeBracket():UpdateInterface{
121
		return $this->_closeBracket(); // @codeCoverageIgnore
122
	}
123
124
}
125