Passed
Push — master ( 5379a4...a2888f )
by Joao
04:49
created

src/Database/DBOci8Driver.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\Exception\DatabaseException;
7
use ByJG\AnyDataset\Repository\Oci8Iterator;
8
9
class DBOci8Driver implements DBDriverInterface
10
{
11
12
    /**
13
     * Enter description here...
14
     *
15
     * @var ConnectionManagement
16
     */
17
    protected $_connectionManagement;
18
19
    /** Used for OCI8 connections * */
20
    protected $_conn;
21
    protected $_transaction = OCI_COMMIT_ON_SUCCESS;
22
23
    /**
24
     * Ex.
25
     *
26
     *    oci8://username:password@host:1521/servicename?protocol=TCP&codepage=WE8MSWIN1252
27
     *
28
     * @param ConnectionManagement $connMngt
29
     * @throws DatabaseException
30
     */
31
    public function __construct($connMngt)
32
    {
33
        $this->_connectionManagement = $connMngt;
34
35
        $codePage = $this->_connectionManagement->getExtraParam("codepage");
36
        $codePage = ($codePage == "") ? 'UTF8' : $codePage;
37
38
        $tns = DBOci8Driver::getTnsString($connMngt);
39
40
        $this->_conn = oci_connect(
41
            $this->_connectionManagement->getUsername(), $this->_connectionManagement->getPassword(), $tns, $codePage
42
        );
43
44
        if (!$this->_conn) {
45
            $e = oci_error();
46
            throw new DatabaseException($e['message']);
47
        }
48
    }
49
50
    /**
51
     *
52
     * @param ConnectionManagement $connMngt
53
     * @return string
54
     */
55
    public static function getTnsString($connMngt)
56
    {
57
        $protocol = $connMngt->getExtraParam("protocol");
58
        $protocol = ($protocol == "") ? 'TCP' : $protocol;
59
60
        $port = $connMngt->getPort();
61
        $port = ($port == "") ? 1521 : $port;
62
63
        $svcName = $connMngt->getDatabase();
64
65
        $host = $connMngt->getServer();
66
67
        $tns = "(DESCRIPTION = " .
68
            "	(ADDRESS = (PROTOCOL = $protocol)(HOST = $host)(PORT = $port)) " .
69
            "		(CONNECT_DATA = (SERVICE_NAME = $svcName)) " .
70
            ")";
71
72
        return $tns;
73
    }
74
75
    public function __destruct()
76
    {
77
        $this->_conn = null;
78
    }
79
80
    protected function getOci8Cursor($sql, $array = null)
81
    {
82
        list($query, $array) = SQLBind::parseSQL($this->_connectionManagement, $sql, $array);
83
84
        // Prepare the statement
85
        $stid = oci_parse($this->_conn, $query);
86
        if (!$stid) {
87
            $e = oci_error($this->_conn);
88
            throw new DatabaseException($e['message']);
89
        }
90
91
        // Bind the parameters
92
        if (is_array($array)) {
93
            foreach ($array as $key => $value) {
94
                oci_bind_by_name($stid, ":$key", $value);
95
            }
96
        }
97
98
        // Perform the logic of the query
99
        $r = oci_execute($stid, $this->_transaction);
100
101
        // Check if is OK;
102
        if (!$r) {
103
            $e = oci_error($stid);
104
            throw new DatabaseException($e['message']);
105
        }
106
107
        return $stid;
108
    }
109
110
    public function getIterator($sql, $array = null)
111
    {
112
        $cur = $this->getOci8Cursor($sql, $array);
113
        $it = new Oci8Iterator($cur);
114
        return $it;
115
    }
116
117
    public function getScalar($sql, $array = null)
118
    {
119
        $cur = $this->getOci8Cursor($sql, $array);
120
121
        $row = oci_fetch_array($cur, OCI_RETURN_NULLS);
122
        if ($row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
123
            $scalar = $row[0];
124
        } else {
125
            $scalar = null;
126
        }
127
128
        oci_free_cursor($cur);
129
130
        return $scalar;
131
    }
132
133
    public function getAllFields($tablename)
134
    {
135
        $cur = $this->getOci8Cursor(SQLHelper::createSafeSQL("select * from :table", array(':table' => $tablename)));
136
137
        $ncols = oci_num_fields($cur);
138
139
        $fields = array();
140
        for ($i = 1; $i <= $ncols; $i++) {
141
            $fields[] = strtolower(oci_field_name($cur, $i));
142
        }
143
144
        oci_free_statement($cur);
145
146
        return $fields;
147
    }
148
149
    public function beginTransaction()
150
    {
151
        $this->_transaction = OCI_NO_AUTO_COMMIT;
152
    }
153
154
    public function commitTransaction()
155
    {
156
        if ($this->_transaction == OCI_COMMIT_ON_SUCCESS) {
157
            throw new DatabaseException('No transaction for commit');
158
        }
159
160
        $this->_transaction = OCI_COMMIT_ON_SUCCESS;
161
162
        $result = oci_commit($this->_conn);
163
        if (!$result) {
164
            $error = oci_error($this->_conn);
165
            throw new DatabaseException($error['message']);
166
        }
167
    }
168
169
    public function rollbackTransaction()
170
    {
171
        if ($this->_transaction == OCI_COMMIT_ON_SUCCESS) {
172
            throw new DatabaseException('No transaction for rollback');
173
        }
174
175
        $this->_transaction = OCI_COMMIT_ON_SUCCESS;
176
177
        oci_rollback($this->_conn);
178
    }
179
180
    public function executeSql($sql, $array = null)
181
    {
182
        $cur = $this->getOci8Cursor($sql, $array);
183
        oci_free_cursor($cur);
184
        return true;
185
    }
186
187
    /**
188
     *
189
     * @return resource
190
     */
191
    public function getDbConnection()
192
    {
193
        return $this->_conn;
194
    }
195
196
    public function getAttribute($name)
197
    {
198
        throw new \Exception('Method not implemented for OCI Driver');
199
    }
200
201
    public function setAttribute($name, $value)
202
    {
203
        throw new \Exception('Method not implemented for OCI Driver');
204
    }
205
}
206