1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of UnderQuery package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\UnderQuery\Query; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; |
15
|
|
|
use Doctrine\DBAL\Driver\Statement as StatementInterface; |
16
|
|
|
use Doctrine\DBAL\Driver\ResultStatement as ResultStatementInterface; |
17
|
|
|
use Phuria\UnderQuery\Parameter\ParameterCollection; |
18
|
|
|
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Query implements QueryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $compiledSQL; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ParameterCollectionInterface |
32
|
|
|
*/ |
33
|
|
|
private $parameterCollection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ConnectionInterface|null |
37
|
|
|
*/ |
38
|
|
|
private $connection; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $compiledSQL |
42
|
|
|
* @param array $parameters |
43
|
|
|
* @param ConnectionInterface|null $connection |
44
|
|
|
*/ |
45
|
3 |
|
public function __construct($compiledSQL, array $parameters = [], ConnectionInterface $connection = null) |
46
|
|
|
{ |
47
|
3 |
|
$this->compiledSQL = $compiledSQL; |
48
|
3 |
|
$this->parameterCollection = new ParameterCollection($parameters); |
49
|
3 |
|
$this->connection = $connection; |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
public function getSQL() |
56
|
|
|
{ |
57
|
1 |
|
return $this->compiledSQL; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return ParameterCollectionInterface |
62
|
|
|
*/ |
63
|
2 |
|
public function getParameters() |
64
|
|
|
{ |
65
|
2 |
|
return $this->parameterCollection; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return ConnectionInterface|null |
70
|
|
|
*/ |
71
|
|
|
public function getConnection() |
72
|
|
|
{ |
73
|
|
|
return $this->connection; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int|string $name |
78
|
|
|
* @param mixed $value |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
1 |
|
public function setParameter($name, $value) |
83
|
|
|
{ |
84
|
1 |
|
$this->getParameters()->setValue($name, $value); |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return StatementInterface |
91
|
|
|
*/ |
92
|
|
|
public function prepareStatement() |
93
|
|
|
{ |
94
|
|
|
$stmt = $this->connection->prepare($this->getSQL()); |
95
|
|
|
|
96
|
|
|
foreach ($this->getParameters()->toArray() as $parameter) { |
97
|
|
|
$stmt->bindValue($parameter->getName(), $parameter->getValue()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $stmt; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ResultStatementInterface |
105
|
|
|
*/ |
106
|
|
|
public function getResult() |
107
|
|
|
{ |
108
|
|
|
$stmt = $this->prepareStatement(); |
109
|
|
|
$stmt->execute(); |
110
|
|
|
|
111
|
|
|
return $stmt; |
112
|
|
|
} |
113
|
|
|
} |