QueryManagerDb::fetchAll()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
/**
3
 * Query Manager
4
 * Copyright (c) Webmatch GmbH
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 */
16
17
namespace WbmQueryManager\Services;
18
19
use Shopware\Components\DependencyInjection\Container;
20
21
if (!class_exists('Zend_Db_Adapter_Mysqli')) {
22
    require_once __DIR__ . '/../Library/Zend/Db/Adapter/Mysqli.php';
23
    require_once __DIR__ . '/../Library/Zend/Db/Adapter/Mysqli/Exception.php';
24
    require_once __DIR__ . '/../Library/Zend/Db/Statement/Mysqli.php';
25
    require_once __DIR__ . '/../Library/Zend/Db/Statement/Mysqli/Exception.php';
26
}
27
28
/**
29
 * Class QueryManagerDb
30
 * @package WbmQueryManager\Services
31
 */
32
class QueryManagerDb implements QueryManagerDbInterface {
33
34
    /**
35
     * @var Container
36
     */
37
    private $container;
38
39
    /**
40
     * @var \Enlight_Components_Db_Adapter_Pdo_Mysql
41
     */
42
    private $db;
43
44
    /**
45
     * QueryManagerDb constructor.
46
     * @param Container $container
47
     * @param \Enlight_Components_Db_Adapter_Pdo_Mysql $db
48
     */
49
    public function __construct(Container $container, \Enlight_Components_Db_Adapter_Pdo_Mysql $db)
50
    {
51
        $this->container = $container;
52
        $this->db = $db;
53
    }
54
55
    /**
56
     * @param $query
57
     * @return \Enlight_Components_Db_Adapter_Pdo_Mysql|\mysqli
58
     */
59
    private function getConnection($query)
60
    {
61
        if(function_exists('mysqli_connect')) {
62
            $config = $this->container->getParameter('shopware.db');
63
            $mysqli = new \Zend_Db_Adapter_Mysqli($config);
64
            $connection = $mysqli->getConnection();
65
            if($connection->multi_query($query)){
66
                return $connection;
67
            }
68
        }
69
        return $this->db;
70
    }
71
72
    /**
73
     * @param $query
74
     * @return \mysqli|\Zend_Db_Statement_Pdo
75
     */
76
    public function query($query)
77
    {
78
        $connection = $this->getConnection($query);
79
        if(get_class($connection) == 'mysqli'){
80
            return $connection;
81
        }
82
        return $connection->query($query);
83
    }
84
85
    /**
86
     * @param \mysqli|\Zend_Db_Statement_Pdo $query
87
     * @return int
88
     */
89
    public function getRowCount($query)
90
    {
91
        if(get_class($query) == 'mysqli'){
92
            return $query->affected_rows;
93
        }
94
        return $query->rowCount();
95
    }
96
97
    /**
98
     * @param \mysqli|\Zend_Db_Statement_Pdo $query
99
     * @return int
100
     */
101
    public function getColumnCount($query)
102
    {
103
        if(get_class($query) == 'mysqli'){
104
            return $query->field_count;
105
        }
106
        return $query->columnCount();
107
    }
108
109
    /**
110
     * @param \mysqli|\Zend_Db_Statement_Pdo $query
111
     * @return array
112
     */
113
    public function fetchAll($query)
114
    {
115
        if(get_class($query) == 'mysqli'){
116
            $results = array();
117
            if($result = $query->store_result()){
118
                while($row = $result->fetch_assoc()){
119
                    $results[] = $row;
120
                }
121
            }
122
            return $results;
123
        }
124
        return $query->fetchAll();
125
    }
126
127
    /**
128
     * @param \mysqli|\Zend_Db_Statement_Pdo $query
129
     * @return bool
130
     */
131
    public function nextResult($query)
132
    {
133
        if(get_class($query) == 'mysqli'){
134
            return $query->next_result();
135
        }
136
        return false;
137
    }
138
139
    /**
140
     * @param \mysqli|\Zend_Db_Statement_Pdo $query
141
     * @return bool
142
     */
143
    public function close($query)
144
    {
145
        if(get_class($query) == 'mysqli'){
146
            return $query->close();
147
        } else {
148
            $query->closeCursor();
149
        }
150
        return true;
151
    }
152
153
}