00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 $this->result = mssql_execute($this->stmt);
00133 if (!$this->result) {
00134 throw new SQLException('unable to execute callable statement', mssql_get_last_message());
00135 }
00136
00137 return new MSSQLResultSet($this->conn, $this->result, $fetchmode, $this->offset, $this->limit);
00138 }
00139
00143 function getMoreResults()
00144 {
00145 $this->rsFetchCount++;
00146 $hasMore = mssql_next_result($this->result);
00147 if ($this->resultSet) $this->resultSet->close();
00148 if ($hasMore) {
00149 $clazz = $this->resultClass;
00150 $this->resultSet = new $clazz($this, $this->result);
00151 } else {
00152 $this->resultSet = null;
00153 }
00154 return $hasMore;
00155 }
00156
00160 function registerOutParameter($paramIndex, $sqlType, $maxLength = null)
00161 {
00162 mssql_bind($this->stmt, $paramIndex, $this->boundOutVars[$paramIndex], self::$typeMap[$sqlType], true, false, $maxLength);
00163 }
00164
00168 function setArray($paramIndex, $value, $out = false)
00169 {
00170 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00171 if ($value === null) {
00172 $this->setNull($paramIndex);
00173 } else {
00174 $value = serialize($value);
00175 mssql_bind($this->stmt, $paramIndex, $value, SQLTEXT, $out);
00176 }
00177 }
00178
00182 function setBoolean($paramIndex, $value, $out = false)
00183 {
00184 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00185 if ($value === null) {
00186 $this->setNull($paramIndex);
00187 } else {
00188 $value = ($value) ? 1 : 0;
00189 mssql_bind($this->stmt, $paramIndex, $value, SQLBIT, $out);
00190 }
00191 }
00192
00193
00197 function setBlob($paramIndex, $blob, $out = false)
00198 {
00199 if ($blob === null) {
00200 $this->setNull($paramIndex);
00201 } else {
00202 if (is_object($blob)) {
00203 $blob = $blob->__toString();
00204 }
00205 if ($out) $this->boundOutVars[$paramIndex] = &$blob;
00206 $data = unpack("H*hex", $blob);
00207 mssql_bind($this->stmt, $paramIndex, $data, SQLTEXT, $out);
00208 }
00209 }
00210
00214 function setClob($paramIndex, $clob, $out = false)
00215 {
00216 if ($clob === null) {
00217 $this->setNull($paramIndex);
00218 } else {
00219 if (is_object($clob)) {
00220 $clob = $clob->__toString();
00221 }
00222 if ($out) $this->boundOutVars[$paramIndex] = &$clob;
00223 mssql_bind($this->stmt, $paramIndex, $clob, SQLTEXT, $out);
00224 }
00225 }
00226
00230 function setDate($paramIndex, $value, $out = false)
00231 {
00232 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00233 if ($value === null) {
00234 $this->setNull($paramIndex);
00235 } else {
00236 if (is_numeric($value)) $value = date("Y-m-d", $value);
00237 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out);
00238 }
00239 }
00240
00244 function setFloat($paramIndex, $value, $out = false)
00245 {
00246 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00247 if ($value === null) {
00248 $this->setNull($paramIndex);
00249 } else {
00250 $value = (float) $value;
00251 mssql_bind($this->stmt, $paramIndex, $value, SQLFLT8, $out);
00252 }
00253 }
00254
00258 function setInt($paramIndex, $value, $out = false)
00259 {
00260 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00261 if ($value === null) {
00262 $this->setNull($paramIndex);
00263 } else {
00264 $value = (int) $value;
00265 mssql_bind($this->stmt, $paramIndex, $value, SQLINT4, $out);
00266 }
00267 }
00268
00272 function setNull($paramIndex)
00273 {
00274
00275 $value = null;
00276 mssql_bind($this->stmt, $paramIndex, $value, $type=null, $out=false, $is_null=true);
00277 }
00278
00282 function setString($paramIndex, $value, $out = false)
00283 {
00284 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00285 if ($value === null) {
00286 $this->setNull($paramIndex);
00287 } else {
00288 $value = (string) $value;
00289 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out);
00290 }
00291 }
00292
00296 function setTime($paramIndex, $value, $out = false)
00297 {
00298 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00299 if ($value === null) {
00300 $this->setNull($paramIndex);
00301 } else {
00302 if (is_numeric($value)) $value = date("H:i:s", $value);
00303 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out);
00304 }
00305 }
00306
00310 function setTimestamp($paramIndex, $value, $out = false)
00311 {
00312 if ($out) $this->boundOutVars[$paramIndex] = &$value;
00313 if ($value === null) {
00314 $this->setNull($paramIndex);
00315 } else {
00316 if (is_numeric($value)) $value = date('Y-m-d H:i:s', $value);
00317 mssql_bind($this->stmt, $paramIndex, $value, SQLVARCHAR, $out);
00318 }
00319 }
00320
00324 function getArray($paramIndex)
00325 {
00326 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00327 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00328 }
00329 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00330 return (array) unserialize($this->boundOutVars[$paramIndex]);
00331 }
00332
00336 function getBoolean($paramIndex)
00337 {
00338 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00339 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00340 }
00341 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00342 return (boolean) $this->boundOutVars[$paramIndex];
00343 }
00344
00348 function getBlob($paramIndex)
00349 {
00350 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00351 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00352 }
00353 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00354 require_once 'creole/util/Blob.php';
00355 $b = new Blob();
00356 $b->setContents($this->boundOutVars[$paramIndex]);
00357 return $b;
00358 }
00359
00363 function getClob($paramIndex)
00364 {
00365 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00366 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00367 }
00368 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00369 require_once 'creole/util/Clob.php';
00370 $c = new Clob();
00371 $c->setContents($this->boundOutVars[$paramIndex]);
00372 return $c;
00373 }
00374
00378 function getDate($paramIndex, $fmt = '%Y-%m-%d')
00379 {
00380 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00381 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00382 }
00383 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00384
00385 $ts = strtotime($this->boundOutVars[$paramIndex]);
00386 if ($ts === -1 || $ts === false) {
00387 throw new SQLException("Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]);
00388 }
00389 if (strpos($format, '%') !== false) {
00390 return strftime($format, $ts);
00391 } else {
00392 return date($format, $ts);
00393 }
00394
00395 return $this->boundOutVars[$paramIndex];
00396 }
00397
00402 function getFloat($paramIndex)
00403 {
00404 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00405 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00406 }
00407 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00408 return (float) $this->boundOutVars[$paramIndex];
00409 }
00410
00414 function getInt($paramIndex)
00415 {
00416 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00417 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00418 }
00419 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00420 return (int) $this->boundOutVars[$paramIndex];
00421 }
00422
00426 function getString($paramIndex)
00427 {
00428 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00429 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00430 }
00431 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00432 return (string) $this->boundOutVars[$paramIndex];
00433 }
00434
00438 function getTime($paramIndex, $format='%X')
00439 {
00440 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00441 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00442 }
00443 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00444
00445 $ts = strtotime($this->boundOutVars[$paramIndex]);
00446 if ($ts === -1 || $ts === false) {
00447 throw new SQLException("Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]);
00448 }
00449 if (strpos($format, '%') !== false) {
00450 return strftime($format, $ts);
00451 } else {
00452 return date($format, $ts);
00453 }
00454
00455 }
00456
00460 function getTimestamp($paramIndex, $format = 'Y-m-d H:i:s')
00461 {
00462 if (!array_key_exists($paramIndex, $this->boundOutVars)) {
00463 throw new SQLException('Requesting variable not bound to output var: '.$paramIndex);
00464 }
00465 if ($this->boundOutVars[$paramIndex] === null) { return null; }
00466
00467 $ts = strtotime($this->boundOutVars[$paramIndex]);
00468 if ($ts === -1 || $ts === false) {
00469 throw new SQLException("Unable to convert value at column " . $paramIndex . " to timestamp: " . $this->boundOutVars[$paramIndex]);
00470 }
00471 if (strpos($format, '%') !== false) {
00472 return strftime($format, $ts);
00473 } else {
00474 return date($format, $ts);
00475 }
00476 }
00477
00478 }