1
|
|
|
<?php |
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\Provider; |
3
|
|
|
|
4
|
|
|
use Doctrine\DBAL\Connection; |
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
7
|
|
|
use Doctrine\DBAL\Logging\EchoSQLLogger; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SqlProvider |
11
|
|
|
* |
12
|
|
|
* @author Florian Ajir <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class SqlProvider implements ProviderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Connection |
18
|
|
|
*/ |
19
|
|
|
protected $conn; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Connection $conn |
23
|
|
|
* @param string $env |
24
|
|
|
*/ |
25
|
8 |
|
public function __construct(Connection $conn, $env) |
26
|
|
|
{ |
27
|
8 |
|
$this->conn = $conn; |
28
|
8 |
|
if ($env !== 'prod') { |
29
|
1 |
|
$this->conn->getConfiguration()->setSQLLogger(new EchoSQLLogger()); |
30
|
|
|
} |
31
|
8 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $table |
35
|
|
|
* @param array $conditions |
36
|
|
|
* |
37
|
|
|
* @return integer The number of affected rows. |
38
|
|
|
* |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
*/ |
41
|
1 |
|
public function delete($table, $conditions) |
42
|
|
|
{ |
43
|
1 |
|
return $this->conn->delete($table, $conditions); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $table |
48
|
|
|
* @param array $data |
49
|
|
|
* @param array $identifier |
50
|
|
|
* |
51
|
|
|
* @return integer last insert id |
52
|
|
|
*/ |
53
|
2 |
|
public function insertOrUpdateIfExists($table, array $data, array $identifier = null) |
54
|
|
|
{ |
55
|
2 |
|
if (!empty($identifier)) { |
56
|
1 |
|
$exists = $this->exists($table, key($identifier), current($identifier)); |
57
|
1 |
|
if ($exists) { |
58
|
1 |
|
return $this->update($table, $data, $identifier); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $this->insert($table, $data); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $table |
67
|
|
|
* @param string $identifier |
68
|
|
|
* @param string $value |
69
|
|
|
* |
70
|
|
|
* @return boolean |
71
|
|
|
* |
72
|
|
|
* @throws DBALException |
73
|
|
|
*/ |
74
|
2 |
|
public function exists($table, $identifier, $value) |
75
|
|
|
{ |
76
|
2 |
|
$sth = $this->conn->executeQuery("SELECT count(*) FROM `{$table}` WHERE `{$identifier}` = '{$value}'"); |
77
|
2 |
|
$exist = $sth->fetchColumn(); |
78
|
|
|
|
79
|
2 |
|
return !empty($exist); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $table |
84
|
|
|
* @param array $data |
85
|
|
|
* @param array $identifier |
86
|
|
|
* |
87
|
|
|
* @return integer last insert id |
88
|
|
|
*/ |
89
|
2 |
|
public function update($table, array $data, array $identifier) |
90
|
|
|
{ |
91
|
2 |
|
$data = $this->quoteIdentifiers($data); |
92
|
2 |
|
$this->conn->update($table, $data, $identifier); |
93
|
|
|
|
94
|
2 |
|
return $this->conn->lastInsertId(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $data |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
4 |
|
private function quoteIdentifiers($data) |
103
|
|
|
{ |
104
|
4 |
|
$prepared = array(); |
105
|
4 |
|
foreach ($data as $id => $value) { |
106
|
4 |
|
$quoted = $this->conn->quoteIdentifier($id); |
107
|
4 |
|
$prepared[$quoted] = $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
return $prepared; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $table |
115
|
|
|
* @param array $data |
116
|
|
|
* |
117
|
|
|
* @return integer last insert id |
118
|
|
|
*/ |
119
|
2 |
|
public function insert($table, array $data) |
120
|
|
|
{ |
121
|
2 |
|
$data = $this->quoteIdentifiers($data); |
122
|
2 |
|
$this->conn->insert($table, $data); |
123
|
|
|
|
124
|
2 |
|
return $this->conn->lastInsertId(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $table |
129
|
|
|
* @param string $column |
130
|
|
|
* @param string $where |
131
|
|
|
* @param string $value |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
* |
135
|
|
|
* @throws DBALException |
136
|
|
|
*/ |
137
|
1 |
|
public function getColumnValueWhere($table, $column, $where, $value) |
138
|
|
|
{ |
139
|
1 |
|
$sth = $this->conn->executeQuery("SELECT `{$column}` FROM `{$table}` WHERE `{$where}` = '{$value}'"); |
140
|
|
|
|
141
|
1 |
|
return $sth->fetchColumn(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|