1 | <?php |
||
2 | /* |
||
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
14 | * |
||
15 | * This software consists of voluntary contributions made by many individuals |
||
16 | * and is licensed under the MIT license. For more information, see |
||
17 | * <http://www.doctrine-project.org>. |
||
18 | */ |
||
19 | |||
20 | namespace Doctrine\DBAL\Driver\IBMDB2; |
||
21 | |||
22 | use Doctrine\DBAL\Driver\Connection; |
||
23 | use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
||
24 | |||
25 | class DB2Connection implements Connection, ServerInfoAwareConnection |
||
26 | { |
||
27 | /** |
||
28 | * @var resource |
||
29 | */ |
||
30 | private $_conn = null; |
||
31 | |||
32 | /** |
||
33 | * @param array $params |
||
34 | * @param string $username |
||
35 | * @param string $password |
||
36 | * @param array $driverOptions |
||
37 | * |
||
38 | * @throws \Doctrine\DBAL\Driver\IBMDB2\DB2Exception |
||
39 | */ |
||
40 | public function __construct(array $params, $username, $password, $driverOptions = []) |
||
41 | { |
||
42 | $isPersistent = (isset($params['persistent']) && $params['persistent'] == true); |
||
43 | |||
44 | if ($isPersistent) { |
||
45 | $this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions); |
||
46 | } else { |
||
47 | $this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions); |
||
48 | } |
||
49 | if ( ! $this->_conn) { |
||
50 | throw new DB2Exception(db2_conn_errormsg()); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getServerVersion() |
||
58 | { |
||
59 | $serverInfo = db2_server_info($this->_conn); |
||
60 | |||
61 | return $serverInfo->DBMS_VER; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function requiresQueryForServerVersion() |
||
68 | { |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function prepare($sql) |
||
76 | { |
||
77 | $stmt = @db2_prepare($this->_conn, $sql); |
||
78 | if ( ! $stmt) { |
||
79 | throw new DB2Exception(db2_stmt_errormsg()); |
||
80 | } |
||
81 | |||
82 | return new DB2Statement($stmt); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | View Code Duplication | public function query() |
|
0 ignored issues
–
show
|
|||
89 | { |
||
90 | $args = func_get_args(); |
||
91 | $sql = $args[0]; |
||
92 | $stmt = $this->prepare($sql); |
||
93 | $stmt->execute(); |
||
94 | |||
95 | return $stmt; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function quote($input, $type=\PDO::PARAM_STR) |
||
102 | { |
||
103 | $input = db2_escape_string($input); |
||
104 | if ($type == \PDO::PARAM_INT) { |
||
105 | return $input; |
||
106 | } else { |
||
107 | return "'".$input."'"; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function exec($statement) |
||
115 | { |
||
116 | $stmt = @db2_exec($this->_conn, $statement); |
||
117 | |||
118 | if (false === $stmt) { |
||
119 | throw new DB2Exception(db2_stmt_errormsg()); |
||
120 | } |
||
121 | |||
122 | return db2_num_rows($stmt); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function lastInsertId($name = null) |
||
129 | { |
||
130 | return db2_last_insert_id($this->_conn); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function beginTransaction() |
||
137 | { |
||
138 | db2_autocommit($this->_conn, DB2_AUTOCOMMIT_OFF); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | View Code Duplication | public function commit() |
|
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. ![]() |
|||
145 | { |
||
146 | if (!db2_commit($this->_conn)) { |
||
147 | throw new DB2Exception(db2_conn_errormsg($this->_conn)); |
||
148 | } |
||
149 | db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | View Code Duplication | public function rollBack() |
|
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. ![]() |
|||
156 | { |
||
157 | if (!db2_rollback($this->_conn)) { |
||
158 | throw new DB2Exception(db2_conn_errormsg($this->_conn)); |
||
159 | } |
||
160 | db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function errorCode() |
||
167 | { |
||
168 | return db2_conn_error($this->_conn); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function errorInfo() |
||
175 | { |
||
176 | return [ |
||
177 | 0 => db2_conn_errormsg($this->_conn), |
||
178 | 1 => $this->errorCode(), |
||
179 | ]; |
||
180 | } |
||
181 | } |
||
182 |
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.