Completed
Push — master ( b97768...587712 )
by Bartko
31:36
created

Pdo::executeSQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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