Completed
Push — master ( 1857e6...d96bec )
by Joao
03:02
created

src/Database/DBPDODriver.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 ByJG\AnyDataset\Database;
4
5
use ByJG\AnyDataset\ConnectionManagement;
6
use ByJG\AnyDataset\Repository\DBIterator;
7
use PDO;
8
use PDOStatement;
9
10
class DBPDODriver implements DBDriverInterface
11
{
12
13
    /**
14
     * @var PDO
15
     */
16
    protected $_db = null;
17
18
    /**
19
     * @var ConnectionManagement
20
     */
21
    protected $_connectionManagement;
22
23
    public function __construct(ConnectionManagement $connMngt, $strcnn, $preOptions, $postOptions)
24
    {
25
        $this->_connectionManagement = $connMngt;
26
27
        if (is_null($strcnn)) {
28
            if ($this->_connectionManagement->getFilePath() != "") {
29
                $strcnn = $this->_connectionManagement->getDriver() . ":" . $this->_connectionManagement->getFilePath();
30
            } else {
31
                $strcnn = $this->_connectionManagement->getDriver() . ":dbname=" . $this->_connectionManagement->getDatabase();
32
                if ($this->_connectionManagement->getExtraParam("unixsocket") != "") {
33
                    $strcnn .= ";unix_socket=" . $this->_connectionManagement->getExtraParam("unixsocket");
34
                } else {
35
                    $strcnn .= ";host=" . $this->_connectionManagement->getServer();
36
                    if ($this->_connectionManagement->getPort() != "") {
37
                        $strcnn .= ";port=" . $this->_connectionManagement->getPort();
38
                    }
39
                }
40
            }
41
        }
42
43
        // Create Connection
44
        $this->_db = new PDO($strcnn, $this->_connectionManagement->getUsername(),
45
            $this->_connectionManagement->getPassword(), (array) $preOptions);
46
        $this->_connectionManagement->setDriver($this->_db->getAttribute(PDO::ATTR_DRIVER_NAME));
47
48
        // Set Specific Attributes
49
        $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
50
        $this->_db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
51
52
        foreach ((array) $postOptions as $key => $value) {
53
            $this->_db->setAttribute($key, $value);
54
        }
55
    }
56
57
    public static function factory(ConnectionManagement $connMngt)
58
    {
59
        if (!defined('PDO::ATTR_DRIVER_NAME')) {
60
            throw new \ByJG\AnyDataset\Exception\NotAvailableException("Extension 'PDO' is not loaded");
61
        }
62
63
        if (!extension_loaded('pdo_' . strtolower($connMngt->getDriver()))) {
64
            throw new \ByJG\AnyDataset\Exception\NotAvailableException("Extension 'pdo_" . strtolower($connMngt->getDriver()) . "' is not loaded");
65
        }
66
67
        $class = '\ByJG\AnyDataset\Database\Pdo' . ucfirst($connMngt->getDriver());
68
69
        if (!class_exists($class, true)) {
70
            return new DBPDODriver($connMngt, null, null, null);
71
        } else {
72
            return new $class($connMngt);
73
        }
74
    }
75
76
    public function __destruct()
77
    {
78
        $this->_db = null;
79
    }
80
81
    /**
82
     *
83
     * @param string $sql
84
     * @param array $array
85
     * @return PDOStatement
86
     */
87
    protected function getDBStatement($sql, $array = null)
88
    {
89
        if ($array) {
90
            list($sql, $array) = SQLBind::parseSQL($this->_connectionManagement, $sql, $array);
91
            $stmt = $this->_db->prepare($sql);
92
            foreach ($array as $key => $value) {
93
                $stmt->bindValue(":" . SQLBind::keyAdj($key), $value);
94
            }
95
        } else $stmt = $this->_db->prepare($sql);
96
97
        return $stmt;
98
    }
99
100
    public function getIterator($sql, $array = null)
101
    {
102
        $stmt = $this->getDBStatement($sql, $array);
103
        $stmt->execute();
104
        $it = new DBIterator($stmt);
0 ignored issues
show
$stmt is of type object<PDOStatement>, but the function expects a object<ByJG\AnyDataset\Repository\PDOStatement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
        return $it;
106
    }
107
108
    public function getScalar($sql, $array = null)
109
    {
110
        $stmt = $this->getDBStatement($sql, $array);
111
        $stmt->execute();
112
113
        $scalar = $stmt->fetchColumn();
114
115
        $stmt->closeCursor();
116
117
        return $scalar;
118
    }
119
120
    public function getAllFields($tablename)
121
    {
122
        $fields = array();
123
        $rs = $this->_db->query(SQLHelper::createSafeSQL("select * from :table where 0=1", array(":table" => $tablename)));
124
        $fieldLength = $rs->columnCount();
125
        for ($i = 0; $i < $fieldLength; $i++) {
126
            $fld = $rs->getColumnMeta($i);
127
            $fields [] = strtolower($fld ["name"]);
128
        }
129
        return $fields;
130
    }
131
132
    public function beginTransaction()
133
    {
134
        $this->_db->beginTransaction();
135
    }
136
137
    public function commitTransaction()
138
    {
139
        $this->_db->commit();
140
    }
141
142
    public function rollbackTransaction()
143
    {
144
        $this->_db->rollBack();
145
    }
146
147
    public function executeSql($sql, $array = null)
148
    {
149
        $stmt = $this->getDBStatement($sql, $array);
150
        $result = $stmt->execute();
151
        return $result;
152
    }
153
154
    /**
155
     * 
156
     * @return PDO
157
     */
158
    public function getDbConnection()
159
    {
160
        return $this->_db;
161
    }
162
163
    public function getAttribute($name)
164
    {
165
        $this->_db->getAttribute($name);
166
    }
167
168
    public function setAttribute($name, $value)
169
    {
170
        $this->_db->setAttribute($name, $value);
171
    }
172
}
173