diff options
Diffstat (limited to 'lib/public/appframework/db/mapper.php')
-rw-r--r-- | lib/public/appframework/db/mapper.php | 73 |
1 files changed, 52 insertions, 21 deletions
diff --git a/lib/public/appframework/db/mapper.php b/lib/public/appframework/db/mapper.php index 5143547c8e8..9a5d6e819b9 100644 --- a/lib/public/appframework/db/mapper.php +++ b/lib/public/appframework/db/mapper.php @@ -26,7 +26,8 @@ namespace OCP\AppFramework\Db; -use \OCP\IDBConnection; +use OCP\IDBConnection; +use OCP\IDb; /** @@ -183,6 +184,31 @@ abstract class Mapper { return $entity; } + /** + * Checks if an array is associative + * @param array $array + * @return bool true if associative + */ + private function isAssocArray(array $array) { + return array_values($array) !== $array; + } + + /** + * Returns the correct PDO constant based on the value type + * @param $value + * @return PDO constant + */ + private function getPDOType($value) { + switch (gettype($value)) { + case 'integer': + return \PDO::PARAM_INT; + case 'boolean': + return \PDO::PARAM_BOOL; + default: + return \PDO::PARAM_STR; + } + } + /** * Runs an sql query @@ -193,32 +219,37 @@ abstract class Mapper { * @return \PDOStatement the database query result */ protected function execute($sql, array $params=[], $limit=null, $offset=null){ - $query = $this->db->prepare($sql, $limit, $offset); - - $index = 1; // bindParam is 1 indexed - foreach($params as $param) { - - switch (gettype($param)) { - case 'integer': - $pdoConstant = \PDO::PARAM_INT; - break; - - case 'boolean': - $pdoConstant = \PDO::PARAM_BOOL; - break; + if ($this->db instanceof IDb) { + $query = $this->db->prepareQuery($sql, $limit, $offset); + } else { + $query = $this->db->prepare($sql, $limit, $offset); + } - default: - $pdoConstant = \PDO::PARAM_STR; - break; + if ($this->isAssocArray($params)) { + foreach ($params as $key => $param) { + $pdoConstant = $this->getPDOType($param); + $query->bindValue($key, $param, $pdoConstant); } + } else { + $index = 1; // bindParam is 1 indexed + foreach ($params as $param) { + $pdoConstant = $this->getPDOType($param); + $query->bindValue($index, $param, $pdoConstant); + $index++; + } + } - $query->bindValue($index, $param, $pdoConstant); + $result = $query->execute(); - $index++; + // this is only for backwards compatibility reasons and can be removed + // in owncloud 10. IDb returns a StatementWrapper from execute, PDO, + // Doctrine and IDbConnection don't so this needs to be done in order + // to stay backwards compatible for the things that rely on the + // StatementWrapper being returned + if ($result instanceof \OC_DB_StatementWrapper) { + return $result; } - $query->execute(); - return $query; } |