Completed
Pull Request — master (#1)
by Joao
04:01 queued 01:28
created

DbPdoDriver::getAllFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace ByJG\AnyDataset\Database;
4
5
use ByJG\AnyDataset\ConnectionManagement;
6
use ByJG\AnyDataset\Exception\NotAvailableException;
7
use ByJG\AnyDataset\Repository\DBIterator;
8
use PDO;
9
use PDOStatement;
10
11
class DbPdoDriver implements DbDriverInterface
12
{
13
14
    /**
15
     * @var PDO
16
     */
17
    protected $instance = null;
18
19
    /**
20
     * @var ConnectionManagement
21
     */
22
    protected $connectionManagement;
23
24
    public function __construct(ConnectionManagement $connMngt, $strcnn, $preOptions, $postOptions)
25
    {
26
        $this->connectionManagement = $connMngt;
27
28
        if (is_null($strcnn)) {
29
            if ($this->connectionManagement->getFilePath() != "") {
30
                $strcnn = $this->connectionManagement->getDriver() . ":" . $this->connectionManagement->getFilePath();
31
            } else {
32
                $strcnn = $this->connectionManagement->getDriver() . ":dbname=" .
33
                    $this->connectionManagement->getDatabase();
34
                if ($this->connectionManagement->getExtraParam("unixsocket") != "") {
35
                    $strcnn .= ";unix_socket=" . $this->connectionManagement->getExtraParam("unixsocket");
36
                } else {
37
                    $strcnn .= ";host=" . $this->connectionManagement->getServer();
38
                    if ($this->connectionManagement->getPort() != "") {
39
                        $strcnn .= ";port=" . $this->connectionManagement->getPort();
40
                    }
41
                }
42
            }
43
        }
44
45
        // Create Connection
46
        $this->instance = new PDO(
47
            $strcnn,
48
            $this->connectionManagement->getUsername(),
49
            $this->connectionManagement->getPassword(),
50
            (array) $preOptions
51
        );
52
        $this->connectionManagement->setDriver($this->instance->getAttribute(PDO::ATTR_DRIVER_NAME));
53
54
        // Set Specific Attributes
55
        $this->instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
56
        $this->instance->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
57
58
        foreach ((array) $postOptions as $key => $value) {
59
            $this->instance->setAttribute($key, $value);
60
        }
61
    }
62
63
    public static function factory(ConnectionManagement $connMngt)
64
    {
65
        if (!defined('PDO::ATTR_DRIVER_NAME')) {
66
            throw new NotAvailableException("Extension 'PDO' is not loaded");
67
        }
68
69
        if (!extension_loaded('pdo_' . strtolower($connMngt->getDriver()))) {
70
            throw new NotAvailableException("Extension 'pdo_" . strtolower($connMngt->getDriver()) . "' is not loaded");
71
        }
72
73
        $class = '\ByJG\AnyDataset\Database\Pdo' . ucfirst($connMngt->getDriver());
74
75
        if (!class_exists($class, true)) {
76
            return new DbPdoDriver($connMngt, null, null, null);
77
        } else {
78
            return new $class($connMngt);
79
        }
80
    }
81
82
    public function __destruct()
83
    {
84
        $this->instance = null;
85
    }
86
87
    /**
88
     *
89
     * @param string $sql
90
     * @param array $array
91
     * @return PDOStatement
92
     */
93
    protected function getDBStatement($sql, $array = null)
94
    {
95
        if ($array) {
96
            list($sql, $array) = SqlBind::parseSQL($this->connectionManagement, $sql, $array);
97
            $stmt = $this->instance->prepare($sql);
98
            foreach ($array as $key => $value) {
99
                $stmt->bindValue(":" . SqlBind::keyAdj($key), $value);
100
            }
101
        } else {
102
            $stmt = $this->instance->prepare($sql);
103
        }
104
105
        return $stmt;
106
    }
107
108
    public function getIterator($sql, $array = null)
109
    {
110
        $stmt = $this->getDBStatement($sql, $array);
111
        $stmt->execute();
112
        $iterator = new DBIterator($stmt);
113
        return $iterator;
114
    }
115
116
    public function getScalar($sql, $array = null)
117
    {
118
        $stmt = $this->getDBStatement($sql, $array);
119
        $stmt->execute();
120
121
        $scalar = $stmt->fetchColumn();
122
123
        $stmt->closeCursor();
124
125
        return $scalar;
126
    }
127
128
    public function getAllFields($tablename)
129
    {
130
        $fields = array();
131
        $statement = $this->instance->query(
132
            SqlHelper::createSafeSQL(
133
                "select * from :table where 0=1",
134
                [
135
                    ":table" => $tablename
136
                ]
137
            )
138
        );
139
        $fieldLength = $statement->columnCount();
140
        for ($i = 0; $i < $fieldLength; $i++) {
141
            $fld = $statement->getColumnMeta($i);
142
            $fields [] = strtolower($fld ["name"]);
143
        }
144
        return $fields;
145
    }
146
147
    public function beginTransaction()
148
    {
149
        $this->instance->beginTransaction();
150
    }
151
152
    public function commitTransaction()
153
    {
154
        $this->instance->commit();
155
    }
156
157
    public function rollbackTransaction()
158
    {
159
        $this->instance->rollBack();
160
    }
161
162
    public function executeSql($sql, $array = null)
163
    {
164
        $stmt = $this->getDBStatement($sql, $array);
165
        $result = $stmt->execute();
166
        return $result;
167
    }
168
169
    /**
170
     *
171
     * @return PDO
172
     */
173
    public function getDbConnection()
174
    {
175
        return $this->instance;
176
    }
177
178
    public function getAttribute($name)
179
    {
180
        $this->instance->getAttribute($name);
181
    }
182
183
    public function setAttribute($name, $value)
184
    {
185
        $this->instance->setAttribute($name, $value);
186
    }
187
}
188