1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace StefanoTree\NestedSet\Adapter; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection as DbConnection; |
8
|
|
|
use StefanoTree\NestedSet\Options; |
9
|
|
|
|
10
|
|
|
class Doctrine2DBAL implements AdapterInterface |
11
|
|
|
{ |
12
|
|
|
private $connection; |
13
|
|
|
private $options; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Options $options |
17
|
|
|
* @param DbConnection $connection |
18
|
|
|
*/ |
19
|
|
|
public function __construct(Options $options, DbConnection $connection) |
20
|
56 |
|
{ |
21
|
|
|
$this->connection = $connection; |
22
|
56 |
|
$this->options = $options; |
23
|
56 |
|
} |
24
|
56 |
|
|
25
|
|
|
/** |
26
|
|
|
* @return DbConnection |
27
|
|
|
*/ |
28
|
|
|
private function getConnection(): DbConnection |
29
|
56 |
|
{ |
30
|
|
|
return $this->connection; |
31
|
56 |
|
} |
32
|
56 |
|
|
33
|
|
|
/** |
34
|
|
|
* @return Options |
35
|
|
|
*/ |
36
|
|
|
public function getOptions(): Options |
37
|
53 |
|
{ |
38
|
|
|
return $this->options; |
39
|
53 |
|
} |
40
|
|
|
|
41
|
|
|
public function beginTransaction(): void |
42
|
|
|
{ |
43
|
|
|
$this->getConnection() |
44
|
|
|
->beginTransaction(); |
45
|
|
|
} |
46
|
|
|
|
47
|
32 |
|
public function commitTransaction(): void |
48
|
|
|
{ |
49
|
32 |
|
$this->getConnection() |
50
|
32 |
|
->commit(); |
51
|
|
|
} |
52
|
32 |
|
|
53
|
32 |
|
public function rollbackTransaction(): void |
54
|
|
|
{ |
55
|
32 |
|
$this->getConnection() |
56
|
|
|
->rollBack(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function isInTransaction(): bool |
60
|
|
|
{ |
61
|
|
|
return 0 === $this->getConnection()->getTransactionNestingLevel() ? false : true; |
62
|
|
|
} |
63
|
17 |
|
|
64
|
|
|
public function canHandleNestedTransaction(): bool |
65
|
17 |
|
{ |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function quoteIdentifier(string $columnName): string |
70
|
|
|
{ |
71
|
1 |
|
return $this->getConnection() |
72
|
|
|
->quoteIdentifier($columnName); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
public function executeInsertSQL(string $sql, array $params = array()) |
76
|
|
|
{ |
77
|
1 |
|
$options = $this->getOptions(); |
78
|
1 |
|
$this->executeSQL($sql, $params); |
79
|
|
|
|
80
|
1 |
|
if (array_key_exists($options->getIdColumnName(), $params)) { |
81
|
|
|
return $params[$options->getIdColumnName()]; |
82
|
1 |
|
} else { |
83
|
1 |
|
return $this->getConnection() |
84
|
|
|
->lastInsertId($options->getSequenceName()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function executeSQL(string $sql, array $params = array()): void |
89
|
|
|
{ |
90
|
1 |
|
$this->getConnection() |
91
|
1 |
|
->executeQuery($sql, $params); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
public function executeSelectSQL(string $sql, array $params = array()): array |
95
|
|
|
{ |
96
|
|
|
return $this->getConnection() |
97
|
1 |
|
->executeQuery($sql, $params) |
98
|
|
|
->fetchAll(); |
99
|
1 |
|
} |
100
|
|
|
} |
101
|
|
|
|