Merge lp:~mohyt/drizzle/json_server_schema into lp:drizzle

Proposed by Mohit Srivastava
Status: Merged
Merged at revision: 2642
Proposed branch: lp:~mohyt/drizzle/json_server_schema
Merge into: lp:drizzle
Diff against target: 1049 lines (+935/-2)
10 files modified
plugin/json_server/ddl/schema.cc (+158/-0)
plugin/json_server/ddl/schema.h (+110/-0)
plugin/json_server/error.cc (+58/-0)
plugin/json_server/error.h (+158/-0)
plugin/json_server/json_handler.cc (+101/-0)
plugin/json_server/json_handler.h (+125/-0)
plugin/json_server/json_server.cc (+107/-2)
plugin/json_server/plugin.ini (+6/-0)
plugin/json_server/tests/r/basic.result (+18/-0)
plugin/json_server/tests/t/basic.test (+94/-0)
To merge this branch: bzr merge lp:~mohyt/drizzle/json_server_schema
Reviewer Review Type Date Requested Status
Stewart Smith (community) Approve
Review via email: mp+177285@code.launchpad.net

Description of the change

Added create & drop Schema functionality on json server

To post a comment you must log in.
Revision history for this message
Stewart Smith (stewart) wrote :

Minor fixes:
- Copyright date should be 2011-2013 rather than just 2011
- It would be good to have a comment in plugin/json_server/ddl/schema.cc that it's mostly just a copy from drizzled/statement/create_schema.cc and friends.
- Where you use a static buffer for processing input (buffer[1024]) please also provide i a test that tests the limits of this buffer to ensure that there are not exploitable buffer overflows.

Questions:
- Why is num_threads set to 1? I don't think this should be here.

Otherwise looks good.

review: Needs Fixing
lp:~mohyt/drizzle/json_server_schema updated
2639. By Mohit Srivastava

Changes related to Review.

Revision history for this message
Mohit Srivastava (mohyt) wrote :

Changes done. Ready to merge.
No change related to buffer overflow as it already handle by libevent.

Revision history for this message
Stewart Smith (stewart) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added directory 'plugin/json_server/ddl'
=== added file 'plugin/json_server/ddl/schema.cc'
--- plugin/json_server/ddl/schema.cc 1970-01-01 00:00:00 +0000
+++ plugin/json_server/ddl/schema.cc 2013-08-13 04:20:40 +0000
@@ -0,0 +1,158 @@
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 Schema to handle various operations related to schema.Its just a copy of drizzled/schema.cc and its friends.
22 */
23
24 #include <config.h>
25
26 #include <drizzled/show.h>
27 #include <drizzled/session.h>
28 #include <drizzled/schema.h>
29 #include <drizzled/message.h>
30 #include <drizzled/sql_lex.h>
31 #include <drizzled/plugin/event_observer.h>
32 #include <drizzled/catalog/instance.h>
33 #include <plugin/json_server/ddl/schema.h>
34 #include <drizzled/plugin/authorization.h>
35 #include <drizzled/plugin/storage_engine.h>
36
37 #include <string>
38
39
40 using namespace std;
41 using namespace drizzled;
42
43 namespace drizzle_plugin {
44 namespace json_server {
45
46 bool Schema::createSchema()
47 {
48 if (not validateSchemaOptions())
49 return true;
50
51 if (session().inTransaction())
52 {
53 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
54 return true;
55 }
56
57 drizzled::identifier::Schema schema_identifier(session().catalog().identifier(),_db_name);
58
59 if (not check(schema_identifier))
60 return false;
61
62 drizzled::message::schema::init(schema_message, schema_identifier);
63 message::set_definer(schema_message, *session().user());
64
65 bool res =false;
66 std::string path = schema_identifier.getSQLPath();
67
68 if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path)))
69 {
70 my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
71 }
72 else
73 {
74 res= schema::create(session(), schema_message, false);
75 if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res)))
76 {
77 my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
78 res = false;
79 }
80 }
81 return not res;
82 }
83
84 bool Schema::dropSchema()
85 {
86 if (session().inTransaction())
87 {
88 my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0));
89 return true;
90 }
91
92 drizzled::identifier::Schema schema_identifier(session().catalog().identifier(),_db_name);
93
94 if (not schema::check(session(),schema_identifier))
95 {
96 my_error(ER_WRONG_DB_NAME, schema_identifier);
97 return false;
98 }
99
100 if (session().inTransaction())
101 {
102 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
103 return true;
104 }
105
106 bool res = true;
107 std::string path = schema_identifier.getSQLPath();
108 if (unlikely(plugin::EventObserver::beforeDropDatabase(session(), path)))
109 {
110 my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier);
111 }
112 else
113 {
114 res= schema::drop(session(), schema_identifier, false);
115 if (unlikely(plugin::EventObserver::afterDropDatabase(session(), path, res)))
116 {
117 my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str());
118 res = false;
119 }
120 }
121
122 return res;
123
124 }
125
126 bool Schema::validateSchemaOptions()
127 {
128 size_t num_engine_options= schema_message.engine().options_size();
129 bool rc= num_engine_options ? false : true;
130
131 for (size_t y= 0; y < num_engine_options; ++y)
132 {
133 my_error(ER_UNKNOWN_SCHEMA_OPTION, MYF(0),schema_message.engine().options(y).name().c_str(),schema_message.engine().options(y).state().c_str());
134 rc= false;
135 }
136 return rc;
137
138 }
139
140 bool Schema::check(const identifier::Schema &identifier)
141 {
142 if (not identifier.isValid())
143 return false;
144
145 if (not plugin::Authorization::isAuthorized(*session().user(), identifier))
146 return false;
147
148 if (plugin::StorageEngine::doesSchemaExist(identifier))
149 {
150 my_error(ER_DB_CREATE_EXISTS, identifier);
151 return false;
152 }
153
154 return true;
155 }
156}
157}
158
0159
=== added file 'plugin/json_server/ddl/schema.h'
--- plugin/json_server/ddl/schema.h 1970-01-01 00:00:00 +0000
+++ plugin/json_server/ddl/schema.h 2013-08-13 04:20:40 +0000
@@ -0,0 +1,110 @@
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 Declare a class Schema to perform various operations related to schema.
22 */
23#include <drizzled/session.h>
24#include <drizzled/statement.h>
25#include <drizzled/message/schema.pb.h>
26#include <uuid/uuid.h>
27#include <drizzled/definitions.h>
28#include <drizzled/error.h>
29#include <drizzled/sql_parse.h>
30#include <drizzled/sql_base.h>
31#include <string>
32using namespace std;
33using namespace drizzled;
34namespace drizzle_plugin {
35namespace json_server {
36 /**
37 * a class.
38 *
39 * To perform various operations related to schema.
40 */
41 class Schema
42 {
43 public:
44 /**
45 * Constructor.
46 *
47 * @param in_session a session object.
48 * @param db_name a schema name string.
49 */
50 Schema(Session *in_session,string db_name) :
51 _session(*in_session),_db_name(db_name)
52 {}
53 /**
54 * Stores whether schema exist or not.
55 */
56 bool is_if_not_exists;
57 /**
58 * Stores schema message.
59 */
60 message::Schema schema_message;
61 /**
62 * create a new schema if it not exists.
63 *
64 * @return false Success.
65 * @return true Failure.
66 */
67 bool createSchema();
68 /**
69 * drop a schema if it exists.
70 *
71 * @return false Success.
72 * @reutrn true Failure.
73 */
74 bool dropSchema();
75 /**
76 * Get a session object.
77 *
78 * @return a session object.
79 */
80 Session& session() const{
81 return _session;
82 }
83
84 private:
85 /**
86 * Validates various schema options.
87 *
88 * @return false Success.
89 * @return true Failure.
90 */
91 bool validateSchemaOptions();
92 /**
93 * Checks whether schema exists or not already.
94 *
95 * @return false Success.
96 * @return true Failure.
97 */
98 bool check(const identifier::Schema &identifier);
99 /**
100 * Stores a session object.
101 */
102 Session& _session;
103 /**
104 * Stores a schema name.
105 */
106 string _db_name;
107 };
108
109}
110}
0111
=== added file 'plugin/json_server/error.cc'
--- plugin/json_server/error.cc 1970-01-01 00:00:00 +0000
+++ plugin/json_server/error.cc 2013-08-13 04:20:40 +0000
@@ -0,0 +1,58 @@
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 JsonErrorArea class which handles errors of json server.
22 */
23#include <plugin/json_server/error.h>
24#include <drizzled/error/sql_state.h>
25
26namespace drizzle_plugin
27{
28namespace json_server
29{
30 JsonErrorArea::JsonErrorArea()
31 {
32 reset_jsonerror_area();
33 }
34
35 void JsonErrorArea::reset_jsonerror_area()
36 {
37 er_type = ER_EMPTY;
38 error_no = drizzled::EE_OK;
39 error_msg= "";
40 sql_state="00000";
41 }
42
43 void JsonErrorArea::set_error(enum_error_type error_type_arg,drizzled::error_t error_no_arg,const char * error_msg_arg)
44 {
45 if(error_type_arg != ER_EMPTY)
46 {
47 er_type = error_type_arg;
48 error_msg = error_msg_arg;
49
50 if(error_type_arg == ER_SQL)
51 {
52 error_no = error_no_arg;
53 sql_state = drizzled::error::convert_to_sqlstate(error_no_arg);
54 }
55 }
56 }
57}
58}
059
=== added file 'plugin/json_server/error.h'
--- plugin/json_server/error.h 1970-01-01 00:00:00 +0000
+++ plugin/json_server/error.h 2013-08-13 04:20:40 +0000
@@ -0,0 +1,158 @@
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 Declare a class to handle errors in json server.
22 */
23#include <config.h>
24
25#include <drizzled/error_t.h>
26#include <string>
27namespace drizzle_plugin
28{
29namespace json_server
30{
31 /**
32 * a class
33 * used to handle various errors of json server.
34 */
35 class JsonErrorArea
36 {
37 public:
38 /**
39 * an enumerated data type for different type of error in json server.
40 */
41 enum enum_error_type
42 {
43 ER_EMPTY=0,
44 ER_SQL,
45 ER_JSON,
46 ER_HTTP,
47 ER_UNKNOWN
48 };
49 /**
50 * Constructor.
51 */
52 JsonErrorArea();
53 /**
54 * Reset the memeber values.
55 */
56 void reset_jsonerror_area();
57 /**
58 * Set an error with details.
59 */
60 void set_error(enum_error_type type,drizzled::error_t sql_errno_arg,const char *message_arg);
61 /**
62 * Check whether error or not.
63 *
64 * @return true Success.
65 * @return false Failure.
66 */
67 bool is_error(){return er_type!= ER_EMPTY;}
68 /**
69 * Check whether sql error or not.
70 *
71 * @return true Success.
72 * @return false Failure.
73 */
74 bool is_sqlerror(){return er_type == ER_SQL;}
75 /**
76 * Check whether json error or not.
77 *
78 * @return true Success.
79 * @return false Failure.
80 */
81 bool is_jsonerror(){return er_type == ER_JSON;}
82 /**
83 * Check whether http error or not.
84 *
85 * @return true Success.
86 * @return false Failure.
87 */
88 bool is_httperror(){return er_type == ER_HTTP;}
89 /**
90 * Check whether unknown error or not.
91 *
92 * @return true Success.
93 * @return false Failure.
94 */
95 bool is_unknownerror(){return er_type == ER_UNKNOWN;}
96 /**
97 * Get an error number.
98 *
99 * @return a error number.
100 */
101 drizzled::error_t get_error_no() const { return error_no;}
102 /**
103 * Get an error message.
104 *
105 * @return a const error message string.
106 */
107 const char* get_error_msg() const { return error_msg;}
108 /**
109 * Get sql state.
110 *
111 * @return a const sql state string.
112 */
113 const char* get_sql_state() const { return sql_state;}
114 /**
115 * Get error type.
116 *
117 * @return a error type.
118 */
119 enum_error_type get_error_type() const { return er_type;}
120 /**
121 * Get error type string.
122 *
123 * @return a error type string.
124 */
125 std::string get_error_type_string() const {
126 std::string error_str;
127 switch(er_type)
128 {
129 case ER_EMPTY: {error_str="NO ERROR"; break;}
130 case ER_SQL: {error_str="SQL ERROR"; break;}
131 case ER_JSON: {error_str="JSON ERROR"; break;}
132 case ER_HTTP: {error_str="HTTP ERROR"; break;}
133 case ER_UNKNOWN: {error_str="UNKNOWN ERROR"; break;}
134 }
135 return error_str;
136 }
137
138 private:
139 /**
140 * Stores error type.
141 */
142 enum_error_type er_type;
143 /**
144 * Stores error number.
145 */
146 drizzled::error_t error_no;
147 /**
148 * Stores error message.
149 */
150 const char *error_msg;
151 /**
152 * Stores sql state.
153 */
154 const char *sql_state;
155
156 };
157}
158}
0159
=== added file 'plugin/json_server/json_handler.cc'
--- plugin/json_server/json_handler.cc 1970-01-01 00:00:00 +0000
+++ plugin/json_server/json_handler.cc 2013-08-13 04:20:40 +0000
@@ -0,0 +1,101 @@
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.getFormatedErrorMessages().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}
0102
=== added file 'plugin/json_server/json_handler.h'
--- plugin/json_server/json_handler.h 1970-01-01 00:00:00 +0000
+++ plugin/json_server/json_handler.h 2013-08-13 04:20:40 +0000
@@ -0,0 +1,125 @@
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 Declare a class JsonHandler which handles the operations related to Json.
22 **/
23
24#include <config.h>
25
26#include <plugin/json_server/json/json.h>
27#include <evhttp.h>
28#include <event.h>
29#include <drizzled/session.h>
30#include <plugin/json_server/error.h>
31
32using namespace drizzled;
33namespace drizzle_plugin
34{
35namespace json_server
36{
37 /*
38 * a class.
39 * used to handle operations related to Json.
40 */
41 class JsonHandler
42 {
43 public:
44 /*
45 * Generate an input query from http request object.
46 *
47 * @param req_arg the http request object.
48 */
49 void generate_input_query(struct evhttp_request *req_arg);
50 /*
51 * Generate an input json from http request object.
52 *
53 * @param req_arg the http request object.
54 * @param _json_error_area the JsonErrorArea object to handle error.
55 */
56 void generate_input_json(struct evhttp_request *req_arg,JsonErrorArea &_json_error_area);
57 /*
58 * Generate an output query string.
59 *
60 * @param _json_error_area the JsonErrorArea object to handle error.
61 */
62 void generate_output_query(JsonErrorArea& _json_error_area);
63 /*
64 * Generate an output Json.
65 *
66 * @param _json_error_area the JsonErrorArea object to handle error.
67 */
68 void generate_output_json(JsonErrorArea& _json_error_area);
69 /*
70 * Get an output query string.
71 *
72 * @return a const output query string.
73 */
74 const std::string& get_output_query() const
75 {
76 return _output_query;
77 }
78 /*
79 * Get an input query string.
80 *
81 * @return a const input query string.
82 */
83 const std::string& get_input_query() const
84 {
85 return _input_query;
86 }
87 /*
88 * Get an output json object.
89 *
90 * @return a const json object.
91 */
92 const Json::Value get_output_json() const
93 {
94 return _json_out;
95 }
96 /*
97 * Get an input json object.
98 *
99 * @return a const json object.
100 */
101 const Json::Value get_input_json() const
102 {
103 return _json_in;
104 }
105
106 private:
107 /*
108 * Stores input json object.
109 */
110 Json::Value _json_in;
111 /*
112 * Stores output json object.
113 */
114 Json::Value _json_out;
115 /*
116 * Stores input string.
117 */
118 std::string _input_query;
119 /*
120 * Stores output string.
121 */
122 std::string _output_query;
123 };
124}
125}
0126
=== modified file 'plugin/json_server/json_server.cc'
--- plugin/json_server/json_server.cc 2012-07-17 09:30:57 +0000
+++ plugin/json_server/json_server.cc 2013-08-13 04:20:40 +0000
@@ -1,7 +1,7 @@
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *3 *
4 * Copyright (C) 2011 Stewart Smith, Henrik Ingo, Mohit Srivastava4 * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava
5 *5 *
6 * This program is free software; you can redistribute it and/or modify6 * 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 by7 * it under the terms of the GNU General Public License as published by
@@ -46,10 +46,11 @@
46#include <event.h>46#include <event.h>
47#include <drizzled/execute.h>47#include <drizzled/execute.h>
48#include <drizzled/sql/result_set.h>48#include <drizzled/sql/result_set.h>
4949#include <drizzled/diagnostics_area.h>
50#include <drizzled/plugin/listen.h>50#include <drizzled/plugin/listen.h>
51#include <drizzled/plugin/client.h>51#include <drizzled/plugin/client.h>
52#include <drizzled/catalog/local.h>52#include <drizzled/catalog/local.h>
53#include <drizzled/current_session.h>
5354
54#include <drizzled/pthread_globals.h>55#include <drizzled/pthread_globals.h>
55#include <boost/bind.hpp>56#include <boost/bind.hpp>
@@ -60,6 +61,8 @@
60#include <plugin/json_server/db_access.h>61#include <plugin/json_server/db_access.h>
61#include <plugin/json_server/http_handler.h>62#include <plugin/json_server/http_handler.h>
62#include <plugin/json_server/http_server.h>63#include <plugin/json_server/http_server.h>
64#include <plugin/json_server/ddl/schema.h>
65#include <plugin/json_server/json_handler.h>
6366
64namespace po= boost::program_options;67namespace po= boost::program_options;
65using namespace drizzled;68using namespace drizzled;
@@ -95,6 +98,8 @@
95extern "C" void process_version_req(struct evhttp_request *req, void* );98extern "C" void process_version_req(struct evhttp_request *req, void* );
96extern "C" void process_sql_req(struct evhttp_request *req, void* );99extern "C" void process_sql_req(struct evhttp_request *req, void* );
97extern "C" void process_json_req(struct evhttp_request *req, void* );100extern "C" void process_json_req(struct evhttp_request *req, void* );
101extern "C" void process_json_ddl_schema_create_req(struct evhttp_request *req, void* );
102extern "C" void process_json_ddl_schema_drop_req(struct evhttp_request *req, void* );
98extern "C" void process_request(struct evhttp_request *req, void* )103extern "C" void process_request(struct evhttp_request *req, void* )
99{104{
100 struct evbuffer *buf = evbuffer_new();105 struct evbuffer *buf = evbuffer_new();
@@ -357,6 +362,104 @@
357 delete(handler);362 delete(handler);
358}363}
359364
365/**
366 * Transform a HTTP Request for create schema and returns results based on the input json.
367 *
368 * @param req a HTTP request parameter,
369 *
370 */
371
372extern "C" void process_json_ddl_schema_create_req(struct evhttp_request *req, void* )
373{
374 drizzled::Session::shared_ptr _session= drizzled::Session::make_shared(drizzled::plugin::Listen::getNullClient(),
375 drizzled::catalog::local());
376 drizzled::identifier::user::mptr user_id= identifier::User::make_shared();
377 _session->main_da().reset_diagnostics_area();
378 setCurrentSession(_session.get());
379
380 std::string query;
381 std::string db_name;
382 std::string output;
383 Json::Value json_out;
384 Json::Value json_in;
385 const char *http_response_text="OK";
386 int http_response_code=HTTP_OK;
387
388 JsonErrorArea _json_error;
389 JsonHandler* _json_handler = new JsonHandler();
390
391 _json_handler->generate_input_json(req,_json_error);
392 if(!_json_error.is_jsonerror())
393 {
394 json_in = _json_handler->get_input_json();
395 db_name=json_in["query"]["name"].asString();
396 Schema *_schema = new Schema(_session.get(),db_name);
397 _schema->createSchema();
398 if(_session->main_da().is_error())
399 {
400 _json_error.set_error(JsonErrorArea::ER_SQL,_session->main_da().sql_errno(),_session->main_da().message());
401 }
402 }
403 _json_handler->generate_output_query(_json_error);
404 output = _json_handler->get_output_query();
405 struct evbuffer *buf = evbuffer_new();
406 if(buf == NULL)
407 {
408 return;
409 }
410 evbuffer_add(buf, output.c_str(), output.length());
411 evhttp_send_reply( req, http_response_code, http_response_text, buf);
412}
413
414/**
415* Transform a HTTP Request for create schema and returns results based on the input json.
416*
417* @param req a HTTP request parameter.
418*/
419extern "C" void process_json_ddl_schema_drop_req(struct evhttp_request *req, void* )
420{
421 drizzled::Session::shared_ptr _session= drizzled::Session::make_shared(drizzled::plugin::Listen::getNullClient(),
422 drizzled::catalog::local());
423 drizzled::identifier::user::mptr user_id= identifier::User::make_shared();
424 _session->main_da().reset_diagnostics_area();
425 setCurrentSession(_session.get());
426
427 std::string query;
428 std::string db_name;
429 std::string output;
430 Json::Value json_out;
431 Json::Value json_in;
432 const char *http_response_text="OK";
433 int http_response_code=HTTP_OK;
434
435 JsonErrorArea _json_error;
436 JsonHandler* _json_handler = new JsonHandler();
437
438 _json_handler->generate_input_json(req,_json_error);
439 if(!_json_error.is_jsonerror())
440 {
441 json_in = _json_handler->get_input_json();
442
443 db_name=json_in["query"]["name"].asString();
444 Schema *_schema = new Schema(_session.get(),db_name);
445 _schema->dropSchema();
446 if(_session->main_da().is_error())
447 {
448 _json_error.set_error(JsonErrorArea::ER_SQL,_session->main_da().sql_errno(),_session->main_da().message());
449 }
450 }
451 _json_handler->generate_output_query(_json_error);
452 output = _json_handler->get_output_query();
453 struct evbuffer *buf = evbuffer_new();
454 if(buf == NULL)
455 {
456 return;
457 }
458 evbuffer_add(buf, output.c_str(), output.length());
459 evhttp_send_reply( req, http_response_code, http_response_text, buf);
460}
461
462
360static void shutdown_event(int fd, short, void *arg)463static void shutdown_event(int fd, short, void *arg)
361{464{
362 struct event_base *base= (struct event_base *)arg;465 struct event_base *base= (struct event_base *)arg;
@@ -466,6 +569,8 @@
466 evhttp_set_cb(httpd, "/version", process_version_req, NULL);569 evhttp_set_cb(httpd, "/version", process_version_req, NULL);
467 evhttp_set_cb(httpd, "/sql", process_sql_req, NULL);570 evhttp_set_cb(httpd, "/sql", process_sql_req, NULL);
468 evhttp_set_cb(httpd, "/json", process_json_req, NULL);571 evhttp_set_cb(httpd, "/json", process_json_req, NULL);
572 evhttp_set_cb(httpd,"/json/ddl/schema/create", process_json_ddl_schema_create_req, NULL);
573 evhttp_set_cb(httpd,"/json/ddl/schema/drop", process_json_ddl_schema_drop_req, NULL);
469 574
470575
471 event_set(&wakeup_event, wakeup_fd[0], EV_READ | EV_PERSIST, shutdown_event, base);576 event_set(&wakeup_event, wakeup_fd[0], EV_READ | EV_PERSIST, shutdown_event, base);
472577
=== modified file 'plugin/json_server/plugin.ini'
--- plugin/json_server/plugin.ini 2013-02-06 08:10:34 +0000
+++ plugin/json_server/plugin.ini 2013-08-13 04:20:40 +0000
@@ -5,7 +5,10 @@
5 sql_to_json_generator.h5 sql_to_json_generator.h
6 http_handler.h6 http_handler.h
7 http_server.h7 http_server.h
8 json_handler.h
9 error.h
8 db_access.h10 db_access.h
11 ddl/schema.h
9 json/autolink.h12 json/autolink.h
10 json/config.h13 json/config.h
11 json/features.h14 json/features.h
@@ -25,7 +28,10 @@
25 sql_to_json_generator.cc28 sql_to_json_generator.cc
26 http_handler.cc29 http_handler.cc
27 http_server.cc30 http_server.cc
31 json_handler.cc
32 error.cc
28 db_access.cc33 db_access.cc
34 ddl/schema.cc
29 json/json_reader.cpp35 json/json_reader.cpp
30 json/json_value.cpp36 json/json_value.cpp
31 json/json_writer.cpp37 json/json_writer.cpp
3238
=== modified file 'plugin/json_server/tests/r/basic.result'
--- plugin/json_server/tests/r/basic.result 2012-07-14 14:10:13 +0000
+++ plugin/json_server/tests/r/basic.result 2013-08-13 04:20:40 +0000
@@ -287,3 +287,21 @@
287SET GLOBAL json_server_table="";287SET GLOBAL json_server_table="";
288SET GLOBAL json_server_schema="test";288SET GLOBAL json_server_schema="test";
289drop schema json;289drop schema json;
290{
291 "sql_state" : "00000"
292}
293{
294 "error_message" : "Can't create schema 'json'; schema exists",
295 "error_no" : 1007,
296 "error_type" : "SQL ERROR",
297 "sql_state" : "HY000"
298}
299{
300 "sql_state" : "00000"
301}
302{
303 "error_message" : "Can't drop schema 'json'; schema doesn't exist",
304 "error_no" : 1008,
305 "error_type" : "SQL ERROR",
306 "sql_state" : "HY000"
307}
290308
=== added file 'plugin/json_server/tests/t/basic.test'
--- plugin/json_server/tests/t/basic.test 1970-01-01 00:00:00 +0000
+++ plugin/json_server/tests/t/basic.test 2013-08-13 04:20:40 +0000
@@ -0,0 +1,94 @@
1create table t1 (a int primary key auto_increment, b varchar(100));
2--replace_result $JSON_SERVER_PORT PORT
3--eval select http_post("http://localhost:$JSON_SERVER_PORT/sql", 'select * from t1;');
4insert into t1 (b) values ("from MySQL protocol");
5--replace_result $JSON_SERVER_PORT PORT
6--eval select http_post('http://localhost:$JSON_SERVER_PORT/sql', 'select * from t1;');
7--replace_result $JSON_SERVER_PORT PORT
8--eval select http_post('http://localhost:$JSON_SERVER_PORT/sql', 'insert into t1 (b) values (\'from http\');');
9SELECT * from t1;
10drop table t1;
11
12create schema json;
13
14use json;
15
16--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1,"document":{"firstname":"Henrik","lastname":"Ingo","age": 35}}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
17
18--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=1'
19
20--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%22_id%22%3A1%7D%7D'
21
22--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1,"document":{"firstname":"Henrik","lastname":"Ingo","age": 36}}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
23
24--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=1'
25
26--exec curl -X POST 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=2'
27
28--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1,"document":{"firstname":"Henrik","lastname":"Ingo","age": 37}}}' 'http://localhost:$JSON_SERVER_PORT/json'
29
30--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json'
31
32--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"document":{"firstname":"Mohit","lastname":"Srivastava","age": 21}}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
33
34--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
35
36--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%7D%7D'
37
38--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%22_id%22%3A%22%22%7D%7D'
39
40SET GLOBAL json_server_schema="json";
41
42--exec curl 'http://localhost:$JSON_SERVER_PORT/json?table=people'
43
44SET GLOBAL json_server_table="people";
45
46--exec curl 'http://localhost:$JSON_SERVER_PORT/json'
47
48--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1 "document":{"firstname":"Henrik","lastname":"Ingo","age": 37}}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
49
50--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1,"document":"It is for testing"}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
51
52--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1,"document":98765}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
53
54--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"_id":1,"document":{"firstname":"Henrik","lastname":"Ingo","age": 35},"metadata":{"interest":"opensource","nick":"hingo","dob":"16-feb-1977"}}}' 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=aboutpeople'
55
56--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=aboutpeople'
57
58--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=1'
59
60--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
61
62--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%22_id%22%3A2%7D%7D'
63
64--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
65
66--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json'
67
68--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
69
70--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%7D%7D'
71
72--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%22_id%22%3A%22%22%7D%7D'
73
74SET GLOBAL json_server_allow_drop_table="ON";
75
76--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%7D%7D'
77
78--exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people'
79
80--exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=aboutpeople&query=%7B%22query%22%3A%7B%22_id%22%3A%22%22%7D%7D'
81
82SET GLOBAL json_server_allow_drop_table="OFF";
83SET GLOBAL json_server_table="";
84SET GLOBAL json_server_schema="test";
85
86drop schema json;
87
88--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"name":"json"}}' 'http://localhost:$JSON_SERVER_PORT/json/ddl/schema/create'
89
90--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"name":"json"}}' 'http://localhost:$JSON_SERVER_PORT/json/ddl/schema/create'
91
92--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"name":"json"}}' 'http://localhost:$JSON_SERVER_PORT/json/ddl/schema/drop'
93
94--exec curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"query":{"name":"json"}}' 'http://localhost:$JSON_SERVER_PORT/json/ddl/schema/drop'

Subscribers

People subscribed via source and target branches