1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
7
|
|
|
use Rougin\Windstorm\Doctrine\Builder\DeleteQuery; |
8
|
|
|
use Rougin\Windstorm\Doctrine\Builder\InsertQuery; |
9
|
|
|
use Rougin\Windstorm\Doctrine\Builder\SelectQuery; |
10
|
|
|
use Rougin\Windstorm\Doctrine\Builder\UpdateQuery; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Query Builder |
14
|
|
|
* |
15
|
|
|
* @package Windstorm |
16
|
|
|
* @author Rougin Gutib <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Builder extends QueryBuilder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform |
22
|
|
|
*/ |
23
|
|
|
protected $platform; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Initializes the platform instance. |
27
|
|
|
* |
28
|
|
|
* @param \Doctrine\DBAL\Connection|\Doctrine\DBAL\Platforms\AbstractPlatform $platform |
29
|
|
|
*/ |
30
|
108 |
|
public function __construct($platform) |
31
|
|
|
{ |
32
|
108 |
|
if ($platform instanceof Connection) |
33
|
72 |
|
{ |
34
|
32 |
|
$platform = $platform->getDatabasePlatform(); |
35
|
|
|
} |
36
|
|
|
|
37
|
108 |
|
$this->platform = $platform; |
38
|
108 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the complete SQL string. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
102 |
|
public function getSql() |
46
|
|
|
{ |
47
|
102 |
|
$first = $this->getFirstResult(); |
48
|
|
|
|
49
|
102 |
|
$max = $this->getMaxResults(); |
50
|
|
|
|
51
|
102 |
|
$parts = $this->getQueryParts(); |
52
|
|
|
|
53
|
102 |
|
switch ($this->getType()) { |
54
|
102 |
|
case self::INSERT: |
55
|
3 |
|
$sql = new InsertQuery($parts); |
56
|
|
|
|
57
|
3 |
|
break; |
58
|
99 |
|
case self::DELETE: |
59
|
6 |
|
$sql = new DeleteQuery($parts); |
60
|
|
|
|
61
|
6 |
|
break; |
62
|
93 |
|
case self::UPDATE: |
63
|
3 |
|
$sql = new UpdateQuery($parts); |
64
|
|
|
|
65
|
3 |
|
break; |
66
|
90 |
|
case self::SELECT: |
67
|
60 |
|
default: |
68
|
90 |
|
$sql = new SelectQuery($parts, $this->platform, $max, $first); |
69
|
|
|
|
70
|
90 |
|
break; |
71
|
68 |
|
} |
72
|
|
|
|
73
|
102 |
|
return (string) $sql->get(); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
public function set($key, $value) |
77
|
|
|
{ |
78
|
3 |
|
$parameters = $this->getParameters(); |
79
|
|
|
|
80
|
3 |
|
$index = count((array) $parameters); |
81
|
|
|
|
82
|
3 |
|
$this->setParameter($index, $value); |
83
|
|
|
|
84
|
3 |
|
return $this->add('set', $key . ' = ?', true); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
public function values(array $values) |
88
|
|
|
{ |
89
|
3 |
|
$index = 0; |
90
|
|
|
|
91
|
3 |
|
foreach ($values as $key => $value) |
92
|
|
|
{ |
93
|
3 |
|
$this->setParameter($index, $value); |
94
|
|
|
|
95
|
3 |
|
$index = $index + 1; |
96
|
|
|
|
97
|
3 |
|
$values[$key] = '?'; |
98
|
2 |
|
} |
99
|
|
|
|
100
|
3 |
|
return $this->add('values', $values); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|