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\Exception\DatasetException; |
||
8 | use ByJG\AnyDataset\Exception\NotAvailableException; |
||
9 | use ByJG\AnyDataset\Repository\SQLRelayIterator; |
||
10 | |||
11 | class DBSQLRelayDriver implements DBDriverInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Enter description here... |
||
16 | * |
||
17 | * @var ConnectionManagement |
||
18 | */ |
||
19 | protected $_connectionManagement; |
||
20 | |||
21 | /** Used for SQL Relay connections * */ |
||
22 | protected $_conn; |
||
23 | protected $_transaction = false; |
||
24 | |||
25 | public function __construct($connMngt) |
||
26 | { |
||
27 | $this->_connectionManagement = $connMngt; |
||
28 | |||
29 | $this->_conn = sqlrcon_alloc( |
||
30 | $this->_connectionManagement->getServer(), $this->_connectionManagement->getPort(), |
||
31 | $this->_connectionManagement->getExtraParam("unixsocket"), $this->_connectionManagement->getUsername(), |
||
32 | $this->_connectionManagement->getPassword(), 0, 1 |
||
33 | ); |
||
34 | |||
35 | sqlrcon_autoCommitOn($this->_conn); |
||
36 | } |
||
37 | |||
38 | public function __destruct() |
||
39 | { |
||
40 | if (!is_null($this->_conn)) { |
||
41 | sqlrcon_free($this->_conn); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | protected function getSQLRelayCursor($sql, $array = null) |
||
46 | { |
||
47 | $cur = sqlrcur_alloc($this->_conn); |
||
48 | |||
49 | if ($array) { |
||
50 | list($sql, $array) = SQLBind::parseSQL($this->_connectionManagement, $sql, $array); |
||
51 | |||
52 | sqlrcur_prepareQuery($cur, $sql); |
||
53 | $bindCount = 1; |
||
54 | foreach ($array as $key => $value) { |
||
55 | $field = strval($bindCount ++); |
||
56 | sqlrcur_inputBind($cur, $field, $value); |
||
57 | } |
||
58 | $success = sqlrcur_executeQuery($cur); |
||
59 | sqlrcon_endSession($this->_conn); |
||
60 | } else { |
||
61 | $success = sqlrcur_sendQuery($cur, $sql); |
||
62 | sqlrcon_endSession($this->_conn); |
||
63 | } |
||
64 | if (!$success) { |
||
65 | throw new DatasetException(sqlrcur_errorMessage($cur)); |
||
66 | } |
||
67 | |||
68 | sqlrcur_lowerCaseColumnNames($cur); |
||
69 | return $cur; |
||
70 | } |
||
71 | |||
72 | public function getIterator($sql, $array = null) |
||
73 | { |
||
74 | $cur = $this->getSQLRelayCursor($sql, $array); |
||
75 | $it = new SQLRelayIterator($cur); |
||
76 | return $it; |
||
77 | } |
||
78 | |||
79 | public function getScalar($sql, $array = null) |
||
80 | { |
||
81 | $cur = $this->getSQLRelayCursor($sql, $array); |
||
82 | $scalar = sqlrcur_getField($cur, 0, 0); |
||
83 | sqlrcur_free($cur); |
||
84 | |||
85 | return $scalar; |
||
86 | } |
||
87 | |||
88 | public function getAllFields($tablename) |
||
89 | { |
||
90 | $cur = sqlrcur_alloc($this->_conn); |
||
91 | |||
92 | $success = sqlrcur_sendQuery($cur, |
||
93 | SQLHelper::createSafeSQL("select * from :table", array(":table" => $tablename))); |
||
94 | sqlrcon_endSession($cur); |
||
95 | |||
96 | if (!$success) { |
||
97 | throw new DatasetException(sqlrcur_errorMessage($cur)); |
||
98 | } |
||
99 | |||
100 | $fields = []; |
||
101 | $colCount = sqlrcur_colCount($cur); |
||
102 | for ($col = 0; $col < $colCount; $col++) { |
||
103 | $fields[] = strtolower(sqlrcur_getColumnName($cur, $col)); |
||
104 | } |
||
105 | |||
106 | sqlrcur_free($cur); |
||
107 | |||
108 | return $fields; |
||
109 | } |
||
110 | |||
111 | public function beginTransaction() |
||
112 | { |
||
113 | $this->_transaction = true; |
||
114 | sqlrcon_autoCommitOff($this->_conn); |
||
115 | } |
||
116 | |||
117 | View Code Duplication | public function commitTransaction() |
|
0 ignored issues
–
show
|
|||
118 | { |
||
119 | if ($this->_transaction) { |
||
120 | $this->_transaction = false; |
||
121 | |||
122 | $ret = sqlrcon_commit($this->_conn); |
||
123 | if ($ret === 0) { |
||
124 | throw new DatabaseException('Commit failed'); |
||
125 | } else if ($ret === -1) { |
||
126 | throw new DatabaseException('An error occurred. Commit failed'); |
||
127 | } |
||
128 | |||
129 | sqlrcon_autoCommitOn($this->_conn); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | View Code Duplication | public function rollbackTransaction() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
134 | { |
||
135 | if ($this->_transaction) { |
||
136 | $this->_transaction = false; |
||
137 | |||
138 | $ret = sqlrcon_rollback($this->_conn); |
||
139 | if ($ret === 0) { |
||
140 | throw new DatabaseException('Commit failed'); |
||
141 | } else if ($ret === -1) { |
||
142 | throw new DatabaseException('An error occurred. Commit failed'); |
||
143 | } |
||
144 | |||
145 | sqlrcon_autoCommitOn($this->_conn); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | public function executeSql($sql, $array = null) |
||
150 | { |
||
151 | $cur = $this->getSQLRelayCursor($sql, $array); |
||
152 | sqlrcur_free($cur); |
||
153 | return true; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function getDbConnection() |
||
161 | { |
||
162 | return $this->_conn; |
||
163 | } |
||
164 | |||
165 | public function getAttribute($name) |
||
166 | { |
||
167 | throw new NotAvailableException('Method not implemented for SQL Relay Driver'); |
||
168 | } |
||
169 | |||
170 | public function setAttribute($name, $value) |
||
171 | { |
||
172 | throw new NotAvailableException('Method not implemented for SQL Relay Driver'); |
||
173 | } |
||
174 | } |
||
175 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.