1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\NativeQuery; |
7
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
8
|
|
|
use Rougin\Windstorm\ExecuteInterface; |
9
|
|
|
use Rougin\Windstorm\QueryInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Execute |
13
|
|
|
* |
14
|
|
|
* @package Windstorm |
15
|
|
|
* @author Rougin Gutib <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Execute implements ExecuteInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Doctrine\ORM\EntityManager |
21
|
|
|
*/ |
22
|
|
|
protected $manager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Doctrine\ORM\Query\ResultSetMapping|null |
26
|
|
|
*/ |
27
|
|
|
protected $mapping = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initializes the entity manager instance. |
31
|
|
|
* |
32
|
|
|
* @param \Doctrine\ORM\EntityManager $manager |
33
|
|
|
*/ |
34
|
6 |
|
public function __construct(EntityManager $manager) |
35
|
|
|
{ |
36
|
6 |
|
$this->manager = $manager; |
37
|
6 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the entity manager instance. |
41
|
|
|
* |
42
|
|
|
* @return \Doctrine\ORM\EntityManager |
43
|
|
|
*/ |
44
|
6 |
|
public function manager() |
45
|
|
|
{ |
46
|
6 |
|
return $this->manager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Executes an SQL statement with its bindings and types. |
51
|
|
|
* |
52
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
6 |
|
public function execute(QueryInterface $query) |
56
|
|
|
{ |
57
|
6 |
|
$bindings = $query->bindings(); |
58
|
|
|
|
59
|
6 |
|
$sql = $query->sql(); |
60
|
|
|
|
61
|
6 |
|
$types = (array) $query->types(); |
|
|
|
|
62
|
|
|
|
63
|
6 |
|
$connection = $this->manager->getConnection(); |
64
|
|
|
|
65
|
6 |
|
if (strpos($sql, 'SELECT') === false) |
66
|
6 |
|
{ |
67
|
|
|
return new Result($connection->executeUpdate($sql, $bindings, $types)); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
if ($this->mapping === null) |
71
|
6 |
|
{ |
72
|
3 |
|
return new Result($connection->executeQuery($sql, $bindings, $types)); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
$query = new NativeQuery($this->manager); |
76
|
|
|
|
77
|
3 |
|
$query->setSql($sql)->setResultSetMapping($this->mapping); |
78
|
|
|
|
79
|
3 |
|
foreach ($bindings as $key => $binding) |
80
|
|
|
{ |
81
|
3 |
|
$query->setParameter($key, $binding, $types[$key]); |
82
|
3 |
|
} |
83
|
|
|
|
84
|
3 |
|
return new Result((array) $query->getResult()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets the result set mapping instance. |
89
|
|
|
* |
90
|
|
|
* @param \Doctrine\ORM\Query\ResultSetMapping $mapping |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
3 |
|
public function mapping(ResultSetMapping $mapping) |
94
|
|
|
{ |
95
|
3 |
|
$this->mapping = $mapping; |
96
|
|
|
|
97
|
3 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|