SetFields::setFieldByExpression()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Author: Adrian Dumitru
5
 * Date: 6/26/2017 6:22 AM
6
 */
7
8
namespace Qpdb\QueryBuilder\Traits;
9
10
11
use Qpdb\QueryBuilder\Dependencies\QueryStructure;
12
13
/**
14
 * Trait SetFields
15
 * @package Qpdb\QueryBuilder\Traits
16
 * @property QueryStructure $queryStructure
17
 */
18
trait SetFields
19
{
20
21
	private $setValues;
22
23
	/**
24
	 * @param $fieldName
25
	 * @param $fieldValue
26
	 * @return $this
27
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
28
	 */
29
	public function setField( $fieldName, $fieldValue )
30
	{
31
		$fieldName = $this->queryStructure->prepare($fieldName);
32
		$valuePdoString = $this->queryStructure->bindParam( $fieldName, $fieldValue );
33
		$this->queryStructure->setElement( QueryStructure::SET_FIELDS, $this->queryStructure->prepare($fieldName) . " = $valuePdoString" );
34
35
		return $this;
36
	}
37
38
	/**
39
	 * @param string $expression
40
	 * @return $this
41
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
42
	 */
43
	public function setFieldByExpression( $expression )
44
	{
45
		$this->queryStructure->setElement( QueryStructure::SET_FIELDS, $expression );
46
47
		return $this;
48
	}
49
50
	/**
51
	 * Set fields by associative array ( fieldName => fieldValue )
52
	 * @param array $updateFieldsArray
53
	 * @return $this
54
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
55
	 */
56
	public function setFieldsByArray( array $updateFieldsArray )
57
	{
58
		foreach ( $updateFieldsArray as $fieldName => $fieldValue )
59
			$this->setField( $fieldName, $fieldValue );
60
61
		return $this;
62
	}
63
64
	/**
65
	 * @return string
66
	 */
67
	private function getSettingFieldsSyntax()
68
	{
69
		if ( !count( $this->queryStructure->getElement( QueryStructure::SET_FIELDS ) ) )
70
			return '';
71
72
		return 'SET ' . implode( ', ', $this->queryStructure->getElement( QueryStructure::SET_FIELDS ) );
73
	}
74
75
}