Completed
Push — master ( 58c9ab...b3ba9b )
by Sébastien
19:11 queued 01:31
created

Dbal2Adapter::query()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.439
cc 5
eloc 14
nc 10
nop 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
    /**
14
     *
15
     * @var \Doctrine\DBAL\Connection
16
     */
17
    protected $dbal;
18
19
20
    /**
21
     *
22
     * @var Dbal2Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param \Doctrine\DBAL\Connection $dbal
30
     */
31
    public function __construct(\Doctrine\DBAL\Connection $dbal)
32
    {
33
        $this->dbal = $dbal;
34
        $this->connection = new Dbal2Connection($this, $dbal);
35
    }
36
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function quoteValue($value)
42
    {
43
        return $this->dbal->quote($value);
44
    }
45
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function query($query)
51
    {
52
        // This error may happen with the libmysql instead of mysqlnd and using set statement (set @test=1)
53
        // : "Attempt to read a row while there is no result set associated with the statement"
54
55
        try {
56
57
            /**
58
             * @var \Doctrine\DBAL\Driver\Mysqli\MysqliStatement
59
             */
60
            $r = $this->dbal->query($query);
61
62
            $results = new Resultset();
63
            if ($r === false) {
64
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
65
            } else {
66
                if ($r->columnCount() > 0) {
67
                    while ($row = $r->fetch()) {
68
                        $results->append((array) $row);
69
                    }
70
                }
71
            }
72
        } catch (\Exception $e) {
73
            $msg = "Doctrine\Dbal2 adapter query error: {$e->getMessage()} [$query]";
74
            throw new Exception\InvalidArgumentException($msg);
75
        }
76
        return $results;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     * @return Dbal2Connection
82
     */
83
    public function getConnection()
84
    {
85
        return $this->connection;
86
    }
87
}
88