Public Member Functions | |
__construct (Connection $conn, $sql) | |
Create new prepared statement instance. | |
setLimit ($v) | |
getLimit () | |
setOffset ($v) | |
getOffset () | |
getResultSet () | |
getUpdateCount () | |
getMoreResults () | |
getConnection () | |
getResource () | |
Statement resources do not exist for emulated prepared statements, so this just returns null . | |
close () | |
Nothing to close for emulated prepared statements. | |
executeQuery ($p1=null, $fetchmode=null) | |
Executes the SQL query in this PreparedStatement object and returns the resultset generated by the query. | |
executeUpdate ($params=null) | |
Executes the SQL INSERT, UPDATE, or DELETE statement in this PreparedStatement object. | |
set ($paramIndex, $value) | |
A generic set method. | |
setArray ($paramIndex, $value) | |
Sets an array. | |
setBoolean ($paramIndex, $value) | |
Sets a boolean value. | |
setBlob ($paramIndex, $blob) | |
setClob ($paramIndex, $clob) | |
setDate ($paramIndex, $value) | |
setDecimal ($paramIndex, $value) | |
setDouble ($paramIndex, $value) | |
setFloat ($paramIndex, $value) | |
setInt ($paramIndex, $value) | |
setInteger ($paramIndex, $value) | |
Alias for setInt(). | |
setNull ($paramIndex) | |
setString ($paramIndex, $value) | |
setTime ($paramIndex, $value) | |
setTimestamp ($paramIndex, $value) | |
Protected Member Functions | |
parseQuery ($sql) | |
Parse the SQL query for ? positions. | |
replaceParams () | |
Replaces placeholders with the specified parameter values in the SQL. | |
escape ($str) | |
Escapes special characters (usu. | |
Protected Attributes | |
$conn | |
$limit = 0 | |
$offset = 0 | |
$sql | |
$sql_cache | |
$sql_cache_valid = false | |
$positions | |
$positionsCount | |
$boundInVars = array() | |
$resultSet | |
$updateCount |
Definition at line 34 of file PreparedStatementCommon.php.
PreparedStatementCommon::__construct | ( | Connection $ | conn, | |
$ | sql | |||
) |
Create new prepared statement instance.
object | $conn Connection object | |
string | $sql The SQL to work with. | |
array | $positions The positions in SQL of ?'s. | |
restult | $stmt If the driver supports prepared queries, then $stmt will contain the statement to use. |
Reimplemented in MSSQLCallableStatement.
Definition at line 115 of file PreparedStatementCommon.php.
References $sql, and parseQuery().
00116 { 00117 $this->conn = $conn; 00118 $this->sql = $sql; 00119 00120 $this->positions = $this->parseQuery ( $sql ); 00121 // save processing later in cases where we may repeatedly exec statement 00122 $this->positionsCount = count ( $this->positions ); 00123 }
PreparedStatementCommon::close | ( | ) |
Nothing to close for emulated prepared statements.
Reimplemented in MSSQLCallableStatement, and OCI8PreparedStatement.
Definition at line 255 of file PreparedStatementCommon.php.
PreparedStatementCommon::escape | ( | $ | str | ) | [abstract, protected] |
Escapes special characters (usu.
quotes) using native driver function.
string | $str The input string. |
Reimplemented in MSSQLPreparedStatement, MySQLPreparedStatement, MySQLiPreparedStatement, ODBCPreparedStatement, OCI8PreparedStatement, PgSQLPreparedStatement, and SQLitePreparedStatement.
Referenced by setArray(), setBlob(), setClob(), setDate(), setString(), and setTime().
PreparedStatementCommon::executeQuery | ( | $ | p1 = null , |
|
$ | fetchmode = null | |||
) |
Executes the SQL query in this PreparedStatement object and returns the resultset generated by the query.
We support two signatures for this method:
mixed | $p1 Either (array) Parameters that will be set using PreparedStatement::set() before query is executed or (int) fetchmode. | |
int | $fetchmode The mode to use when fetching the results (e.g. ResultSet::FETCHMODE_NUM, ResultSet::FETCHMODE_ASSOC). |
SQLException | if a database access error occurs. |
Reimplemented in MSSQLCallableStatement, MSSQLPreparedStatement, and OCI8PreparedStatement.
Definition at line 310 of file PreparedStatementCommon.php.
References $sql, and replaceParams().
00311 { 00312 $params = null; 00313 if ($fetchmode !== null) { 00314 $params = $p1; 00315 } elseif ($p1 !== null) { 00316 if (is_array($p1)) $params = $p1; 00317 else $fetchmode = $p1; 00318 } 00319 00320 foreach ( (array) $params as $i=>$param ) { 00321 $this->set ( $i + 1, $param ); 00322 unset ( $i, $param ); 00323 } 00324 unset ( $params ); 00325 00326 $this->updateCount = null; // reset 00327 $sql = $this->replaceParams(); 00328 00329 if ($this->limit > 0 || $this->offset > 0) { 00330 $this->conn->applyLimit($sql, $this->offset, $this->limit); 00331 } 00332 00333 $this->resultSet = $this->conn->executeQuery($sql, $fetchmode); 00334 return $this->resultSet; 00335 }
PreparedStatementCommon::executeUpdate | ( | $ | params = null |
) |
Executes the SQL INSERT, UPDATE, or DELETE statement in this PreparedStatement object.
array | $params Parameters that will be set using PreparedStatement::set() before query is executed. |
SQLException | if a database access error occurs. |
Reimplemented in ODBCPreparedStatement, and OCI8PreparedStatement.
Definition at line 344 of file PreparedStatementCommon.php.
References $sql, and replaceParams().
00345 { 00346 foreach ( (array) $params as $i=>$param ) { 00347 $this->set ( $i + 1, $param ); 00348 unset ( $i, $param ); 00349 } 00350 unset ( $params ); 00351 00352 if($this->resultSet) $this->resultSet->close(); 00353 $this->resultSet = null; // reset 00354 $sql = $this->replaceParams(); 00355 $this->updateCount = $this->conn->executeUpdate($sql); 00356 return $this->updateCount; 00357 }
PreparedStatementCommon::getConnection | ( | ) |
Definition at line 237 of file PreparedStatementCommon.php.
Referenced by MySQLiPreparedStatement::escape().
PreparedStatementCommon::getLimit | ( | ) |
PreparedStatementCommon::getMoreResults | ( | ) |
Reimplemented in MSSQLCallableStatement.
Definition at line 227 of file PreparedStatementCommon.php.
00228 { 00229 if ($this->resultSet) $this->resultSet->close(); 00230 $this->resultSet = null; 00231 return false; 00232 }
PreparedStatementCommon::getOffset | ( | ) |
PreparedStatementCommon::getResource | ( | ) |
Statement resources do not exist for emulated prepared statements, so this just returns null
.
Reimplemented in MSSQLCallableStatement.
Definition at line 247 of file PreparedStatementCommon.php.
Referenced by MySQLiPreparedStatement::escape().
PreparedStatementCommon::getResultSet | ( | ) |
Definition at line 211 of file PreparedStatementCommon.php.
PreparedStatementCommon::getUpdateCount | ( | ) |
Definition at line 219 of file PreparedStatementCommon.php.
PreparedStatementCommon::parseQuery | ( | $ | sql | ) | [protected] |
Parse the SQL query for ? positions.
string | $sql The query to process |
Definition at line 131 of file PreparedStatementCommon.php.
References $positions, and $sql.
Referenced by __construct().
00132 { 00133 00134 $positions = array(); 00135 // match anything ? ' " or \ in $sql with an early out if we find nothing 00136 if ( preg_match_all ( '([\?]|[\']|[\"]|[\\\])', $sql, $matches, PREG_OFFSET_CAPTURE ) !== 0 ) { 00137 $matches = $matches['0']; 00138 $open = NULL; 00139 // go thru all our matches and see what we can find 00140 for ( $i = 0, $j = count ( $matches ); $i < $j; $i++ ) { 00141 switch ( $matches[$i]['0'] ) { 00142 // if we already have an open " or ' then check if this is the end 00143 // to close it or not 00144 case $open: 00145 $open = NULL; 00146 break; 00147 // we have a quote, set ourselves open 00148 case '"': 00149 case "'": 00150 $open = $matches[$i]['0']; 00151 break; 00152 // check if it is an escaped quote and skip if it is 00153 case '\\': 00154 $next_match = $matches[$i+1]['0']; 00155 if ( $next_match === '"' || $next_match === "'" ) { 00156 $i++; 00157 } 00158 unset ( $next_match ); 00159 break; 00160 // we found a ?, check we arent in an open "/' first and 00161 // add it to the position list if we arent 00162 default: 00163 if ( $open === NULL ) { 00164 $positions[] = $matches[$i]['1']; 00165 } 00166 } 00167 unset ( $matches[$i] ); 00168 } 00169 unset ( $open, $matches, $i, $j ); 00170 } 00171 00172 return $positions; 00173 00174 }
PreparedStatementCommon::replaceParams | ( | ) | [protected] |
Replaces placeholders with the specified parameter values in the SQL.
This is for emulated prepared statements.
SQLException | - if param not bound. |
Reimplemented in ODBCPreparedStatement.
Definition at line 267 of file PreparedStatementCommon.php.
References $sql.
Referenced by MSSQLPreparedStatement::executeQuery(), executeQuery(), and executeUpdate().
00268 { 00269 // early out if we still have the same query ready 00270 if ( $this->sql_cache_valid === true ) { 00271 return $this->sql_cache; 00272 } 00273 00274 // Default behavior for this function is to behave in 'emulated' mode. 00275 $sql = ''; 00276 $last_position = 0; 00277 00278 for ($position = 0; $position < $this->positionsCount; $position++) { 00279 if (!isset($this->boundInVars[$position + 1])) { 00280 throw new SQLException('Replace params: undefined query param: ' . ($position + 1)); 00281 } 00282 $current_position = $this->positions[$position]; 00283 $sql .= substr($this->sql, $last_position, $current_position - $last_position); 00284 $sql .= $this->boundInVars[$position + 1]; 00285 $last_position = $current_position + 1; 00286 } 00287 // append the rest of the query 00288 $sql .= substr($this->sql, $last_position); 00289 00290 // just so we dont touch anything with a blob/clob 00291 if ( strlen ( $sql ) > 2048 ) { 00292 $this->sql_cache = $sql; 00293 $this->sql_cache_valid = true; 00294 return $this->sql_cache; 00295 } else { 00296 return $sql; 00297 } 00298 }
PreparedStatementCommon::set | ( | $ | paramIndex, | |
$ | value | |||
) |
A generic set method.
You can use this if you don't want to concern yourself with the details. It involves slightly more overhead than the specific settesr, since it grabs the PHP type to determine which method makes most sense.
int | $paramIndex | |
mixed | $value |
SQLException |
Definition at line 378 of file PreparedStatementCommon.php.
References setBlob(), setClob(), and setTimestamp().
00379 { 00380 $type = gettype($value); 00381 if ($type == "object") { 00382 if (is_a($value, 'Blob')) { 00383 $this->setBlob($paramIndex, $value); 00384 } elseif (is_a($value, 'Clob')) { 00385 $this->setClob($paramIndex, $value); 00386 } elseif (is_a($value, 'Date')) { 00387 // can't be sure if the column type is a DATE, TIME, or TIMESTAMP column 00388 // we'll just use TIMESTAMP by default; hopefully DB won't complain (if 00389 // it does, then this method just shouldn't be used). 00390 $this->setTimestamp($paramIndex, $value); 00391 } else { 00392 throw new SQLException("Unsupported object type passed to set(): " . get_class($value)); 00393 } 00394 } else { 00395 switch ( $type ) { 00396 case 'integer': 00397 $type = 'int'; 00398 break; 00399 case 'double': 00400 $type = 'float'; 00401 break; 00402 } 00403 $setter = 'set' . ucfirst($type); // PHP types are case-insensitive, but we'll do this in case that change 00404 if ( method_exists ( $this, $setter ) ) { 00405 $this->$setter($paramIndex, $value); 00406 } else { 00407 throw new SQLException ( "Unsupported datatype passed to set(): " . $type ); 00408 } 00409 } 00410 }
PreparedStatementCommon::setArray | ( | $ | paramIndex, | |
$ | value | |||
) |
Sets an array.
Unless a driver-specific method is used, this means simply serializing the passed parameter and storing it as a string.
int | $paramIndex | |
array | $value |
Reimplemented in PgSQLPreparedStatement.
Definition at line 420 of file PreparedStatementCommon.php.
References escape(), and setNull().
00421 { 00422 $this->sql_cache_valid = false; 00423 if ($value === null) { 00424 $this->setNull($paramIndex); 00425 } else { 00426 $this->boundInVars[$paramIndex] = "'" . $this->escape(serialize($value)) . "'"; 00427 } 00428 }
PreparedStatementCommon::setBlob | ( | $ | paramIndex, | |
$ | blob | |||
) |
Reimplemented in MSSQLPreparedStatement, ODBCPreparedStatement, OCI8PreparedStatement, PgSQLPreparedStatement, and SQLitePreparedStatement.
Definition at line 451 of file PreparedStatementCommon.php.
References escape(), and setNull().
Referenced by set().
00452 { 00453 $this->sql_cache_valid = false; 00454 if ($blob === null) { 00455 $this->setNull($paramIndex); 00456 } else { 00457 // they took magic __toString() out of PHP5.0.0; this sucks 00458 if (is_object($blob)) { 00459 $this->boundInVars[$paramIndex] = "'" . $this->escape($blob->__toString()) . "'"; 00460 } else { 00461 $this->boundInVars[$paramIndex] = "'" . $this->escape($blob) . "'"; 00462 } 00463 } 00464 }
PreparedStatementCommon::setBoolean | ( | $ | paramIndex, | |
$ | value | |||
) |
Sets a boolean value.
Default behavior is true = 1, false = 0.
int | $paramIndex | |
boolean | $value |
Reimplemented in PgSQLPreparedStatement.
Definition at line 437 of file PreparedStatementCommon.php.
References setNull().
00438 { 00439 $this->sql_cache_valid = false; 00440 if ($value === null) { 00441 $this->setNull($paramIndex); 00442 } else { 00443 $this->boundInVars[$paramIndex] = (int) $value; 00444 } 00445 }
PreparedStatementCommon::setClob | ( | $ | paramIndex, | |
$ | clob | |||
) |
Reimplemented in ODBCPreparedStatement, and OCI8PreparedStatement.
Definition at line 469 of file PreparedStatementCommon.php.
References escape(), and setNull().
Referenced by set().
00470 { 00471 $this->sql_cache_valid = false; 00472 if ($clob === null) { 00473 $this->setNull($paramIndex); 00474 } else { 00475 // they took magic __toString() out of PHP5.0.0; this sucks 00476 if (is_object($clob)) { 00477 $this->boundInVars[$paramIndex] = "'" . $this->escape($clob->__toString()) . "'"; 00478 } else { 00479 $this->boundInVars[$paramIndex] = "'" . $this->escape($clob) . "'"; 00480 } 00481 } 00482 }
PreparedStatementCommon::setDate | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
string | $value |
Reimplemented in OCI8PreparedStatement.
Definition at line 489 of file PreparedStatementCommon.php.
References escape(), and setNull().
00490 { 00491 $this->sql_cache_valid = false; 00492 if ($value === null) { 00493 $this->setNull($paramIndex); 00494 } else { 00495 if (is_numeric($value)) $value = date("Y-m-d", $value); 00496 elseif (is_object($value)) $value = date("Y-m-d", $value->getTime()); 00497 $this->boundInVars[$paramIndex] = "'" . $this->escape($value) . "'"; 00498 } 00499 }
PreparedStatementCommon::setDecimal | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
double | $value |
Definition at line 506 of file PreparedStatementCommon.php.
References setNull().
00507 { 00508 $this->sql_cache_valid = false; 00509 if ($value === null) { 00510 $this->setNull($paramIndex); 00511 } else { 00512 $this->boundInVars[$paramIndex] = (float) $value; 00513 } 00514 }
PreparedStatementCommon::setDouble | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
double | $value |
Definition at line 521 of file PreparedStatementCommon.php.
References setNull().
00522 { 00523 $this->sql_cache_valid = false; 00524 if ($value === null) { 00525 $this->setNull($paramIndex); 00526 } else { 00527 $this->boundInVars[$paramIndex] = (double) $value; 00528 } 00529 }
PreparedStatementCommon::setFloat | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
float | $value |
Definition at line 536 of file PreparedStatementCommon.php.
References setNull().
00537 { 00538 $this->sql_cache_valid = false; 00539 if ($value === null) { 00540 $this->setNull($paramIndex); 00541 } else { 00542 $this->boundInVars[$paramIndex] = (float) $value; 00543 } 00544 }
PreparedStatementCommon::setInt | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
int | $value |
Definition at line 551 of file PreparedStatementCommon.php.
References setNull().
Referenced by setInteger().
00552 { 00553 $this->sql_cache_valid = false; 00554 if ($value === null) { 00555 $this->setNull($paramIndex); 00556 } else { 00557 $this->boundInVars[$paramIndex] = (int) $value; 00558 } 00559 }
PreparedStatementCommon::setInteger | ( | $ | paramIndex, | |
$ | value | |||
) |
Alias for setInt().
int | $paramIndex | |
int | $value |
Definition at line 566 of file PreparedStatementCommon.php.
References setInt().
00567 { 00568 $this->sql_cache_valid = false; 00569 $this->setInt($paramIndex, $value); 00570 }
PreparedStatementCommon::setLimit | ( | $ | v | ) |
PreparedStatementCommon::setNull | ( | $ | paramIndex | ) |
int | $paramIndex |
Reimplemented in MSSQLCallableStatement, ODBCPreparedStatement, and OCI8PreparedStatement.
Definition at line 576 of file PreparedStatementCommon.php.
Referenced by PgSQLPreparedStatement::setArray(), setArray(), SQLitePreparedStatement::setBlob(), PgSQLPreparedStatement::setBlob(), MSSQLPreparedStatement::setBlob(), setBlob(), PgSQLPreparedStatement::setBoolean(), setBoolean(), setClob(), setDate(), setDecimal(), setDouble(), setFloat(), setInt(), setString(), PgSQLPreparedStatement::setTime(), setTime(), PgSQLPreparedStatement::setTimestamp(), and setTimestamp().
00577 { 00578 $this->sql_cache_valid = false; 00579 $this->boundInVars[$paramIndex] = 'NULL'; 00580 }
PreparedStatementCommon::setOffset | ( | $ | v | ) |
PreparedStatementCommon::setString | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
string | $value |
Reimplemented in OCI8PreparedStatement.
Definition at line 587 of file PreparedStatementCommon.php.
References escape(), and setNull().
00588 { 00589 $this->sql_cache_valid = false; 00590 if ($value === null) { 00591 $this->setNull($paramIndex); 00592 } else { 00593 // it's ok to have a fatal error here, IMO, if object doesn't have 00594 // __toString() and is being passed to this method. 00595 if ( is_object ( $value ) ) { 00596 $this->boundInVars[$paramIndex] = "'" . $this->escape($value->__toString()) . "'"; 00597 } else { 00598 $this->boundInVars[$paramIndex] = "'" . $this->escape((string)$value) . "'"; 00599 } 00600 } 00601 }
PreparedStatementCommon::setTime | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
string | $value |
Reimplemented in PgSQLPreparedStatement.
Definition at line 608 of file PreparedStatementCommon.php.
References escape(), and setNull().
00609 { 00610 $this->sql_cache_valid = false; 00611 if ($value === null) { 00612 $this->setNull($paramIndex); 00613 } else { 00614 if ( is_numeric ( $value ) ) { 00615 $value = date ('H:i:s', $value ); 00616 } elseif ( is_object ( $value ) ) { 00617 $value = date ('H:i:s', $value->getTime ( ) ); 00618 } 00619 $this->boundInVars [ $paramIndex ] = "'" . $this->escape ( $value ) . "'"; 00620 } 00621 }
PreparedStatementCommon::setTimestamp | ( | $ | paramIndex, | |
$ | value | |||
) |
int | $paramIndex | |
string | $value |
Reimplemented in OCI8PreparedStatement, and PgSQLPreparedStatement.
Definition at line 628 of file PreparedStatementCommon.php.
References setNull().
Referenced by set().
00629 { 00630 $this->sql_cache_valid = false; 00631 if ($value === null) { 00632 $this->setNull($paramIndex); 00633 } else { 00634 if (is_numeric($value)) $value = date('Y-m-d H:i:s', $value); 00635 elseif (is_object($value)) $value = date('Y-m-d H:i:s', $value->getTime()); 00636 $this->boundInVars[$paramIndex] = "'".$this->escape($value)."'"; 00637 } 00638 }
PreparedStatementCommon::$boundInVars = array() [protected] |
Definition at line 93 of file PreparedStatementCommon.php.
PreparedStatementCommon::$conn [protected] |
Definition at line 40 of file PreparedStatementCommon.php.
PreparedStatementCommon::$limit = 0 [protected] |
Definition at line 46 of file PreparedStatementCommon.php.
PreparedStatementCommon::$offset = 0 [protected] |
Definition at line 53 of file PreparedStatementCommon.php.
PreparedStatementCommon::$positions [protected] |
PreparedStatementCommon::$positionsCount [protected] |
Definition at line 87 of file PreparedStatementCommon.php.
PreparedStatementCommon::$resultSet [protected] |
Definition at line 99 of file PreparedStatementCommon.php.
PreparedStatementCommon::$sql [protected] |
Definition at line 59 of file PreparedStatementCommon.php.
Referenced by __construct(), ODBCPreparedStatement::_execute(), OCI8PreparedStatement::executeQuery(), ODBCPreparedStatement::executeQuery(), MSSQLPreparedStatement::executeQuery(), executeQuery(), ODBCPreparedStatement::executeUpdate(), executeUpdate(), parseQuery(), replaceParams(), and OCI8PreparedStatement::sqlToOracleBindVars().
PreparedStatementCommon::$sql_cache [protected] |
Definition at line 68 of file PreparedStatementCommon.php.
PreparedStatementCommon::$sql_cache_valid = false [protected] |
Definition at line 74 of file PreparedStatementCommon.php.
PreparedStatementCommon::$updateCount [protected] |
Definition at line 105 of file PreparedStatementCommon.php.