drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
json_handler.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20/*
21 * @file Implements a class JsonHandler which handles the operations related to Json.
22 */
23#include <plugin/json_server/json_handler.h>
24#include <string.h>
25using namespace drizzled;
26namespace drizzle_plugin
27{
28namespace json_server
29{
30 void JsonHandler::generate_input_query(struct evhttp_request *req_arg)
31 {
32 evhttp_parse_query(evhttp_request_uri(req_arg), req_arg->input_headers);
33 if(req_arg->type== EVHTTP_REQ_POST )
34 {
35 char buffer[1024];
36 int l=0;
37 do
38 {
39 l= evbuffer_remove(req_arg->input_buffer, buffer, 1024);
40 _input_query.append(buffer, l);
41 }
42 while(l);
43 }
44 else
45 {
46 const char* _query;
47 _query= (char *)evhttp_find_header(req_arg->input_headers, "query");
48 if(_query == NULL || strcmp(_query,"")==0)
49 {
50 _query="{}";
51 }
52 _input_query.append(_query,strlen(_query));
53 }
54
55 }
56
57 void JsonHandler::generate_input_json(struct evhttp_request *req_arg,JsonErrorArea &_json_error_area)
58 {
59 generate_input_query(req_arg);
60 Json::Features _json_conf;
61 Json::Reader reader(_json_conf);
62 bool retval = reader.parse(_input_query,_json_in);
63 if(retval!=true)
64 {
65 _json_error_area.set_error(JsonErrorArea::ER_JSON,drizzled::EE_OK,reader.getFormattedErrorMessages().c_str());
66 }
67 }
68
69 void JsonHandler::generate_output_json(JsonErrorArea& _json_error_area)
70 {
71 if(_json_error_area.is_error())
72 {
73 if(_json_error_area.is_sqlerror())
74 {
75 _json_out["error_type"]=_json_error_area.get_error_type_string();
76 _json_out["error_no"]=_json_error_area.get_error_no();
77 _json_out["error_message"]=_json_error_area.get_error_msg();
78 _json_out["sql_state"]=_json_error_area.get_sql_state();
79
80 }
81 else
82 {
83 _json_out["error_type"]=_json_error_area.get_error_type_string();
84 _json_out["error_message"]=_json_error_area.get_error_msg();
85 }
86 }
87 else
88 {
89 _json_out["sql_state"]=_json_error_area.get_sql_state();
90 }
91
92 }
93
94 void JsonHandler::generate_output_query(JsonErrorArea& _json_error_area)
95 {
96 generate_output_json(_json_error_area);
97 Json::StyledWriter writer;
98 _output_query= writer.write(_json_out);
99 }
100}
101}
TODO: Rename this file - func.h is stupid.