diff options
author | Jörn Friedrich Dreyer <jfd@butonic.de> | 2013-06-20 16:46:22 +0400 |
---|---|---|
committer | Jörn Friedrich Dreyer <jfd@butonic.de> | 2013-07-04 17:48:45 +0400 |
commit | dc748f3ced7fd8e362acb15b356de4ee07719478 (patch) | |
tree | 97a94271fca3f5104a5af5fcec563da52e19ef4a /lib/db.php | |
parent | a902e5048a00f0fdad5fa4132416db6b1d8b4125 (diff) |
make PDOStatementWrapper return number of updated rows on INSERT, UPDATE or DELETE queries, introduces isManipulation() to guess type of query
Diffstat (limited to 'lib/db.php')
-rw-r--r-- | lib/db.php | 80 |
1 files changed, 65 insertions, 15 deletions
diff --git a/lib/db.php b/lib/db.php index cd010d53104..3112edb4d43 100644 --- a/lib/db.php +++ b/lib/db.php @@ -329,7 +329,7 @@ class OC_DB { * * SQL query via MDB2 prepare(), needs to be execute()'d! */ - static public function prepare( $query , $limit=null, $offset=null ) { + static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) { if (!is_null($limit) && $limit != -1) { if (self::$backend == self::BACKEND_MDB2) { @@ -364,12 +364,23 @@ class OC_DB { $query = self::processQuery( $query ); self::connect(); + + if ($isManipulation === null) { + //try to guess, so we return the number of rows on manipulations + $isManipulation = self::isManipulation($query); + } + // return the result if(self::$backend==self::BACKEND_MDB2) { - $result = self::$connection->prepare( $query ); + // differentiate between query and manipulation + if ($isManipulation) { + $result = self::$connection->prepare( $query, null, MDB2_PREPARE_MANIP ); + } else { + $result = self::$connection->prepare( $query, null, MDB2_PREPARE_RESULT ); + } // Die if we have an error (error means: bad query, not 0 results!) - if( PEAR::isError($result)) { + if( self::isError($result)) { throw new DatabaseException($result->getMessage(), $query); } }else{ @@ -378,7 +389,12 @@ class OC_DB { }catch(PDOException $e) { throw new DatabaseException($e->getMessage(), $query); } - $result=new PDOStatementWrapper($result); + // differentiate between query and manipulation + if ($isManipulation) { + $result=new PDOStatementWrapper($result, true); + } else { + $result=new PDOStatementWrapper($result, false); + } } if ((is_null($limit) || $limit == -1) and self::$cachingEnabled ) { $type = OC_Config::getValue( "dbtype", "sqlite" ); @@ -388,7 +404,33 @@ class OC_DB { } return $result; } - + + /** + * tries to guess the type of statement based on the first 10 characters + * the current check allows some whitespace but does not work with IF EXISTS or other more complex statements + * + * @param string $sql + */ + static public function isManipulation( $sql ) { + $selectOccurence = stripos ($sql, "SELECT"); + if ($selectOccurence !== false && $selectOccurence < 10) { + return false; + } + $insertOccurence = stripos ($sql, "INSERT"); + if ($insertOccurence !== false && $insertOccurence < 10) { + return true; + } + $updateOccurence = stripos ($sql, "UPDATE"); + if ($updateOccurence !== false && $updateOccurence < 10) { + return true; + } + $deleteOccurance = stripos ($sql, "DELETE"); + if ($deleteOccurance !== false && $deleteOccurance < 10) { + return true; + } + return false; + } + /** * @brief gets last value of autoincrement * @param string $table The optional table name (will replace *PREFIX*) and add sequence suffix @@ -690,6 +732,9 @@ class OC_DB { error_log('DB error: ' . $entry); OC_Template::printErrorPage( $entry ); } + if ($result === 0) { + return true; + } return $result->execute($inserts); } @@ -891,7 +936,7 @@ class OC_DB { * @return bool */ public static function isError($result) { - if(!$result) { + if(self::$backend==self::BACKEND_PDO and $result === false) { return true; }elseif(self::$backend==self::BACKEND_MDB2 and PEAR::isError($result)) { return true; @@ -946,11 +991,13 @@ class PDOStatementWrapper{ /** * @var PDOStatement */ - private $statement=null; - private $lastArguments=array(); + private $statement = null; + private $isManipulation = false; + private $lastArguments = array(); - public function __construct($statement) { - $this->statement=$statement; + public function __construct($statement, $isManipulation = false) { + $this->statement = $statement; + $this->isManipulation = $isManipulation; } /** @@ -968,16 +1015,19 @@ class PDOStatementWrapper{ $input = $this->tryFixSubstringLastArgumentDataForMSSQL($input); } - $result=$this->statement->execute($input); + $result = $this->statement->execute($input); } else { - $result=$this->statement->execute(); + $result = $this->statement->execute(); } - if ($result) { - return $this; - } else { + if ($result === false) { return false; } + if ($this->isManipulation) { + return $this->statement->rowCount(); + } else { + return $this; + } } private function tryFixSubstringLastArgumentDataForMSSQL($input) { |