Passed
Branch master (10fdc5)
by Beniamin
02:43
created

PDOConnection::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\UnderQuery\Connection;
13
14
use Phuria\UnderQuery\Parameter\QueryParameterInterface;
15
16
/**
17
 * @author Beniamin Jonatan Šimko <[email protected]>
18
 */
19
class PDOConnection implements ConnectionInterface
20
{
21
    /**
22
     * @var \PDO $wrappedConnection
23
     */
24
    private $wrappedConnection;
25
26
    /**
27
     * @param \PDO $connection
28
     */
29 3
    public function __construct(\PDO $connection)
30
    {
31 3
        $this->wrappedConnection = $connection;
32 3
    }
33
34
    /**
35
     * @param $SQL
36
     *
37
     * @return \PDOStatement
38
     */
39 3
    private function prepareStatement($SQL)
40
    {
41 3
        return $this->wrappedConnection->prepare($SQL);
42
    }
43
44
    /**
45
     * @param string                    $SQL
46
     * @param QueryParameterInterface[] $parameters
47
     *
48
     * @return \PDOStatement
49
     */
50 3
    private function getExecutedStatement($SQL, array $parameters = [])
51
    {
52 3
        $preparedStmt = $this->prepareStatement($SQL);
53
54 3
        foreach ($parameters as $parameter) {
55 1
            $preparedStmt->bindValue($parameter->getName(), $parameter->getValue());
56 3
        }
57
58 3
        $preparedStmt->execute();
59
60 3
        return $preparedStmt;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function execute($SQL, array $parameters = [])
67
    {
68 1
        $stmt = $this->getExecutedStatement($SQL, $parameters);
69
70 1
        return $stmt->rowCount();
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 1
    public function fetchScalar($SQL, array $parameters = [])
77
    {
78 1
        $stmt = $this->getExecutedStatement($SQL, $parameters);
79
80 1
        if (0 < $stmt->rowCount()) {
81 1
            return $stmt->fetch(\PDO::FETCH_COLUMN);
82
        }
83
84
        return null;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function fetchRow($SQL, array $parameters = [])
91
    {
92
        $stmt = $this->getExecutedStatement($SQL, $parameters);
93
94
        if (0 < $stmt->rowCount()) {
95
            return $stmt->fetch(\PDO::FETCH_ASSOC);
96
        }
97
98
        return [];
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104 1
    public function fetchAll($SQL, array $parameters = [])
105
    {
106 1
        $stmt = $this->getExecutedStatement($SQL, $parameters);
107
108 1
        if (0 < $stmt->rowCount()) {
109 1
            return $stmt->fetchAll(\PDO::FETCH_ASSOC);
110
        }
111
112
        return [];
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function rowCount($SQL, array $parameters = [])
119
    {
120
        $stmt = $this->getExecutedStatement($SQL, $parameters);
121
122
        return $stmt->rowCount() ?: 0;
123
    }
124
}