[aur-dev] [RFC] aurjson: add result count to JSON result
We already ask for the result count, but only use it as a basis for testing query success or failure. Add the value to the JSON reply. Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- I call this an RFC because: a) I don't have a test environment setup b) I don't know PHP ...but I really like the idea of knowing ahead of time how many results you're about to receive. web/lib/aurjson.class.php | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 234a3c4..23eb854 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -103,7 +103,7 @@ class AurJSON { private function json_error($msg) { // set content type header to app/json header('content-type: application/json'); - return $this->json_results('error', $msg); + return $this->json_results('error', 0, $msg); } /** @@ -112,8 +112,8 @@ class AurJSON { * @param $data The result data to return * @return mixed A json formatted result response. **/ - private function json_results($type, $data) { - return json_encode( array('type' => $type, 'results' => $data) ); + private function json_results($type, $count, $data) { + return json_encode( array('type' => $type, 'resultcount' => $count, 'results' => $data) ); } private function process_query($type, $where_condition) { @@ -124,7 +124,8 @@ class AurJSON { "WHERE ${where_condition}"; $result = db_query($query, $this->dbh); - if ( $result && (mysql_num_rows($result) > 0) ) { + $resultcount = mysql_num_rows($result); + if ( $result && $resultcount > 0 ) { $search_data = array(); while ( $row = mysql_fetch_assoc($result) ) { $name = $row['Name']; @@ -148,7 +149,7 @@ class AurJSON { } mysql_free_result($result); - return $this->json_results($type, $search_data); + return $this->json_results($type, $resultcount, $search_data); } else { return $this->json_error('No results found'); -- 1.7.7.1
On Tue, Oct 25, 2011 at 06:39:52PM -0400, Dave Reisner wrote:
We already ask for the result count, but only use it as a basis for testing query success or failure. Add the value to the JSON reply.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- I call this an RFC because:
a) I don't have a test environment setup b) I don't know PHP
...but I really like the idea of knowing ahead of time how many results you're about to receive.
Just tested it, seems to work! Looks fine to me, also :) Is there any use case for this yet? Just out of curiosity...
web/lib/aurjson.class.php | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 234a3c4..23eb854 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -103,7 +103,7 @@ class AurJSON { private function json_error($msg) { // set content type header to app/json header('content-type: application/json'); - return $this->json_results('error', $msg); + return $this->json_results('error', 0, $msg); }
/** @@ -112,8 +112,8 @@ class AurJSON { * @param $data The result data to return * @return mixed A json formatted result response. **/ - private function json_results($type, $data) { - return json_encode( array('type' => $type, 'results' => $data) ); + private function json_results($type, $count, $data) { + return json_encode( array('type' => $type, 'resultcount' => $count, 'results' => $data) ); }
private function process_query($type, $where_condition) { @@ -124,7 +124,8 @@ class AurJSON { "WHERE ${where_condition}"; $result = db_query($query, $this->dbh);
- if ( $result && (mysql_num_rows($result) > 0) ) { + $resultcount = mysql_num_rows($result); + if ( $result && $resultcount > 0 ) { $search_data = array(); while ( $row = mysql_fetch_assoc($result) ) { $name = $row['Name']; @@ -148,7 +149,7 @@ class AurJSON { }
mysql_free_result($result); - return $this->json_results($type, $search_data); + return $this->json_results($type, $resultcount, $search_data); } else { return $this->json_error('No results found'); -- 1.7.7.1
On Wed, Nov 2, 2011 at 3:17 PM, Lukas Fleischer <archlinux@cryptocrack.de> wrote:
On Tue, Oct 25, 2011 at 06:39:52PM -0400, Dave Reisner wrote:
We already ask for the result count, but only use it as a basis for testing query success or failure. Add the value to the JSON reply.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- I call this an RFC because:
a) I don't have a test environment setup b) I don't know PHP
...but I really like the idea of knowing ahead of time how many results you're about to receive.
Just tested it, seems to work! Looks fine to me, also :)
Is there any use case for this yet? Just out of curiosity...
Obviously you shouldn't treat the number as gospel and ensure you don't overflow, but if you want to preallocate space or something like that for the result it might be useful. -Dan
On Wed, Nov 02, 2011 at 09:17:47PM +0100, Lukas Fleischer wrote:
On Tue, Oct 25, 2011 at 06:39:52PM -0400, Dave Reisner wrote:
We already ask for the result count, but only use it as a basis for testing query success or failure. Add the value to the JSON reply.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- I call this an RFC because:
a) I don't have a test environment setup b) I don't know PHP
...but I really like the idea of knowing ahead of time how many results you're about to receive.
Just tested it, seems to work! Looks fine to me, also :)
Is there any use case for this yet? Just out of curiosity...
Not yet. I guess I had 2 thoughts: 1) folks like me who enjoy pain (C) can use this for a little optimization and "plan head" in terms of memory allocation. 2) I think its a bit strange that 0 results is considered an error. If we had a result count in the JSON reply, we'd be able to change this. d
web/lib/aurjson.class.php | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 234a3c4..23eb854 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -103,7 +103,7 @@ class AurJSON { private function json_error($msg) { // set content type header to app/json header('content-type: application/json'); - return $this->json_results('error', $msg); + return $this->json_results('error', 0, $msg); }
/** @@ -112,8 +112,8 @@ class AurJSON { * @param $data The result data to return * @return mixed A json formatted result response. **/ - private function json_results($type, $data) { - return json_encode( array('type' => $type, 'results' => $data) ); + private function json_results($type, $count, $data) { + return json_encode( array('type' => $type, 'resultcount' => $count, 'results' => $data) ); }
private function process_query($type, $where_condition) { @@ -124,7 +124,8 @@ class AurJSON { "WHERE ${where_condition}"; $result = db_query($query, $this->dbh);
- if ( $result && (mysql_num_rows($result) > 0) ) { + $resultcount = mysql_num_rows($result); + if ( $result && $resultcount > 0 ) { $search_data = array(); while ( $row = mysql_fetch_assoc($result) ) { $name = $row['Name']; @@ -148,7 +149,7 @@ class AurJSON { }
mysql_free_result($result); - return $this->json_results($type, $search_data); + return $this->json_results($type, $resultcount, $search_data); } else { return $this->json_error('No results found'); -- 1.7.7.1
On Wed, Nov 02, 2011 at 04:52:02PM -0400, Dave Reisner wrote:
On Wed, Nov 02, 2011 at 09:17:47PM +0100, Lukas Fleischer wrote:
On Tue, Oct 25, 2011 at 06:39:52PM -0400, Dave Reisner wrote:
We already ask for the result count, but only use it as a basis for testing query success or failure. Add the value to the JSON reply.
Signed-off-by: Dave Reisner <dreisner@archlinux.org> --- I call this an RFC because:
a) I don't have a test environment setup b) I don't know PHP
...but I really like the idea of knowing ahead of time how many results you're about to receive.
Just tested it, seems to work! Looks fine to me, also :)
Is there any use case for this yet? Just out of curiosity...
Not yet. I guess I had 2 thoughts:
1) folks like me who enjoy pain (C) can use this for a little optimization and "plan head" in terms of memory allocation.
Agreed, looks like we can speed up memory allocation here (O(1) instead of O(n) or so)... Hopefully, this won't be the basis for another couple of buffer overflows in some AUR helpers (I'm not talking about cower).
2) I think its a bit strange that 0 results is considered an error. If we had a result count in the JSON reply, we'd be able to change this.
Oh, I wasn't aware of this. Makes sense. Thanks!
d
web/lib/aurjson.class.php | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 234a3c4..23eb854 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -103,7 +103,7 @@ class AurJSON { private function json_error($msg) { // set content type header to app/json header('content-type: application/json'); - return $this->json_results('error', $msg); + return $this->json_results('error', 0, $msg); }
/** @@ -112,8 +112,8 @@ class AurJSON { * @param $data The result data to return * @return mixed A json formatted result response. **/ - private function json_results($type, $data) { - return json_encode( array('type' => $type, 'results' => $data) ); + private function json_results($type, $count, $data) { + return json_encode( array('type' => $type, 'resultcount' => $count, 'results' => $data) ); }
private function process_query($type, $where_condition) { @@ -124,7 +124,8 @@ class AurJSON { "WHERE ${where_condition}"; $result = db_query($query, $this->dbh);
- if ( $result && (mysql_num_rows($result) > 0) ) { + $resultcount = mysql_num_rows($result); + if ( $result && $resultcount > 0 ) { $search_data = array(); while ( $row = mysql_fetch_assoc($result) ) { $name = $row['Name']; @@ -148,7 +149,7 @@ class AurJSON { }
mysql_free_result($result); - return $this->json_results($type, $search_data); + return $this->json_results($type, $resultcount, $search_data); } else { return $this->json_error('No results found'); -- 1.7.7.1
participants (3)
-
Dan McGee
-
Dave Reisner
-
Lukas Fleischer