MSSQLCallableStatement.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MSSQLCallableStatement.php,v 1.2 2004/05/22 13:43:36 micha 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 include_once 'creole/CreoleTypes.php'; 00024 00042 class MSSQLCallableStatement extends MSSQLPreparedStatement 00043 { 00045 var $boundOutVars = array(); 00046 00051 /*var $typeMap = array(); */ 00056 var $stmt; 00057 00058 00063 var $result; 00064 00071 function MSSQLCallableStatement(/*Connection*/ &$conn, $stmt) 00072 { 00073 if (! is_a($table, 'Connection')) { 00074 trigger_error( 00075 "MSSQLCallableStatement::MSSQLCallableStatement(): parameter 1 not of type 'Connection' !", 00076 E_USER_ERROR 00077 ); 00078 } 00079 00080 $this->conn =& $conn; 00081 $this->stmt = $stmt; 00082 } 00083 00087 function getResource() 00088 { 00089 return $this->stmt; 00090 } 00091 00095 function close() 00096 { 00097 @mssql_free_statement($this->stmt); 00098 $this->rsFetchCount = 0; 00099 } 00100 00104 function & executeQuery($p1 = null, $fetchmode = null) 00105 { 00106 $params = null; 00107 if ($fetchmode !== null) { 00108 $params = $p1; 00109 } elseif ($p1 !== null) { 00110 if (is_array($p1)) $params = $p1; 00111 else $fetchmode = $p1; 00112 } 00113 00114 if ($params) { 00115 for($i=0,$cnt=count($params); $i < $cnt; $i++) { 00116 $this->set($i+1, $params[$i]); 00117 } 00118 } 00119 00120 // no need to copy offset/limit in this class, because 00121 // we're not modifying an SQL here 00122 if ($this->offset > 0 && !($this->limit > 0)) { 00123 return new SQLException(CREOLE_ERROR, 'Cannot specify an offset without limit.'); 00124 } 00125 00126 $this->result = mssql_execute($this->stmt); 00127 if (!$this->result) { 00128 return new SQLException(CREOLE_ERROR, 'unable to execute callable statement', mssql_get_last_message()); 00129 } 00130 00131 return new MSSQLResultSet($this->conn, $this->result, $fetchmode, $this->offset, $this->limit); 00132 } 00133 00137 function getMoreResults() 00138 { 00139 $this->rsFetchCount++; // we track this because 00140 $hasMore = mssql_next_result($this->result); 00141 if ($this->resultSet) $this->resultSet->close(); 00142 if ($hasMore) { 00143 $clazz = $this->resultClass; 00144 $this->resultSet =& new $clazz($this, $this->result); 00145 } else { 00146 $this->resultSet = null; 00147 } 00148 return $hasMore; 00149 } 00150 00154 function registerOutParameter($paramIndex, $sqlType) 00155 { 00156 /* $typeMap seems to be only used here, so keep it simple */ 00157 static $typeMap; 00158 00159 if ($typeMap === null) 00160 { 00161 $typeMap = array 00162 ( 00163 CreoleTypes::BOOLEAN() => SQLBIT, 00164 CreoleTypes::BIGINT() => SQLINT4, 00165 CreoleTypes::SMALLINT() => SQLINT2, 00166 CreoleTypes::TINYINT() => SQLINT2, 00167 CreoleTypes::INTEGER() => SQLINT4, 00168 CreoleTypes::CHAR() => SQLCHAR, 00169 CreoleTypes::VARCHAR() => SQLVARCHAR, 00170 CreoleTypes::TEXT() => SQLTEXT, 00171 CreoleTypes::FLOAT() => SQLFLT8, 00172 CreoleTypes::DOUBLE() => SQLFLT8, 00173 CreoleTypes::DATE() => SQLVARCHAR, 00174 CreoleTypes::TIME() => SQLVARCHAR, 00175 CreoleTypes::TIMESTAMP() => SQLVARCHAR, 00176 CreoleTypes::VARBINARY() => SQLVARCHAR, 00177 CreoleTypes::NUMERIC() => SQLINT4, 00178 CreoleTypes::DECIMAL() => SQLFLT8 00179 ); 00180 } 00181 00182 mssql_bind($this->stmt, $paramIndex, $this->boundOutVars[$paramIndex], $typeMap[$sqlType], true); 00183 } 00184 00188 function setArray($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 = serialize($value); 00195 mssql_bind($this->stmt, $paramIndex, $value, SQLTEXT, $out); 00196 } 00197 } 00198 00202 function setBoolean($paramIndex, &$value, $out = false) 00203 { 00204 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00205 if ($value === null) { 00206 $this->setNull($paramIndex); 00207 } else { 00208 $value = ($value) ? 1 : 0; 00209 mssql_bind($this->stmt, $paramIndex, $value, SQLBIT, $out); 00210 } 00211 } 00212 00213 00217 function setBlob($paramIndex, &$blob, $out = false) 00218 { 00219 if ($blob === null) { 00220 $this->setNull($paramIndex); 00221 } else { 00222 if (is_object($blob)) { 00223 $blob = $blob->__toString(); 00224 } 00225 if ($out) $this->boundOutVars[$paramIndex] = &$blob; // reference means that changes to value, will be reflected 00226 $data = unpack("H*hex", $blob); 00227 mssql_bind($this->stmt, $paramIndex, $data, SQLTEXT, $out); 00228 } 00229 } 00230 00234 function setClob($paramIndex, &$clob, $out = false) 00235 { 00236 if ($clob === null) { 00237 $this->setNull($paramIndex); 00238 } else { 00239 if (is_object($clob)) { 00240 $clob = $clob->__toString(); 00241 } 00242 if ($out) $this->boundOutVars[$paramIndex] = &$clob; // reference means that changes to value, will be reflected 00243 mssql_bind($this->stmt, $paramIndex, $clob, SQLTEXT, $out); 00244 } 00245 } 00246 00250 function setDate($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 if (is_numeric($value)) $value = date("Y-m-d", $value); 00257 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00258 } 00259 } 00260 00264 function setFloat($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 = (float) $value; 00271 mssql_bind($this->stmt, $paramIndex, $value, SQLFLT8, $out); 00272 } 00273 } 00274 00278 function setInt($paramIndex, &$value, $out = false) 00279 { 00280 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00281 if ($value === null) { 00282 $this->setNull($paramIndex); 00283 } else { 00284 $value = (int) $value; 00285 mssql_bind($this->stmt, $paramIndex, $value, SQLINT4, $out); 00286 } 00287 } 00288 00292 function setNull($paramIndex) 00293 { 00294 // hopefully type isn't essential here :) 00295 $value = null; // wants a var to pass by reference 00296 mssql_bind($this->stmt, $paramIndex, $value, $type=null, $out=false, $is_null=true); 00297 } 00298 00302 function setString($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 $value = (string) $value; 00309 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00310 } 00311 } 00312 00316 function setTime($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("H:i:s", $value); 00323 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00324 } 00325 } 00326 00330 function setTimestamp($paramIndex, &$value, $out = false) 00331 { 00332 if ($out) $this->boundOutVars[$paramIndex] = &$value; // reference means that changes to value, will be reflected 00333 if ($value === null) { 00334 $this->setNull($paramIndex); 00335 } else { 00336 if (is_numeric($value)) $value = date('Y-m-d H:i:s', $value); 00337 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out); 00338 } 00339 } 00340 00345 function getArray($paramIndex) 00346 { 00347 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00348 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00349 } 00350 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00351 return (array) unserialize($this->boundOutVars[$paramIndex]); 00352 } 00353 00358 function getBoolean($paramIndex) 00359 { 00360 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00361 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00362 } 00363 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00364 return (boolean) $this->boundOutVars[$paramIndex]; 00365 } 00366 00371 function & getBlob($paramIndex) 00372 { 00373 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00374 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00375 } 00376 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00377 require_once 'creole/util/Blob.php'; 00378 $b =& new Blob(); 00379 $b->setContents($this->boundOutVars[$paramIndex]); 00380 return $b; 00381 } 00382 00387 function & getClob($paramIndex) 00388 { 00389 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00390 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00391 } 00392 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00393 require_once 'creole/util/Clob.php'; 00394 $c =& new Clob(); 00395 $c->setContents($this->boundOutVars[$paramIndex]); 00396 return $c; 00397 } 00398 00403 function getDate($paramIndex, $fmt = '%Y-%m-%d') 00404 { 00405 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00406 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00407 } 00408 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00409 00410 $ts = strtotime($this->boundOutVars[$paramIndex]); 00411 if ($ts === -1) { 00412 return new SQLException(CREOLE_ERROR_INVALID, "Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]); 00413 } 00414 if (strpos($format, '%') !== false) { 00415 return strftime($format, $ts); 00416 } else { 00417 return date($format, $ts); 00418 } 00419 00420 return $this->boundOutVars[$paramIndex]; 00421 } 00422 00427 function getFloat($paramIndex) 00428 { 00429 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00430 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00431 } 00432 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00433 return (float) $this->boundOutVars[$paramIndex]; 00434 } 00435 00440 function getInt($paramIndex) 00441 { 00442 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00443 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00444 } 00445 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00446 return (int) $this->boundOutVars[$paramIndex]; 00447 } 00448 00453 function getString($paramIndex) 00454 { 00455 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00456 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00457 } 00458 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00459 return (string) $this->boundOutVars[$paramIndex]; 00460 } 00461 00466 function getTime($paramIndex, $format='%X') 00467 { 00468 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00469 return new SQLException(CREOLE_ERROR_INVALID, '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 return new SQLException(CREOLE_ERROR_INVALID, "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 00488 function getTimestamp($paramIndex, $format = 'Y-m-d H:i:s') 00489 { 00490 if (!array_key_exists($paramIndex, $this->boundOutVars)) { 00491 return new SQLException(CREOLE_ERROR_INVALID, 'Requesting variable not bound to output var: '.$paramIndex); 00492 } 00493 if ($this->boundOutVars[$paramIndex] === null) { return null; } 00494 00495 $ts = strtotime($this->boundOutVars[$paramIndex]); 00496 if ($ts === -1) { 00497 return new SQLException(CREOLE_ERROR_INVALID, "Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]); 00498 } 00499 if (strpos($format, '%') !== false) { 00500 return strftime($format, $ts); 00501 } else { 00502 return date($format, $ts); 00503 } 00504 } 00505 00506 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS