MSSQLCallableStatement.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MSSQLCallableStatement.php,v 1.17 2004/03/20 02:57:50 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/drivers/mssql/MSSQLPreparedStatement.php'; 00023 require_once 'creole/CallableStatement.php'; 00024 include_once 'creole/CreoleTypes.php'; 00025 00042 class MSSQLCallableStatement extends MSSQLPreparedStatement implements CallableStatement { 00043 00045 private $boundOutVars = array(); 00046 00051 private static $typeMap = array( 00052 CreoleTypes::BOOLEAN => SQLBIT, 00053 CreoleTypes::BIGINT => SQLINT4, 00054 CreoleTypes::SMALLINT => SQLINT2, 00055 CreoleTypes::TINYINT => SQLINT2, 00056 CreoleTypes::INTEGER => SQLINT4, 00057 CreoleTypes::CHAR => SQLCHAR, 00058 CreoleTypes::VARCHAR => SQLVARCHAR, 00059 CreoleTypes::TEXT => SQLTEXT, 00060 CreoleTypes::FLOAT => SQLFLT8, 00061 CreoleTypes::DOUBLE => SQLFLT8, 00062 CreoleTypes::DATE => SQLVARCHAR, 00063 CreoleTypes::TIME => SQLVARCHAR, 00064 CreoleTypes::TIMESTAMP => SQLVARCHAR, 00065 CreoleTypes::VARBINARY => SQLVARCHAR, 00066 CreoleTypes::NUMERIC => SQLINT4, 00067 CreoleTypes::DECIMAL => SQLFLT8 00068 ); 00069 00074 private $stmt; 00075 00076 00081 private $result; 00082 00089 public function __construct(Connection $conn, $stmt) 00090 { 00091 print " - > IN CONSTRUCTOR \n"; 00092 $this->conn = $conn; 00093 $this->stmt = $stmt; 00094 } 00095 00099 public function getResource() 00100 { 00101 return $this->stmt; 00102 } 00103 00107 function close() 00108 { 00109 @mssql_free_statement($this->stmt); 00110 $this->rsFetchCount = 0; 00111 } 00112 00116 function executeQuery($p1 = null, $fetchmode = null) 00117 { 00118 $params = null; 00119 if ($fetchmode !== null) { 00120 $params = $p1; 00121 } elseif ($p1 !== null) { 00122 if (is_array($p1)) $params = $p1; 00123 else $fetchmode = $p1; 00124 } 00125 00126 if ($params) { 00127 for($i=0,$cnt=count($params); $i < $cnt; $i++) { 00128 $this->set($i+1, $params[$i]); 00129 } 00130 } 00131 00132 // no need to copy offset/limit in this class, because 00133 // we're not modifying an SQL here 00134 if ($this->offset > 0 && !($this->limit > 0)) { 00135 throw new SQLException('Cannot specify an offset without limit.'); 00136 } 00137 00138 $this->result = mssql_execute($this->stmt); 00139 if (!$this->result) { 00140 throw new SQLException('unable to execute callable statement', mssql_get_last_message()); 00141 } 00142 00143 return new MSSQLResultSet($this->conn, $this->result, $fetchmode, $this->offset, $this->limit); 00144 } 00145 00149 function getMoreResults() 00150 { 00151 $this->rsFetchCount++; // we track this because 00152 $hasMore = mssql_next_result($this->result); 00153 if ($this->resultSet) $this->resultSet->close(); 00154 if ($hasMore) { 00155 $clazz = $this->resultClass; 00156 $this->resultSet = new $clazz($this, $this->result); 00157 } else { 00158 $this->resultSet = null; 00159 } 00160 return $hasMore; 00161 } 00162 00166 function registerOutParameter($paramIndex, $sqlType) 00167 { 00168 mssql_bind($this->stmt, $paramIndex, $this->boundOutVars[$paramIndex], self::$typeMap[$sqlType], true); 00169 } 00170 00174 function setArray($paramIndex, $value, $out = false) 00175 { 00176 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00177 if ($value === null) { 00178 $this->setNull($paramIndex); 00179 } else { 00180 $value = serialize($value); 00181 mssql_bind($this->stmt, $paramIndex, $value, SQLTEXT, $out); 00182 } 00183 } 00184 00188 function setBoolean($paramIndex, $value, $out = false) 00189 { 00190 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00191 if ($value === null) { 00192 $this->setNull($paramIndex); 00193 } else { 00194 $value = ($value) ? 1 : 0; 00195 mssql_bind($this->stmt, $paramIndex, $value, SQLBIT, $out); 00196 } 00197 } 00198 00199 00203 function setBlob($paramIndex, $blob, $out = false) 00204 { 00205 if ($blob === null) { 00206 $this->setNull($paramIndex); 00207 } else { 00208 if (is_object($blob)) { 00209 $blob = $blob->__toString(); 00210 } 00211 if ($out) $this->boundOutVars[$paramIndex] = &$blob; // reference means that changes to value, will be reflected 00212 $data = unpack("H*hex", $blob); 00213 mssql_bind($this->stmt, $paramIndex, $data, SQLTEXT, $out); 00214 } 00215 } 00216 00220 function setClob($paramIndex, $clob, $out = false) 00221 { 00222 if ($clob === null) { 00223 $this->setNull($paramIndex); 00224 } else { 00225 if (is_object($clob)) { 00226 $clob = $clob->__toString(); 00227 } 00228 if ($out) $this->boundOutVars[$paramIndex] = &$clob; // reference means that changes to value, will be reflected 00229 mssql_bind($this->stmt, $paramIndex, $clob, SQLTEXT, $out); 00230 } 00231 } 00232 00236 function setDate($paramIndex, $value, $out = false) 00237 { 00238 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00239 if ($value === null) { 00240 $this->setNull($paramIndex); 00241 } else { 00242 if (is_numeric($value)) $value = date("Y-m-d", $value); 00243 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00244 } 00245 } 00246 00250 function setFloat($paramIndex, $value, $out = false) 00251 { 00252 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00253 if ($value === null) { 00254 $this->setNull($paramIndex); 00255 } else { 00256 $value = (float) $value; 00257 mssql_bind($this->stmt, $paramIndex, $value, SQLFLT8, $out); 00258 } 00259 } 00260 00264 function setInt($paramIndex, $value, $out = false) 00265 { 00266 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00267 if ($value === null) { 00268 $this->setNull($paramIndex); 00269 } else { 00270 $value = (int) $value; 00271 mssql_bind($this->stmt, $paramIndex, $value, SQLINT4, $out); 00272 } 00273 } 00274 00278 function setNull($paramIndex) 00279 { 00280 // hopefully type isn't essential here :) 00281 $value = null; // wants a var to pass by reference 00282 mssql_bind($this->stmt, $paramIndex, $value, $type=null, $out=false, $is_null=true); 00283 } 00284 00288 function setString($paramIndex, $value, $out = false) 00289 { 00290 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00291 if ($value === null) { 00292 $this->setNull($paramIndex); 00293 } else { 00294 $value = (string) $value; 00295 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00296 } 00297 } 00298 00302 function setTime($paramIndex, $value, $out = false) 00303 { 00304 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00305 if ($value === null) { 00306 $this->setNull($paramIndex); 00307 } else { 00308 if (is_numeric($value)) $value = date("H:i:s", $value); 00309 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00310 } 00311 } 00312 00316 function setTimestamp($paramIndex, $value, $out = false) 00317 { 00318 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00319 if ($value === null) { 00320 $this->setNull($paramIndex); 00321 } else { 00322 if (is_numeric($value)) $value = date('Y-m-d H:i:s', $value); 00323 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00324 } 00325 } 00326 00330 function getArray($paramIndex) 00331 { 00332 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00333 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00334 } 00335 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00336 return (array) unserialize($this->boundOutVars[$paramIndex]); 00337 } 00338 00342 function getBoolean($paramIndex) 00343 { 00344 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00345 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00346 } 00347 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00348 return (boolean) $this->boundOutVars[$paramIndex]; 00349 } 00350 00354 function getBlob($paramIndex) 00355 { 00356 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00357 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00358 } 00359 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00360 require_once 'creole/util/Blob.php'; 00361 $b = new Blob(); 00362 $b->setContents($this->boundOutVars[$paramIndex]); 00363 return $b; 00364 } 00365 00369 function getClob($paramIndex) 00370 { 00371 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00372 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00373 } 00374 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00375 require_once 'creole/util/Clob.php'; 00376 $c = new Clob(); 00377 $c->setContents($this->boundOutVars[$paramIndex]); 00378 return $c; 00379 } 00380 00384 function getDate($paramIndex, $fmt = '%Y-%m-%d') 00385 { 00386 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00387 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00388 } 00389 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00390 00391 $ts = strtotime($this->boundOutVars[$paramIndex]); 00392 if ($ts === -1) { 00393 throw new SQLException("Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]); 00394 } 00395 if (strpos($format, '%') !== false) { 00396 return strftime($format, $ts); 00397 } else { 00398 return date($format, $ts); 00399 } 00400 00401 return $this->boundOutVars[$paramIndex]; 00402 } 00403 00408 function getFloat($paramIndex) 00409 { 00410 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00411 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00412 } 00413 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00414 return (float) $this->boundOutVars[$paramIndex]; 00415 } 00416 00420 function getInt($paramIndex) 00421 { 00422 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00423 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00424 } 00425 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00426 return (int) $this->boundOutVars[$paramIndex]; 00427 } 00428 00432 function getString($paramIndex) 00433 { 00434 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00435 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00436 } 00437 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00438 return (string) $this->boundOutVars[$paramIndex]; 00439 } 00440 00444 function getTime($paramIndex, $format='%X') 00445 { 00446 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00447 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00448 } 00449 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00450 00451 $ts = strtotime($this->boundOutVars[$paramIndex]); 00452 if ($ts === -1) { 00453 throw new SQLException("Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]); 00454 } 00455 if (strpos($format, '%') !== false) { 00456 return strftime($format, $ts); 00457 } else { 00458 return date($format, $ts); 00459 } 00460 00461 } 00462 00466 function getTimestamp($paramIndex, $format = 'Y-m-d H:i:s') 00467 { 00468 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00469 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex); 00470 } 00471 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00472 00473 $ts = strtotime($this->boundOutVars[$paramIndex]); 00474 if ($ts === -1) { 00475 throw new SQLException("Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]); 00476 } 00477 if (strpos($format, '%') !== false) { 00478 return strftime($format, $ts); 00479 } else { 00480 return date($format, $ts); 00481 } 00482 } 00483 00484 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS