Completed
Push — master ( c5e8a4...cf6653 )
by Sébastien
03:26
created

DbWrapper/Adapter/Doctrine/Dbal2Adapter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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