From d9986cb2056c3fb8b56f0e2a2091f7498684b1d6 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 5 Jul 2021 11:33:47 +0200 Subject: [PATCH 1/3] fix(database): properly cast to int --- app/controllers/api/database.php | 8 +++++++- src/Appwrite/Database/Adapter.php | 3 ++- src/Appwrite/Database/Adapter/MySQL.php | 12 ++++++++++-- src/Appwrite/Database/Adapter/Redis.php | 5 +++-- src/Appwrite/Database/Database.php | 5 +++-- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 5af0e6ab4e..4c87a6be12 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -436,6 +436,12 @@ App::get('/v1/database/collections/:collectionId/documents') throw new Exception('Collection not found', 404); } + $types = []; + foreach ($collection->getAttribute('rules') as $rule) { + /** @var Document $rule */ + $types[$rule->getAttribute('key')] = $rule->getAttribute('type'); + } + $list = $projectDB->getCollection([ 'limit' => $limit, 'offset' => $offset, @@ -446,7 +452,7 @@ App::get('/v1/database/collections/:collectionId/documents') 'filters' => \array_merge($filters, [ '$collection='.$collectionId, ]), - ]); + ], $types); // if (App::isDevelopment()) { // $collection diff --git a/src/Appwrite/Database/Adapter.php b/src/Appwrite/Database/Adapter.php index bb650e844f..d8a1d0ecfc 100644 --- a/src/Appwrite/Database/Adapter.php +++ b/src/Appwrite/Database/Adapter.php @@ -130,10 +130,11 @@ abstract class Adapter * Filter data sets using chosen queries * * @param array $options + * @param array $filterTypes * * @return array */ - abstract public function getCollection(array $options); + abstract public function getCollection(array $options, array $filterTypes = []); /** * @param array $options diff --git a/src/Appwrite/Database/Adapter/MySQL.php b/src/Appwrite/Database/Adapter/MySQL.php index 86d79a1122..75c9f50169 100644 --- a/src/Appwrite/Database/Adapter/MySQL.php +++ b/src/Appwrite/Database/Adapter/MySQL.php @@ -517,12 +517,13 @@ class MySQL extends Adapter * Get Collection. * * @param array $options + * @param array $filterTypes * * @throws Exception * * @return array */ - public function getCollection(array $options) + public function getCollection(array $options, array $filterTypes = []) { $start = \microtime(true); $orderCastMap = [ @@ -568,8 +569,15 @@ class MySQL extends Adapter //$path = implode('.', $path); + $castToInt = array_key_exists($key, $filterTypes) && $filterTypes[$key] === 'numeric'; + $key = $this->getPDO()->quote($key, PDO::PARAM_STR); $value = $this->getPDO()->quote($value, PDO::PARAM_STR); + + if ($castToInt) { + $value .= '+0'; + } + //$path = $this->getPDO()->quote($path, PDO::PARAM_STR); $options['offset'] = (int) $options['offset']; $options['limit'] = (int) $options['limit']; @@ -676,7 +684,7 @@ class MySQL extends Adapter ORDER BY sort_ff {$options['orderType']} %s"; $st = $this->getPDO()->prepare(\sprintf($query, $select, $range)); - + var_dump(\sprintf($query, $select, $range)); $st->execute(); $results = ['data' => []]; diff --git a/src/Appwrite/Database/Adapter/Redis.php b/src/Appwrite/Database/Adapter/Redis.php index 4e5619c5ff..68946b0469 100644 --- a/src/Appwrite/Database/Adapter/Redis.php +++ b/src/Appwrite/Database/Adapter/Redis.php @@ -210,14 +210,15 @@ class Redis extends Adapter /** * @param array $options + * @param array $filterTypes * * @return array * * @throws Exception */ - public function getCollection(array $options) + public function getCollection(array $options, array $filterTypes = []) { - $data = $this->adapter->getCollection($options); + $data = $this->adapter->getCollection($options, $filterTypes); $keys = []; foreach ($data as $node) { diff --git a/src/Appwrite/Database/Database.php b/src/Appwrite/Database/Database.php index bf1033eb81..400a145168 100644 --- a/src/Appwrite/Database/Database.php +++ b/src/Appwrite/Database/Database.php @@ -145,10 +145,11 @@ class Database /** * @param array $options + * @param array $filterTypes * * @return Document[] */ - public function getCollection(array $options) + public function getCollection(array $options, array $filterTypes = []) { $options = \array_merge([ 'offset' => 0, @@ -161,7 +162,7 @@ class Database 'filters' => [], ], $options); - $results = $this->adapter->getCollection($options); + $results = $this->adapter->getCollection($options, $filterTypes); foreach ($results as &$node) { $node = $this->decode(new Document($node)); From e5c65cce1aec27a9ceaccd09602e393957c58dfd Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 5 Jul 2021 11:36:40 +0200 Subject: [PATCH 2/3] remove debug logs --- src/Appwrite/Database/Adapter/MySQL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Database/Adapter/MySQL.php b/src/Appwrite/Database/Adapter/MySQL.php index 75c9f50169..9ea2c7cb2a 100644 --- a/src/Appwrite/Database/Adapter/MySQL.php +++ b/src/Appwrite/Database/Adapter/MySQL.php @@ -684,7 +684,7 @@ class MySQL extends Adapter ORDER BY sort_ff {$options['orderType']} %s"; $st = $this->getPDO()->prepare(\sprintf($query, $select, $range)); - var_dump(\sprintf($query, $select, $range)); + $st->execute(); $results = ['data' => []]; From 04f529d7db802ca245ad7ce031cb527e51c2132a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 6 Jul 2021 09:01:12 +0200 Subject: [PATCH 3/3] cast in php and skip quoting --- src/Appwrite/Database/Adapter/MySQL.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Database/Adapter/MySQL.php b/src/Appwrite/Database/Adapter/MySQL.php index 9ea2c7cb2a..fc3370c778 100644 --- a/src/Appwrite/Database/Adapter/MySQL.php +++ b/src/Appwrite/Database/Adapter/MySQL.php @@ -569,14 +569,13 @@ class MySQL extends Adapter //$path = implode('.', $path); - $castToInt = array_key_exists($key, $filterTypes) && $filterTypes[$key] === 'numeric'; + if(array_key_exists($key, $filterTypes) && $filterTypes[$key] === 'numeric') { + $value = (float) $value; + } else { + $value = $this->getPDO()->quote($value, PDO::PARAM_STR); + } $key = $this->getPDO()->quote($key, PDO::PARAM_STR); - $value = $this->getPDO()->quote($value, PDO::PARAM_STR); - - if ($castToInt) { - $value .= '+0'; - } //$path = $this->getPDO()->quote($path, PDO::PARAM_STR); $options['offset'] = (int) $options['offset'];