1 | <?php |
---|
2 | /* |
---|
3 | * $Id$ |
---|
4 | * |
---|
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
16 | * |
---|
17 | * This software consists of voluntary contributions made by many individuals |
---|
18 | * and is licensed under the LGPL. For more information, see |
---|
19 | * <http://www.doctrine-project.org>. |
---|
20 | */ |
---|
21 | |
---|
22 | namespace Doctrine\DBAL\Driver\IBMDB2; |
---|
23 | |
---|
24 | use \Doctrine\DBAL\Driver\Statement; |
---|
25 | |
---|
26 | class DB2Statement implements \IteratorAggregate, Statement |
---|
27 | { |
---|
28 | private $_stmt = null; |
---|
29 | |
---|
30 | private $_bindParam = array(); |
---|
31 | |
---|
32 | private $_defaultFetchStyle = \PDO::FETCH_BOTH; |
---|
33 | |
---|
34 | /** |
---|
35 | * DB2_BINARY, DB2_CHAR, DB2_DOUBLE, or DB2_LONG |
---|
36 | * @var array |
---|
37 | */ |
---|
38 | static private $_typeMap = array( |
---|
39 | \PDO::PARAM_INT => DB2_LONG, |
---|
40 | \PDO::PARAM_STR => DB2_CHAR, |
---|
41 | ); |
---|
42 | |
---|
43 | public function __construct($stmt) |
---|
44 | { |
---|
45 | $this->_stmt = $stmt; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * {@inheritdoc} |
---|
50 | */ |
---|
51 | public function bindValue($param, $value, $type = null) |
---|
52 | { |
---|
53 | return $this->bindParam($param, $value, $type); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * {@inheritdoc} |
---|
58 | */ |
---|
59 | public function bindParam($column, &$variable, $type = null) |
---|
60 | { |
---|
61 | $this->_bindParam[$column] =& $variable; |
---|
62 | |
---|
63 | if ($type && isset(self::$_typeMap[$type])) { |
---|
64 | $type = self::$_typeMap[$type]; |
---|
65 | } else { |
---|
66 | $type = DB2_CHAR; |
---|
67 | } |
---|
68 | |
---|
69 | if (!db2_bind_param($this->_stmt, $column, "variable", DB2_PARAM_IN, $type)) { |
---|
70 | throw new DB2Exception(db2_stmt_errormsg()); |
---|
71 | } |
---|
72 | return true; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * {@inheritdoc} |
---|
77 | */ |
---|
78 | public function closeCursor() |
---|
79 | { |
---|
80 | if (!$this->_stmt) { |
---|
81 | return false; |
---|
82 | } |
---|
83 | |
---|
84 | $this->_bindParam = array(); |
---|
85 | db2_free_result($this->_stmt); |
---|
86 | $ret = db2_free_stmt($this->_stmt); |
---|
87 | $this->_stmt = false; |
---|
88 | return $ret; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * {@inheritdoc} |
---|
93 | */ |
---|
94 | public function columnCount() |
---|
95 | { |
---|
96 | if (!$this->_stmt) { |
---|
97 | return false; |
---|
98 | } |
---|
99 | return db2_num_fields($this->_stmt); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * {@inheritdoc} |
---|
104 | */ |
---|
105 | public function errorCode() |
---|
106 | { |
---|
107 | return db2_stmt_error(); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * {@inheritdoc} |
---|
112 | */ |
---|
113 | public function errorInfo() |
---|
114 | { |
---|
115 | return array( |
---|
116 | 0 => db2_stmt_errormsg(), |
---|
117 | 1 => db2_stmt_error(), |
---|
118 | ); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * {@inheritdoc} |
---|
123 | */ |
---|
124 | public function execute($params = null) |
---|
125 | { |
---|
126 | if (!$this->_stmt) { |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | /*$retval = true; |
---|
131 | if ($params !== null) { |
---|
132 | $retval = @db2_execute($this->_stmt, $params); |
---|
133 | } else { |
---|
134 | $retval = @db2_execute($this->_stmt); |
---|
135 | }*/ |
---|
136 | if ($params === null) { |
---|
137 | ksort($this->_bindParam); |
---|
138 | $params = array_values($this->_bindParam); |
---|
139 | } |
---|
140 | $retval = @db2_execute($this->_stmt, $params); |
---|
141 | |
---|
142 | if ($retval === false) { |
---|
143 | throw new DB2Exception(db2_stmt_errormsg()); |
---|
144 | } |
---|
145 | return $retval; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * {@inheritdoc} |
---|
150 | */ |
---|
151 | public function setFetchMode($fetchStyle = \PDO::FETCH_BOTH) |
---|
152 | { |
---|
153 | $this->_defaultFetchStyle = $fetchStyle; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * {@inheritdoc} |
---|
158 | */ |
---|
159 | public function getIterator() |
---|
160 | { |
---|
161 | $data = $this->fetchAll($this->_defaultFetchStyle); |
---|
162 | return new \ArrayIterator($data); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * {@inheritdoc} |
---|
167 | */ |
---|
168 | public function fetch($fetchStyle = null) |
---|
169 | { |
---|
170 | $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle; |
---|
171 | switch ($fetchStyle) { |
---|
172 | case \PDO::FETCH_BOTH: |
---|
173 | return db2_fetch_both($this->_stmt); |
---|
174 | case \PDO::FETCH_ASSOC: |
---|
175 | return db2_fetch_assoc($this->_stmt); |
---|
176 | case \PDO::FETCH_NUM: |
---|
177 | return db2_fetch_array($this->_stmt); |
---|
178 | default: |
---|
179 | throw new DB2Exception("Given Fetch-Style " . $fetchStyle . " is not supported."); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * {@inheritdoc} |
---|
185 | */ |
---|
186 | public function fetchAll($fetchStyle = null) |
---|
187 | { |
---|
188 | $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle; |
---|
189 | $rows = array(); |
---|
190 | while ($row = $this->fetch($fetchStyle)) { |
---|
191 | $rows[] = $row; |
---|
192 | } |
---|
193 | return $rows; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * {@inheritdoc} |
---|
198 | */ |
---|
199 | public function fetchColumn($columnIndex = 0) |
---|
200 | { |
---|
201 | $row = $this->fetch(\PDO::FETCH_NUM); |
---|
202 | if ($row && isset($row[$columnIndex])) { |
---|
203 | return $row[$columnIndex]; |
---|
204 | } |
---|
205 | return false; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * {@inheritdoc} |
---|
210 | */ |
---|
211 | public function rowCount() |
---|
212 | { |
---|
213 | return (@db2_num_rows($this->_stmt))?:0; |
---|
214 | } |
---|
215 | } |
---|