Skip to content

Commit

Permalink
Added access to previous PDOStatement through ::get_last_statement
Browse files Browse the repository at this point in the history
Addresses j4mie#84
  • Loading branch information
tag committed Nov 30, 2012
1 parent 309f7b6 commit fc4f9b6
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class ORM {
// Query cache, only used if query caching is enabled
protected static $_query_cache = array();

// Reference to previously used PDOStatement object to enable low-level access, if needed
protected static $_last_statement = null;

// --------------------------- //
// --- INSTANCE PROPERTIES --- //
// --------------------------- //
Expand Down Expand Up @@ -255,6 +258,14 @@ public static function get_db() {
return self::$_db;
}

/**
* Returns the PDOSatemen instance last used by the the ORM.
* Useful for access to PDOStatement::rowCount() or error information
*/
public static function get_last_statement() {
return self::_last_statement;
}

/**
* Executes a raw query as a wrapper for PDOStatement::execute.
* Useful for queries that can't be accomplished through Idiorm,
Expand All @@ -268,8 +279,21 @@ public static function get_db() {
public static function raw_execute($query, $parameters = array()) {
self::_setup_db();

return self::_execute($query, $parameters);
}

/**
* Internal helper method for executing statments. Logs queries, and
* stores statement object in ::_last_statment, accessible publicly
* through ::get_last_statement()
* @return bool Response of PDOStatement::execute()
**/
protected static function _execute($query, $parameters = array()) {
self::_log_query($query, $parameters);
$statement = self::$_db->prepare($query);

self::$_last_statement = $statement;

return $statement->execute($parameters);
}

Expand Down Expand Up @@ -1173,9 +1197,8 @@ protected function _run() {
}
}

self::_log_query($query, $this->_values);
$statement = self::$_db->prepare($query);
$statement->execute($this->_values);
self::_execute($query, $this->_values);
$statement = self::$_last_statement;

$rows = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
Expand Down Expand Up @@ -1315,9 +1338,7 @@ public function save() {
$query = $this->_build_insert();
}

self::_log_query($query, $values);
$statement = self::$_db->prepare($query);
$success = $statement->execute($values);
$success = self::_execute($query, $values);

// If we've just inserted a new record, set the ID of this object
if ($this->_is_new) {
Expand Down Expand Up @@ -1378,10 +1399,8 @@ public function delete() {
$this->_quote_identifier($this->_get_id_column_name()),
"= ?",
));
$params = array($this->id());
self::_log_query($query, $params);
$statement = self::$_db->prepare($query);
return $statement->execute($params);

return self::_execute($query, array($this->id()));
}

/**
Expand All @@ -1395,9 +1414,8 @@ public function delete_many() {
$this->_quote_identifier($this->_table_name),
$this->_build_where(),
));
self::_log_query($query, $this->_values);
$statement = self::$_db->prepare($query);
return $statement->execute($this->_values);

return self::_execute($query, $this->_values);
}

// --------------------- //
Expand Down

0 comments on commit fc4f9b6

Please sign in to comment.