Permalink
Browse files

added errstr_key process for rds_json_output

  • Loading branch information...
1 parent 25a5efc commit 8f303ee35d1f55707bfb436d9cc1924b45edad5e @liseen liseen committed May 22, 2012
Showing with 81 additions and 40 deletions.
  1. +9 −3 src/ngx_http_rds_json_handler.c
  2. +48 −36 src/ngx_http_rds_json_output.c
  3. +24 −1 t/property.t
@@ -35,18 +35,24 @@ ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
/* calculate the buffer size */
- len = sizeof("{:}") - 1
+ len = sizeof("{") - 1
+ conf->errcode_key.len
+ + sizeof(":") - 1
+ conf->errcode.len
+ + sizeof("}") - 1
;
if (errstr.len) {
escape = ngx_http_rds_json_escape_json_str(NULL,
errstr.data, errstr.len);
- len += sizeof(",:\"\"") - 1
+ len += sizeof(",") - 1
+ conf->errstr_key.len
- + errstr.len + escape
+ + sizeof(":") - 1
+ + sizeof("\"") - 1
+ + errstr.len
+ + escape
+ + sizeof("\"") - 1
;
}
@@ -137,10 +137,29 @@ ngx_http_rds_json_output_header(ngx_http_request_t *r,
/* calculate the buffer size */
- size = sizeof("{\"errcode\":") - 1
- + ngx_get_num_size(header->std_errcode)
- + sizeof("}") - 1
- ;
+ size = sizeof("{") - 1
+ + conf->errcode_key.len
+ + sizeof(":") - 1
+- + ngx_get_num_size(header->std_errcode)
+ + sizeof("}") - 1
+ ;
+
+ if (header->errstr.len) {
+ escape = ngx_http_rds_json_escape_json_str(NULL, header->errstr.data,
+ header->errstr.len);
+
+ size += sizeof(",") - 1
+ + conf->errstr_key.len
+ + sizeof(":") - 1
+ + sizeof("\"") - 1
+ + header->errstr.len
+ + escape
+ + sizeof("\"") - 1
+ ;
+
+ } else {
+ escape = (uintptr_t) 0;
+ }
if (conf->success.len) {
size += conf->success.len + sizeof(":,") - 1;
@@ -180,18 +199,6 @@ ngx_http_rds_json_output_header(ngx_http_request_t *r,
}
}
- if (header->errstr.len) {
- escape = ngx_http_rds_json_escape_json_str(NULL, header->errstr.data,
- header->errstr.len);
-
- size += sizeof(",\"errstr\":\"") - 1
- + header->errstr.len + escape
- + sizeof("\"") - 1
- ;
- } else {
- escape = (uintptr_t) 0;
- }
-
if (header->insert_id) {
size += sizeof(",\"insert_id\":") - 1
+ ngx_get_num_size(header->insert_id)
@@ -216,6 +223,31 @@ ngx_http_rds_json_output_header(ngx_http_request_t *r,
/* fill up the buffer */
*last++ = '{';
+ last = ngx_copy(last, conf->errcode_key.data, conf->errcode_key.len);
+ *last++ = ':';
+
+ last = ngx_snprintf(last, NGX_UINT16_LEN, "%uD",
+ (uint32_t) header->std_errcode);
+
+ if (header->errstr.len) {
+ *last++ = ',';
+ last = ngx_copy(last,
+ conf->errstr_key.data, conf->errstr_key.len);
+ *last++ = ':';
+ *last++ = '"';
+
+ if (escape == 0) {
+ last = ngx_copy(last, header->errstr.data,
+ header->errstr.len);
+
+ } else {
+ last = (u_char *) ngx_http_rds_json_escape_json_str(last,
+ header->errstr.data, header->errstr.len);
+ }
+
+ *last++ = '"';
+ }
+
if (conf->success.len) {
last = ngx_copy(last, conf->success.data, conf->success.len);
@@ -247,26 +279,6 @@ ngx_http_rds_json_output_header(ngx_http_request_t *r,
}
}
- last = ngx_copy_literal(last, "\"errcode\":");
-
- last = ngx_snprintf(last, NGX_UINT16_LEN, "%uD",
- (uint32_t) header->std_errcode);
-
- if (header->errstr.len) {
- last = ngx_copy_literal(last, ",\"errstr\":\"");
-
- if (escape == 0) {
- last = ngx_copy(last, header->errstr.data,
- header->errstr.len);
-
- } else {
- last = (u_char *) ngx_http_rds_json_escape_json_str(last,
- header->errstr.data, header->errstr.len);
- }
-
- *last++ = '"';
- }
-
if (header->insert_id) {
last = ngx_copy_literal(last, ",\"insert_id\":");
last = ngx_snprintf(last, NGX_UINT64_LEN, "%uL",
View
@@ -215,7 +215,7 @@ GET /mysql
GET /mysql
--- response_body chop
{"success":true,"errcode":0,"errstr":"Rows matched: 1 Changed: 0 Warnings: 0"}
-
+--- ONLY
=== TEST 12: update (user property)
@@ -536,3 +536,26 @@ GET /mysql
--- response_body chop
{"name":"Jimmy\"","age":"32","\"code\"":0,"\"str\"":"Rows matched: 1 Changed: 0 Warnings: 0"}
+
+=== TEST 30: select - custom errstr_key
+--- http_config eval: $::http_config
+--- config
+ location /mysql {
+ drizzle_pass backend;
+ drizzle_query "select 'aaa' as a, 'bbb' as b";
+ rds_json on;
+ rds_json_errcode_key "\"code\"";
+ rds_json_errstr_key "\"str\"";
+
+ rds_json_root data;
+
+ set $name 'Jimmy"';
+ set $age 32;
+ rds_json_user_property name $name;
+ rds_json_user_property age $age;
+ }
+--- request
+GET /mysql
+--- response_body chop
+{"name":"Jimmy\"","age":"32", "data": [ ] }
+

0 comments on commit 8f303ee

Please sign in to comment.