Spaces:
Sleeping
Sleeping
File size: 9,301 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | <?php
/*
* $Id: StatementCommon.php,v 1.4 2004/06/13 02:31:07 hlellelid Exp $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://creole.phpdb.org>.
*/
/**
* Class that contains common/shared functionality for Statements.
*
* @author Hans Lellelid <hans@xmpl.org>
* @version $Revision: 1.4 $
* @package creole.common
*/
abstract class StatementCommon {
/**
* The database connection.
* @var Connection
*/
protected $conn;
/**
* Temporarily hold a ResultSet object after an execute() query.
* @var ResultSet
*/
protected $resultSet;
/**
* Temporary hold the affected row cound after an execute() query.
* @var int
*/
protected $updateCount;
/**
* Array of warning objects generated by methods performed on result set.
* @var array SQLWarning[]
*/
protected $warnings = array();
/**
* The ResultSet class name.
* @var string
*/
protected $resultClass;
/**
* The prepared statement resource id.
* @var resource
*/
protected $stmt;
/**
* Max rows to retrieve from DB.
* @var int
*/
protected $limit = 0;
/**
* Offset at which to start processing DB rows.
* "Skip X rows"
* @var int
*/
protected $offset = 0;
/**
* Create new statement instance.
*
* @param Connection $conn Connection object
*/
function __construct(Connection $conn)
{
$this->conn = $conn;
}
/**
* Sets the maximum number of rows to return from db.
* This will affect the SQL if the RDBMS supports native LIMIT; if not,
* it will be emulated. Limit only applies to queries (not update sql).
* @param int $v Maximum number of rows or 0 for all rows.
* @return void
*/
public function setLimit($v)
{
$this->limit = (int) $v;
}
/**
* Returns the maximum number of rows to return or 0 for all.
* @return int
*/
public function getLimit()
{
return $this->limit;
}
/**
* Sets the start row.
* This will affect the SQL if the RDBMS supports native OFFSET; if not,
* it will be emulated. Offset only applies to queries (not update) and
* only is evaluated when LIMIT is set!
* @param int $v
* @return void
*/
public function setOffset($v)
{
$this->offset = (int) $v;
}
/**
* Returns the start row.
* Offset only applies when Limit is set!
* @return int
*/
public function getOffset()
{
return $this->offset;
}
/**
* Free resources associated with this statement.
* Some drivers will need to implement this method to free
* database result resources.
*
* @return void
*/
public function close()
{
// do nothing here (subclasses will implement)
}
/**
* Generic execute() function has to check to see whether SQL is an update or select query.
*
* If you already know whether it's a SELECT or an update (manipulating) SQL, then use
* the appropriate method, as this one will incurr overhead to check the SQL.
*
* @param int $fetchmode Fetchmode (only applies to queries).
* @return boolean True if it is a result set, false if not or if no more results (this is identical to JDBC return val).
* @throws SQLException
* @todo -cStatementCommon Update execute() to not use isSelect() method, but rather to determine type based on returned results.
*/
public function execute($sql, $fetchmode = null)
{
if (!$this->isSelect($sql)) {
$this->updateCount = $this->executeUpdate($sql);
return false;
} else {
$this->resultSet = $this->executeQuery($sql, $fetchmode);
if ($this->resultSet->getRecordCount() === 0) {
return false;
}
return true;
}
}
/**
* Get result set.
* This assumes that the last thing done was an executeQuery() or an execute()
* with SELECT-type query.
*
* @return RestultSet (or null if none)
*/
public function getResultSet()
{
return $this->resultSet;
}
/**
* Get update count.
*
* @return int Number of records affected, or <code>null</code> if not applicable.
*/
public function getUpdateCount()
{
return $this->updateCount;
}
/**
* Returns whether the passed SQL is a SELECT statement.
*
* Returns true if SQL starts with 'SELECT' but not 'SELECT INTO'. This exists
* to support the execute() function -- which could either execute an update or
* a query.
*
* Currently this function does not take into consideration comments, primarily
* because there are a number of different comment options for different drivers:
* <pre>
* -- SQL-defined comment, but not truly comment in Oracle
* # comment in mysql
* /* comment in mssql, others * /
* // comment sometimes?
* REM also comment ...
* </pre>
*
* If you're wondering why we can't just execute the query and look at the return results
* to see whether it was an update or a select, the reason is that for update queries we
* need to do stuff before we execute them -- like start transactions if auto-commit is off.
*
* @param string $sql
* @return boolean Whether statement is a SELECT SQL statement.
* @see execute()
*/
protected function isSelect($sql)
{
// is first word is SELECT, then return true, unless it's SELECT INTO ...
// this doesn't, however, take comments into account ...
$sql = trim($sql);
return (stripos($sql, 'select') === 0 && stripos($sql, 'select into ') !== 0);
}
/**
* Executes the SQL query in this PreparedStatement object and returns the resultset generated by the query.
*
* @param string $sql This method may optionally be called with the SQL statement.
* @param int $fetchmode The mode to use when fetching the results (e.g. ResultSet::FETCHMODE_NUM, ResultSet::FETCHMODE_ASSOC).
* @return object Creole::ResultSet
* @throws SQLException If there is an error executing the specified query.
* @todo -cStatementCommon Put native query execution logic in statement subclasses.
*/
public function executeQuery($sql, $fetchmode = null)
{
$this->updateCount = null;
if ($this->limit > 0 || $this->offset > 0) {
$this->conn->applyLimit($sql, $this->offset, $this->limit);
}
$this->resultSet = $this->conn->executeQuery($sql, $fetchmode);
return $this->resultSet;
}
/**
* Executes the SQL INSERT, UPDATE, or DELETE statement in this PreparedStatement object.
*
* @param string $sql This method may optionally be called with the SQL statement.
* @return int Number of affected rows (or 0 for drivers that return nothing).
* @throws SQLException if a database access error occurs.
*/
public function executeUpdate($sql)
{
if ($this->resultSet) $this->resultSet->close();
$this->resultSet = null;
$this->updateCount = $this->conn->executeUpdate($sql);
return $this->updateCount;
}
/**
* Gets next result set (if this behavior is supported by driver).
* Some drivers (e.g. MSSQL) support returning multiple result sets -- e.g.
* from stored procedures.
*
* This function also closes any current restult set.
*
* Default behavior is for this function to return false. Driver-specific
* implementations of this class can override this method if they actually
* support multiple result sets.
*
* @return boolean True if there is another result set, otherwise false.
*/
public function getMoreResults()
{
if ($this->resultSet) $this->resultSet->close();
$this->resultSet = null;
return false;
}
/**
* Gets the db Connection that created this statement.
* @return Connection
*/
public function getConnection()
{
return $this->conn;
}
}
|