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

src/Database/DBSQLRelayDriver.php (2 issues)

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\Database\DBDriverInterface;
7
use ByJG\AnyDataset\Database\SQLBind;
8
use ByJG\AnyDataset\Database\SQLHelper;
9
use ByJG\AnyDataset\Exception\DatabaseException;
10
use ByJG\AnyDataset\Exception\DatasetException;
11
use ByJG\AnyDataset\Exception\NotAvailableException;
12
use ByJG\AnyDataset\Repository\SQLRelayIterator;
13
14
class DBSQLRelayDriver implements DBDriverInterface
15
{
16
17
    /**
18
     * Enter description here...
19
     *
20
     * @var ConnectionManagement
21
     */
22
    protected $_connectionManagement;
23
24
    /** Used for SQL Relay connections * */
25
    protected $_conn;
26
    protected $_transaction = false;
27
28
    public function __construct($connMngt)
29
    {
30
        $this->_connectionManagement = $connMngt;
31
32
        $this->_conn = sqlrcon_alloc(
33
            $this->_connectionManagement->getServer(), $this->_connectionManagement->getPort(),
34
            $this->_connectionManagement->getExtraParam("unixsocket"), $this->_connectionManagement->getUsername(),
35
            $this->_connectionManagement->getPassword(), 0, 1
36
        );
37
38
        sqlrcon_autoCommitOn($this->_conn);
39
    }
40
41
    public function __destruct()
42
    {
43
        if (!is_null($this->_conn)) {
44
            sqlrcon_free($this->_conn);
45
        }
46
    }
47
48
    protected function getSQLRelayCursor($sql, $array = null)
49
    {
50
        $cur = sqlrcur_alloc($this->_conn);
51
        $success = true;
0 ignored issues
show
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
53
        if ($array) {
54
            list($sql, $array) = SQLBind::parseSQL($this->_connectionManagement, $sql, $array);
55
56
            sqlrcur_prepareQuery($cur, $sql);
57
            $bindCount = 1;
58
            foreach ($array as $key => $value) {
59
                $field = strval($bindCount ++);
60
                sqlrcur_inputBind($cur, $field, $value);
61
            }
62
            $success = sqlrcur_executeQuery($cur);
63
            sqlrcon_endSession($this->_conn);
64
        } else {
65
            $success = sqlrcur_sendQuery($cur, $sql);
66
            sqlrcon_endSession($this->_conn);
67
        }
68
        if (!$success) {
69
            throw new DatasetException(sqlrcur_errorMessage($cur));
70
        }
71
72
        sqlrcur_lowerCaseColumnNames($cur);
73
        return $cur;
74
    }
75
76
    public function getIterator($sql, $array = null)
77
    {
78
        $cur = $this->getSQLRelayCursor($sql, $array);
79
        $it = new SQLRelayIterator($cur);
80
        return $it;
81
    }
82
83
    public function getScalar($sql, $array = null)
84
    {
85
        $cur = $this->getSQLRelayCursor($sql, $array);
86
        $scalar = sqlrcur_getField($cur, 0, 0);
87
        sqlrcur_free($cur);
88
89
        return $scalar;
90
    }
91
92
    public function getAllFields($tablename)
93
    {
94
        $cur = sqlrcur_alloc($this->_conn);
95
96
        $success = sqlrcur_sendQuery($cur,
97
            SQLHelper::createSafeSQL("select * from :table", array(":table" => $tablename)));
98
        sqlrcon_endSession($con);
0 ignored issues
show
The variable $con does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
99
100
        if (!$success) {
101
            throw new DatasetException(sqlrcur_errorMessage($cur));
102
        }
103
104
        $fields = [];
105
        $colCount = sqlrcur_colCount($cur);
106
        for ($col = 0; $col < $colCount; $col++) {
107
            $fields[] = strtolower(sqlrcur_getColumnName($cur, $col));
108
        }
109
110
        sqlrcur_free($cur);
111
112
        return $fields;
113
    }
114
115
    public function beginTransaction()
116
    {
117
        $this->_transaction = true;
118
        sqlrcon_autoCommitOff($this->_conn);
119
    }
120
121 View Code Duplication
    public function commitTransaction()
122
    {
123
        if ($this->_transaction) {
124
            $this->_transaction = false;
125
126
            $ret = sqlrcon_commit($this->_conn);
127
            if ($ret === 0) {
128
                throw new DataBaseException('Commit failed');
129
            } else if ($ret === -1) {
130
                throw new DataBaseException('An error occurred. Commit failed');
131
            }
132
133
            sqlrcon_autoCommitOn($this->_conn);
134
        }
135
    }
136
137 View Code Duplication
    public function rollbackTransaction()
138
    {
139
        if ($this->_transaction) {
140
            $this->_transaction = false;
141
142
            $ret = sqlrcon_rollback($this->_conn);
143
            if ($ret === 0) {
144
                throw new DatabaseException('Commit failed');
145
            } else if ($ret === -1) {
146
                throw new DatabaseException('An error occurred. Commit failed');
147
            }
148
149
            sqlrcon_autoCommitOn($this->_conn);
150
        }
151
    }
152
153
    public function executeSql($sql, $array = null)
154
    {
155
        $cur = $this->getSQLRelayCursor($sql, $array);
156
        sqlrcur_free($cur);
157
        return true;
158
    }
159
160
    /**
161
     *
162
     * @return handle
163
     */
164
    public function getDbConnection()
165
    {
166
        return $this->_conn;
167
    }
168
169
    public function getAttribute($name)
170
    {
171
        throw new NotAvailableException('Method not implemented for SQL Relay Driver');
172
    }
173
174
    public function setAttribute($name, $value)
175
    {
176
        throw new NotAvailableException('Method not implemented for SQL Relay Driver');
177
    }
178
}
179