Completed
Push — master ( a66c95...c5e8a4 )
by Sébastien
03:49
created

Dbal2Adapter::quoteValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter\Doctrine;
4
5
use Soluble\DbWrapper\Exception;
6
use Soluble\DbWrapper\Result\Resultset;
7
use Soluble\DbWrapper\Adapter\AdapterInterface;
8
use Soluble\DbWrapper\Connection\Doctrine\Dbal2Connection;
9
10
class Dbal2Adapter implements AdapterInterface
11
{
12
    /**
13
     * @var \Doctrine\DBAL\Connection
14
     */
15
    protected $dbal;
16
17
    /**
18
     * @var Dbal2Connection
19
     */
20
    protected $connection;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param \Doctrine\DBAL\Connection $dbal
26
     */
27 6
    public function __construct(\Doctrine\DBAL\Connection $dbal)
28
    {
29 6
        $this->dbal = $dbal;
30 6
        $this->connection = new Dbal2Connection($this, $dbal);
31 6
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function quoteValue($value)
37
    {
38 1
        return $this->dbal->quote($value);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function query($query, $resultsetType = Resultset::TYPE_ARRAY)
45
    {
46
        // This error may happen with the libmysql instead of mysqlnd and using set statement (set @test=1)
47
        // : "Attempt to read a row while there is no result set associated with the statement"
48
49
        try {
50
            /**
51
             * @var \Doctrine\DBAL\Driver\Mysqli\MysqliStatement
52
             */
53 3
            $r = $this->dbal->query($query);
54
55 3
            $results = new Resultset($resultsetType);
56 3
            if ($r->columnCount() > 0) {
57 2
                while ($row = $r->fetch()) {
58 2
                    $results->append((array) $row);
59 2
                }
60 2
            }
61
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
62 3
        } catch (\Exception $e) {
63 2
            $msg = "Doctrine\Dbal2 adapter query error: {$e->getMessage()} [$query]";
64 2
            throw new Exception\InvalidArgumentException($msg);
65
        }
66
67 3
        return $results;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @return Dbal2Connection
74
     */
75 1
    public function getConnection()
76
    {
77 1
        return $this->connection;
78
    }
79
}
80