summaryrefslogtreecommitdiffstats
path: root/lib/public/appframework/db
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2015-03-19 14:55:21 +0100
committerBernhard Posselt <dev@bernhard-posselt.com>2015-03-19 17:08:46 +0100
commitdf24a014b8bc205e6bc047ead2af393b3e169542 (patch)
tree78648ce5663a2cda89e4107103d3d210116aea39 /lib/public/appframework/db
parent857b22c61be8f78581f41ac155efeff34ed335cd (diff)
downloadnextcloud-server-df24a014b8bc205e6bc047ead2af393b3e169542.tar.gz
nextcloud-server-df24a014b8bc205e6bc047ead2af393b3e169542.zip
If the execute method on the mapper receives an assoc array, it binds by value instead of index
Diffstat (limited to 'lib/public/appframework/db')
-rw-r--r--lib/public/appframework/db/mapper.php55
1 files changed, 36 insertions, 19 deletions
diff --git a/lib/public/appframework/db/mapper.php b/lib/public/appframework/db/mapper.php
index aaef0f79d27..9a5d6e819b9 100644
--- a/lib/public/appframework/db/mapper.php
+++ b/lib/public/appframework/db/mapper.php
@@ -184,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
@@ -200,26 +225,18 @@ abstract class Mapper {
$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;
-
- 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);
-
- $index++;
}
$result = $query->execute();