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

ZendDb2Adapter::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter\Zend;
4
5
use Soluble\DbWrapper\Exception;
6
use Soluble\DbWrapper\Result\Resultset;
7
use Soluble\DbWrapper\Adapter\AdapterInterface;
8
use Soluble\DbWrapper\Connection\Zend\ZendDb2Connection;
9
10
class ZendDb2Adapter implements AdapterInterface
11
{
12
13
    /**
14
     *
15
     * @var \Zend\Db\Adapter\Adapter
16
     */
17
    protected $zendAdapter;
18
19
    /**
20
     *
21
     * @var ZendDb2Connection
22
     */
23
    protected $connection;
24
25
26
    /**
27
     * Constructor
28
     *
29
     * @param \Zend\Db\Adapter\Adapter $zendAdapter
30
     */
31
    public function __construct(\Zend\Db\Adapter\Adapter $zendAdapter)
32
    {
33
        $this->zendAdapter = $zendAdapter;
34
        $this->connection = new ZendDb2Connection($this, $zendAdapter);
35
    }
36
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function quoteValue($value)
42
    {
43
        return $this->zendAdapter->getPlatform()->quoteValue($value);
44
    }
45
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function query($query)
51
    {
52
        try {
53
            $r = $this->zendAdapter->query($query, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
54
            $results = new Resultset();
55
            if ($r === false) {
56
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
57
            } elseif ($r instanceof \Zend\Db\ResultSet\ResultSet) {
58
                foreach ($r as $row) {
59
                    $results->append((array) $row);
60
                }
61
            }
62
        } catch (\Exception $e) {
63
            $msg = "ZendDb2 adapter query error: {$e->getMessage()} [$query]";
64
            throw new Exception\InvalidArgumentException($msg);
65
        }
66
        return $results;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     * @return ZendDb2Connection
72
     */
73
    public function getConnection()
74
    {
75
        return $this->connection;
76
    }
77
}
78