00001 <?php
00026 require_once 'Zend/Db/Statement.php';
00027
00037 class ZendX_Db_Statement_Firebird extends Zend_Db_Statement
00038 {
00039
00045 protected $_stmtPrepared = null;
00046
00052 protected $_stmtResult = null;
00053
00059 protected $_stmtRowCount = 0;
00060
00066 protected $_stmtColumnCount = 0;
00067
00073 protected $_keys = array();
00074
00080 protected $_values = array();
00081
00085 protected $_meta = null;
00086
00092 public function _prepare($sql)
00093 {
00094 $this->_stmtRowCount = 0;
00095 $this->_stmtColumnCount = 0;
00096
00097 $connection = $this->_adapter->getConnection();
00098
00099 if ($trans = $this->_adapter->getTransaction())
00100 $this->_stmtPrepared = @ibase_prepare($connection, $trans, $sql);
00101 else
00102 $this->_stmtPrepared = @ibase_prepare($connection, $sql);
00103
00104 if ($this->_stmtPrepared === false) {
00108 require_once 'ZendX/Db/Statement/Firebird/Exception.php';
00109 throw new ZendX_Db_Statement_Firebird_Exception("Firebird prepare error: " . ibase_errmsg());
00110 }
00111 }
00112
00124 protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
00125 {
00126 return true;
00127 }
00128
00134 public function close()
00135 {
00136 if ($stmt = $this->_stmtResult) {
00137 @ibase_free_result($this->_stmtResult);
00138 $this->_stmtResult = null;
00139 }
00140
00141 if ($this->_stmtPrepared) {
00142 $r = @ibase_free_query($this->_stmtPrepared);
00143 $this->_stmtPrepared = null;
00144 return $r;
00145 }
00146 return false;
00147 }
00148
00154 public function closeCursor()
00155 {
00156 if ($stmt = $this->_stmtResult) {
00157 return @ibase_free_result($this->_stmtResult);
00158 }
00159 return false;
00160 }
00161
00168 public function columnCount()
00169 {
00170 return $this->_stmtColumnCount ? $this->_stmtColumnCount : 0;
00171 }
00172
00179 public function errorCode()
00180 {
00181 if ($this->_stmtPrepared || $this->_stmtResult) {
00182 return ibase_errcode();
00183 }
00184 return false;
00185 }
00186
00193 public function errorInfo()
00194 {
00195 if (!$this->_stmtPrepared) {
00196 return false;
00197 }
00198 return array(
00199 ibase_errcode(),
00200 ibase_errmsg()
00201 );
00202 }
00203
00211 public function _execute(array $params = null)
00212 {
00213 if (!$this->_stmtPrepared) {
00214 return false;
00215 }
00216
00217
00218
00219 if ($params === null) {
00220 $params = $this->_bindParam;
00221 }
00222
00223 if ($params) {
00224 array_unshift($params, $this->_stmtPrepared);
00225 $retval = @call_user_func_array(
00226 'ibase_execute',
00227 $params
00228 );
00229 } else
00230
00231 $retval = @ibase_execute($this->_stmtPrepared);
00232 $this->_stmtResult = $retval;
00233
00234 if ($retval === false) {
00235 $last_error = ibase_errmsg();
00236 $this->_stmtRowCount = 0;
00237 }
00238
00239
00240
00241 if (!$this->_adapter->getTransaction())
00242 ibase_commit_ret();
00243
00244 if ($retval === false) {
00248 require_once 'ZendX/Db/Statement/Firebird/Exception.php';
00249 throw new ZendX_Db_Statement_Firebird_Exception("Firebird statement execute error : " . $last_error);
00250 }
00251
00252
00253 if (is_resource($this->_stmtResult)) {
00254
00255
00256 $this->_keys = array();
00257 $coln = ibase_num_fields($this->_stmtResult);
00258 $this->_stmtColumnCount = $coln;
00259 for ($i = 0; $i < $coln; $i++) {
00260 $col_info = ibase_field_info($this->_stmtResult, $i);
00261 $this->_keys[] = $this->_adapter->foldCase($col_info['name']);
00262 }
00263
00264
00265 $this->_values = array_fill(0, count($this->_keys), null);
00266
00267
00268
00269
00270 $refs = array();
00271 foreach ($this->_values as $i => &$f) {
00272 $refs[$i] = &$f;
00273 }
00274 }
00275
00276 if ($trans = $this->_adapter->getTransaction())
00277 $this->_stmtRowCount = ibase_affected_rows($trans);
00278 else
00279 $this->_stmtRowCount = ibase_affected_rows($this->_adapter->getConnection());
00280 return true;
00281 }
00282
00292 public function fetch($style = null, $cursor = null, $offset = null)
00293 {
00294 if (!$this->_stmtResult) {
00295 return false;
00296 }
00297
00298 if ($style === null) {
00299 $style = $this->_fetchMode;
00300 }
00301
00302 switch ($style) {
00303 case Zend_Db::FETCH_NUM:
00304 $row = ibase_fetch_row($this->_stmtResult, IBASE_TEXT);
00305 break;
00306 case Zend_Db::FETCH_ASSOC:
00307 $row = ibase_fetch_assoc($this->_stmtResult, IBASE_TEXT);
00308 break;
00309 case Zend_Db::FETCH_BOTH:
00310 $row = ibase_fetch_assoc($this->_stmtResult, IBASE_TEXT);
00311 if ($row !== false)
00312 $row = array_merge($row, array_values($row));
00313 break;
00314 case Zend_Db::FETCH_OBJ:
00315 $row = ibase_fetch_object($this->_stmtResult, IBASE_TEXT);
00316 break;
00317 case Zend_Db::FETCH_BOUND:
00318 $row = ibase_fetch_assoc($this->_stmtResult, IBASE_TEXT);
00319 if ($row !== false){
00320 $row = array_merge($row, array_values($row));
00321 $row = $this->_fetchBound($row);
00322 }
00323 break;
00324 default:
00328 require_once 'ZendX/Db/Statement/Firebird/Exception.php';
00329 throw new ZendX_Db_Statement_Firebird_Exception(
00330 "Invalid fetch mode '$style' specified"
00331 );
00332 break;
00333 }
00334
00335
00336 return $row;
00337 }
00338
00347 public function nextRowset()
00348 {
00352 require_once 'ZendX/Db/Statement/Firebird/Exception.php';
00353 throw new ZendX_Db_Statement_Firebird_Exception(__FUNCTION__.'() is not implemented');
00354 }
00355
00364 public function rowCount()
00365 {
00366 return $this->_stmtRowCount ? $this->_stmtRowCount : 0;
00367 }
00368
00369 }