Merge lp:~mohyt/drizzle/json_server_schema into lp:drizzle
- json_server_schema
- Merge into 7.2
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 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Stewart Smith (community) | Approve | ||
Review via email: mp+177285@code.launchpad.net |
Commit message
Description of the change
Added create & drop Schema functionality on json server
To post a comment you must log in.
- 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
1 | === added directory 'plugin/json_server/ddl' | |||
2 | === added file 'plugin/json_server/ddl/schema.cc' | |||
3 | --- plugin/json_server/ddl/schema.cc 1970-01-01 00:00:00 +0000 | |||
4 | +++ plugin/json_server/ddl/schema.cc 2013-08-13 04:20:40 +0000 | |||
5 | @@ -0,0 +1,158 @@ | |||
6 | 1 | /* mode: c; c-basic-offset: 2; indent-tabs-mode: nil; | ||
7 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | ||
8 | 3 | * | ||
9 | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava | ||
10 | 5 | * | ||
11 | 6 | * This program is free software; you can redistribute it and/or modify | ||
12 | 7 | * it under the terms of the GNU General Public License as published by | ||
13 | 8 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | 9 | * (at your option) any later version. | ||
15 | 10 | * | ||
16 | 11 | * This program is distributed in the hope that it will be useful, | ||
17 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | 14 | * GNU General Public License for more details. | ||
20 | 15 | * | ||
21 | 16 | * You should have received a copy of the GNU General Public License | ||
22 | 17 | * along with this program; if not, write to the Free Software | ||
23 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
24 | 19 | */ | ||
25 | 20 | /** | ||
26 | 21 | * @file Implements a class Schema to handle various operations related to schema.Its just a copy of drizzled/schema.cc and its friends. | ||
27 | 22 | */ | ||
28 | 23 | |||
29 | 24 | #include <config.h> | ||
30 | 25 | |||
31 | 26 | #include <drizzled/show.h> | ||
32 | 27 | #include <drizzled/session.h> | ||
33 | 28 | #include <drizzled/schema.h> | ||
34 | 29 | #include <drizzled/message.h> | ||
35 | 30 | #include <drizzled/sql_lex.h> | ||
36 | 31 | #include <drizzled/plugin/event_observer.h> | ||
37 | 32 | #include <drizzled/catalog/instance.h> | ||
38 | 33 | #include <plugin/json_server/ddl/schema.h> | ||
39 | 34 | #include <drizzled/plugin/authorization.h> | ||
40 | 35 | #include <drizzled/plugin/storage_engine.h> | ||
41 | 36 | |||
42 | 37 | #include <string> | ||
43 | 38 | |||
44 | 39 | |||
45 | 40 | using namespace std; | ||
46 | 41 | using namespace drizzled; | ||
47 | 42 | |||
48 | 43 | namespace drizzle_plugin { | ||
49 | 44 | namespace json_server { | ||
50 | 45 | |||
51 | 46 | bool Schema::createSchema() | ||
52 | 47 | { | ||
53 | 48 | if (not validateSchemaOptions()) | ||
54 | 49 | return true; | ||
55 | 50 | |||
56 | 51 | if (session().inTransaction()) | ||
57 | 52 | { | ||
58 | 53 | my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0)); | ||
59 | 54 | return true; | ||
60 | 55 | } | ||
61 | 56 | |||
62 | 57 | drizzled::identifier::Schema schema_identifier(session().catalog().identifier(),_db_name); | ||
63 | 58 | |||
64 | 59 | if (not check(schema_identifier)) | ||
65 | 60 | return false; | ||
66 | 61 | |||
67 | 62 | drizzled::message::schema::init(schema_message, schema_identifier); | ||
68 | 63 | message::set_definer(schema_message, *session().user()); | ||
69 | 64 | |||
70 | 65 | bool res =false; | ||
71 | 66 | std::string path = schema_identifier.getSQLPath(); | ||
72 | 67 | |||
73 | 68 | if (unlikely(plugin::EventObserver::beforeCreateDatabase(session(), path))) | ||
74 | 69 | { | ||
75 | 70 | my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str()); | ||
76 | 71 | } | ||
77 | 72 | else | ||
78 | 73 | { | ||
79 | 74 | res= schema::create(session(), schema_message, false); | ||
80 | 75 | if (unlikely(plugin::EventObserver::afterCreateDatabase(session(), path, res))) | ||
81 | 76 | { | ||
82 | 77 | my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier); | ||
83 | 78 | res = false; | ||
84 | 79 | } | ||
85 | 80 | } | ||
86 | 81 | return not res; | ||
87 | 82 | } | ||
88 | 83 | |||
89 | 84 | bool Schema::dropSchema() | ||
90 | 85 | { | ||
91 | 86 | if (session().inTransaction()) | ||
92 | 87 | { | ||
93 | 88 | my_error(ER_TRANSACTIONAL_DDL_NOT_SUPPORTED, MYF(0)); | ||
94 | 89 | return true; | ||
95 | 90 | } | ||
96 | 91 | |||
97 | 92 | drizzled::identifier::Schema schema_identifier(session().catalog().identifier(),_db_name); | ||
98 | 93 | |||
99 | 94 | if (not schema::check(session(),schema_identifier)) | ||
100 | 95 | { | ||
101 | 96 | my_error(ER_WRONG_DB_NAME, schema_identifier); | ||
102 | 97 | return false; | ||
103 | 98 | } | ||
104 | 99 | |||
105 | 100 | if (session().inTransaction()) | ||
106 | 101 | { | ||
107 | 102 | my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0)); | ||
108 | 103 | return true; | ||
109 | 104 | } | ||
110 | 105 | |||
111 | 106 | bool res = true; | ||
112 | 107 | std::string path = schema_identifier.getSQLPath(); | ||
113 | 108 | if (unlikely(plugin::EventObserver::beforeDropDatabase(session(), path))) | ||
114 | 109 | { | ||
115 | 110 | my_error(ER_EVENT_OBSERVER_PLUGIN, schema_identifier); | ||
116 | 111 | } | ||
117 | 112 | else | ||
118 | 113 | { | ||
119 | 114 | res= schema::drop(session(), schema_identifier, false); | ||
120 | 115 | if (unlikely(plugin::EventObserver::afterDropDatabase(session(), path, res))) | ||
121 | 116 | { | ||
122 | 117 | my_error(ER_EVENT_OBSERVER_PLUGIN, MYF(0), path.c_str()); | ||
123 | 118 | res = false; | ||
124 | 119 | } | ||
125 | 120 | } | ||
126 | 121 | |||
127 | 122 | return res; | ||
128 | 123 | |||
129 | 124 | } | ||
130 | 125 | |||
131 | 126 | bool Schema::validateSchemaOptions() | ||
132 | 127 | { | ||
133 | 128 | size_t num_engine_options= schema_message.engine().options_size(); | ||
134 | 129 | bool rc= num_engine_options ? false : true; | ||
135 | 130 | |||
136 | 131 | for (size_t y= 0; y < num_engine_options; ++y) | ||
137 | 132 | { | ||
138 | 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()); | ||
139 | 134 | rc= false; | ||
140 | 135 | } | ||
141 | 136 | return rc; | ||
142 | 137 | |||
143 | 138 | } | ||
144 | 139 | |||
145 | 140 | bool Schema::check(const identifier::Schema &identifier) | ||
146 | 141 | { | ||
147 | 142 | if (not identifier.isValid()) | ||
148 | 143 | return false; | ||
149 | 144 | |||
150 | 145 | if (not plugin::Authorization::isAuthorized(*session().user(), identifier)) | ||
151 | 146 | return false; | ||
152 | 147 | |||
153 | 148 | if (plugin::StorageEngine::doesSchemaExist(identifier)) | ||
154 | 149 | { | ||
155 | 150 | my_error(ER_DB_CREATE_EXISTS, identifier); | ||
156 | 151 | return false; | ||
157 | 152 | } | ||
158 | 153 | |||
159 | 154 | return true; | ||
160 | 155 | } | ||
161 | 156 | } | ||
162 | 157 | } | ||
163 | 158 | |||
164 | 0 | 159 | ||
165 | === added file 'plugin/json_server/ddl/schema.h' | |||
166 | --- plugin/json_server/ddl/schema.h 1970-01-01 00:00:00 +0000 | |||
167 | +++ plugin/json_server/ddl/schema.h 2013-08-13 04:20:40 +0000 | |||
168 | @@ -0,0 +1,110 @@ | |||
169 | 1 | /** - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- | ||
170 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | ||
171 | 3 | * | ||
172 | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava | ||
173 | 5 | * | ||
174 | 6 | * This program is free software; you can redistribute it and/or modify | ||
175 | 7 | * it under the terms of the GNU General Public License as published by | ||
176 | 8 | * the Free Software Foundation; either version 2 of the License, or | ||
177 | 9 | * (at your option) any later version. | ||
178 | 10 | * | ||
179 | 11 | * This program is distributed in the hope that it will be useful, | ||
180 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
181 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
182 | 14 | * GNU General Public License for more details. | ||
183 | 15 | * | ||
184 | 16 | * You should have received a copy of the GNU General Public License | ||
185 | 17 | * along with this program; if not, write to the Free Software | ||
186 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
187 | 19 | */ | ||
188 | 20 | /** | ||
189 | 21 | * @file Declare a class Schema to perform various operations related to schema. | ||
190 | 22 | */ | ||
191 | 23 | #include <drizzled/session.h> | ||
192 | 24 | #include <drizzled/statement.h> | ||
193 | 25 | #include <drizzled/message/schema.pb.h> | ||
194 | 26 | #include <uuid/uuid.h> | ||
195 | 27 | #include <drizzled/definitions.h> | ||
196 | 28 | #include <drizzled/error.h> | ||
197 | 29 | #include <drizzled/sql_parse.h> | ||
198 | 30 | #include <drizzled/sql_base.h> | ||
199 | 31 | #include <string> | ||
200 | 32 | using namespace std; | ||
201 | 33 | using namespace drizzled; | ||
202 | 34 | namespace drizzle_plugin { | ||
203 | 35 | namespace json_server { | ||
204 | 36 | /** | ||
205 | 37 | * a class. | ||
206 | 38 | * | ||
207 | 39 | * To perform various operations related to schema. | ||
208 | 40 | */ | ||
209 | 41 | class Schema | ||
210 | 42 | { | ||
211 | 43 | public: | ||
212 | 44 | /** | ||
213 | 45 | * Constructor. | ||
214 | 46 | * | ||
215 | 47 | * @param in_session a session object. | ||
216 | 48 | * @param db_name a schema name string. | ||
217 | 49 | */ | ||
218 | 50 | Schema(Session *in_session,string db_name) : | ||
219 | 51 | _session(*in_session),_db_name(db_name) | ||
220 | 52 | {} | ||
221 | 53 | /** | ||
222 | 54 | * Stores whether schema exist or not. | ||
223 | 55 | */ | ||
224 | 56 | bool is_if_not_exists; | ||
225 | 57 | /** | ||
226 | 58 | * Stores schema message. | ||
227 | 59 | */ | ||
228 | 60 | message::Schema schema_message; | ||
229 | 61 | /** | ||
230 | 62 | * create a new schema if it not exists. | ||
231 | 63 | * | ||
232 | 64 | * @return false Success. | ||
233 | 65 | * @return true Failure. | ||
234 | 66 | */ | ||
235 | 67 | bool createSchema(); | ||
236 | 68 | /** | ||
237 | 69 | * drop a schema if it exists. | ||
238 | 70 | * | ||
239 | 71 | * @return false Success. | ||
240 | 72 | * @reutrn true Failure. | ||
241 | 73 | */ | ||
242 | 74 | bool dropSchema(); | ||
243 | 75 | /** | ||
244 | 76 | * Get a session object. | ||
245 | 77 | * | ||
246 | 78 | * @return a session object. | ||
247 | 79 | */ | ||
248 | 80 | Session& session() const{ | ||
249 | 81 | return _session; | ||
250 | 82 | } | ||
251 | 83 | |||
252 | 84 | private: | ||
253 | 85 | /** | ||
254 | 86 | * Validates various schema options. | ||
255 | 87 | * | ||
256 | 88 | * @return false Success. | ||
257 | 89 | * @return true Failure. | ||
258 | 90 | */ | ||
259 | 91 | bool validateSchemaOptions(); | ||
260 | 92 | /** | ||
261 | 93 | * Checks whether schema exists or not already. | ||
262 | 94 | * | ||
263 | 95 | * @return false Success. | ||
264 | 96 | * @return true Failure. | ||
265 | 97 | */ | ||
266 | 98 | bool check(const identifier::Schema &identifier); | ||
267 | 99 | /** | ||
268 | 100 | * Stores a session object. | ||
269 | 101 | */ | ||
270 | 102 | Session& _session; | ||
271 | 103 | /** | ||
272 | 104 | * Stores a schema name. | ||
273 | 105 | */ | ||
274 | 106 | string _db_name; | ||
275 | 107 | }; | ||
276 | 108 | |||
277 | 109 | } | ||
278 | 110 | } | ||
279 | 0 | 111 | ||
280 | === added file 'plugin/json_server/error.cc' | |||
281 | --- plugin/json_server/error.cc 1970-01-01 00:00:00 +0000 | |||
282 | +++ plugin/json_server/error.cc 2013-08-13 04:20:40 +0000 | |||
283 | @@ -0,0 +1,58 @@ | |||
284 | 1 | /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- | ||
285 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | ||
286 | 3 | * | ||
287 | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava | ||
288 | 5 | * | ||
289 | 6 | * This program is free software; you can redistribute it and/or modify | ||
290 | 7 | * it under the terms of the GNU General Public License as published by | ||
291 | 8 | * the Free Software Foundation; either version 2 of the License, or | ||
292 | 9 | * (at your option) any later version. | ||
293 | 10 | * | ||
294 | 11 | * This program is distributed in the hope that it will be useful, | ||
295 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
296 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
297 | 14 | * GNU General Public License for more details. | ||
298 | 15 | * | ||
299 | 16 | * You should have received a copy of the GNU General Public License | ||
300 | 17 | * along with this program; if not, write to the Free Software | ||
301 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
302 | 19 | */ | ||
303 | 20 | /** | ||
304 | 21 | * @file Implements JsonErrorArea class which handles errors of json server. | ||
305 | 22 | */ | ||
306 | 23 | #include <plugin/json_server/error.h> | ||
307 | 24 | #include <drizzled/error/sql_state.h> | ||
308 | 25 | |||
309 | 26 | namespace drizzle_plugin | ||
310 | 27 | { | ||
311 | 28 | namespace json_server | ||
312 | 29 | { | ||
313 | 30 | JsonErrorArea::JsonErrorArea() | ||
314 | 31 | { | ||
315 | 32 | reset_jsonerror_area(); | ||
316 | 33 | } | ||
317 | 34 | |||
318 | 35 | void JsonErrorArea::reset_jsonerror_area() | ||
319 | 36 | { | ||
320 | 37 | er_type = ER_EMPTY; | ||
321 | 38 | error_no = drizzled::EE_OK; | ||
322 | 39 | error_msg= ""; | ||
323 | 40 | sql_state="00000"; | ||
324 | 41 | } | ||
325 | 42 | |||
326 | 43 | void JsonErrorArea::set_error(enum_error_type error_type_arg,drizzled::error_t error_no_arg,const char * error_msg_arg) | ||
327 | 44 | { | ||
328 | 45 | if(error_type_arg != ER_EMPTY) | ||
329 | 46 | { | ||
330 | 47 | er_type = error_type_arg; | ||
331 | 48 | error_msg = error_msg_arg; | ||
332 | 49 | |||
333 | 50 | if(error_type_arg == ER_SQL) | ||
334 | 51 | { | ||
335 | 52 | error_no = error_no_arg; | ||
336 | 53 | sql_state = drizzled::error::convert_to_sqlstate(error_no_arg); | ||
337 | 54 | } | ||
338 | 55 | } | ||
339 | 56 | } | ||
340 | 57 | } | ||
341 | 58 | } | ||
342 | 0 | 59 | ||
343 | === added file 'plugin/json_server/error.h' | |||
344 | --- plugin/json_server/error.h 1970-01-01 00:00:00 +0000 | |||
345 | +++ plugin/json_server/error.h 2013-08-13 04:20:40 +0000 | |||
346 | @@ -0,0 +1,158 @@ | |||
347 | 1 | /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- | ||
348 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | ||
349 | 3 | * | ||
350 | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava | ||
351 | 5 | * | ||
352 | 6 | * This program is free software; you can redistribute it and/or modify | ||
353 | 7 | * it under the terms of the GNU General Public License as published by | ||
354 | 8 | * the Free Software Foundation; either version 2 of the License, or | ||
355 | 9 | * (at your option) any later version. | ||
356 | 10 | * | ||
357 | 11 | * This program is distributed in the hope that it will be useful, | ||
358 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
359 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
360 | 14 | * GNU General Public License for more details. | ||
361 | 15 | * | ||
362 | 16 | * You should have received a copy of the GNU General Public License | ||
363 | 17 | * along with this program; if not, write to the Free Software | ||
364 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
365 | 19 | */ | ||
366 | 20 | /** | ||
367 | 21 | * @file Declare a class to handle errors in json server. | ||
368 | 22 | */ | ||
369 | 23 | #include <config.h> | ||
370 | 24 | |||
371 | 25 | #include <drizzled/error_t.h> | ||
372 | 26 | #include <string> | ||
373 | 27 | namespace drizzle_plugin | ||
374 | 28 | { | ||
375 | 29 | namespace json_server | ||
376 | 30 | { | ||
377 | 31 | /** | ||
378 | 32 | * a class | ||
379 | 33 | * used to handle various errors of json server. | ||
380 | 34 | */ | ||
381 | 35 | class JsonErrorArea | ||
382 | 36 | { | ||
383 | 37 | public: | ||
384 | 38 | /** | ||
385 | 39 | * an enumerated data type for different type of error in json server. | ||
386 | 40 | */ | ||
387 | 41 | enum enum_error_type | ||
388 | 42 | { | ||
389 | 43 | ER_EMPTY=0, | ||
390 | 44 | ER_SQL, | ||
391 | 45 | ER_JSON, | ||
392 | 46 | ER_HTTP, | ||
393 | 47 | ER_UNKNOWN | ||
394 | 48 | }; | ||
395 | 49 | /** | ||
396 | 50 | * Constructor. | ||
397 | 51 | */ | ||
398 | 52 | JsonErrorArea(); | ||
399 | 53 | /** | ||
400 | 54 | * Reset the memeber values. | ||
401 | 55 | */ | ||
402 | 56 | void reset_jsonerror_area(); | ||
403 | 57 | /** | ||
404 | 58 | * Set an error with details. | ||
405 | 59 | */ | ||
406 | 60 | void set_error(enum_error_type type,drizzled::error_t sql_errno_arg,const char *message_arg); | ||
407 | 61 | /** | ||
408 | 62 | * Check whether error or not. | ||
409 | 63 | * | ||
410 | 64 | * @return true Success. | ||
411 | 65 | * @return false Failure. | ||
412 | 66 | */ | ||
413 | 67 | bool is_error(){return er_type!= ER_EMPTY;} | ||
414 | 68 | /** | ||
415 | 69 | * Check whether sql error or not. | ||
416 | 70 | * | ||
417 | 71 | * @return true Success. | ||
418 | 72 | * @return false Failure. | ||
419 | 73 | */ | ||
420 | 74 | bool is_sqlerror(){return er_type == ER_SQL;} | ||
421 | 75 | /** | ||
422 | 76 | * Check whether json error or not. | ||
423 | 77 | * | ||
424 | 78 | * @return true Success. | ||
425 | 79 | * @return false Failure. | ||
426 | 80 | */ | ||
427 | 81 | bool is_jsonerror(){return er_type == ER_JSON;} | ||
428 | 82 | /** | ||
429 | 83 | * Check whether http error or not. | ||
430 | 84 | * | ||
431 | 85 | * @return true Success. | ||
432 | 86 | * @return false Failure. | ||
433 | 87 | */ | ||
434 | 88 | bool is_httperror(){return er_type == ER_HTTP;} | ||
435 | 89 | /** | ||
436 | 90 | * Check whether unknown error or not. | ||
437 | 91 | * | ||
438 | 92 | * @return true Success. | ||
439 | 93 | * @return false Failure. | ||
440 | 94 | */ | ||
441 | 95 | bool is_unknownerror(){return er_type == ER_UNKNOWN;} | ||
442 | 96 | /** | ||
443 | 97 | * Get an error number. | ||
444 | 98 | * | ||
445 | 99 | * @return a error number. | ||
446 | 100 | */ | ||
447 | 101 | drizzled::error_t get_error_no() const { return error_no;} | ||
448 | 102 | /** | ||
449 | 103 | * Get an error message. | ||
450 | 104 | * | ||
451 | 105 | * @return a const error message string. | ||
452 | 106 | */ | ||
453 | 107 | const char* get_error_msg() const { return error_msg;} | ||
454 | 108 | /** | ||
455 | 109 | * Get sql state. | ||
456 | 110 | * | ||
457 | 111 | * @return a const sql state string. | ||
458 | 112 | */ | ||
459 | 113 | const char* get_sql_state() const { return sql_state;} | ||
460 | 114 | /** | ||
461 | 115 | * Get error type. | ||
462 | 116 | * | ||
463 | 117 | * @return a error type. | ||
464 | 118 | */ | ||
465 | 119 | enum_error_type get_error_type() const { return er_type;} | ||
466 | 120 | /** | ||
467 | 121 | * Get error type string. | ||
468 | 122 | * | ||
469 | 123 | * @return a error type string. | ||
470 | 124 | */ | ||
471 | 125 | std::string get_error_type_string() const { | ||
472 | 126 | std::string error_str; | ||
473 | 127 | switch(er_type) | ||
474 | 128 | { | ||
475 | 129 | case ER_EMPTY: {error_str="NO ERROR"; break;} | ||
476 | 130 | case ER_SQL: {error_str="SQL ERROR"; break;} | ||
477 | 131 | case ER_JSON: {error_str="JSON ERROR"; break;} | ||
478 | 132 | case ER_HTTP: {error_str="HTTP ERROR"; break;} | ||
479 | 133 | case ER_UNKNOWN: {error_str="UNKNOWN ERROR"; break;} | ||
480 | 134 | } | ||
481 | 135 | return error_str; | ||
482 | 136 | } | ||
483 | 137 | |||
484 | 138 | private: | ||
485 | 139 | /** | ||
486 | 140 | * Stores error type. | ||
487 | 141 | */ | ||
488 | 142 | enum_error_type er_type; | ||
489 | 143 | /** | ||
490 | 144 | * Stores error number. | ||
491 | 145 | */ | ||
492 | 146 | drizzled::error_t error_no; | ||
493 | 147 | /** | ||
494 | 148 | * Stores error message. | ||
495 | 149 | */ | ||
496 | 150 | const char *error_msg; | ||
497 | 151 | /** | ||
498 | 152 | * Stores sql state. | ||
499 | 153 | */ | ||
500 | 154 | const char *sql_state; | ||
501 | 155 | |||
502 | 156 | }; | ||
503 | 157 | } | ||
504 | 158 | } | ||
505 | 0 | 159 | ||
506 | === added file 'plugin/json_server/json_handler.cc' | |||
507 | --- plugin/json_server/json_handler.cc 1970-01-01 00:00:00 +0000 | |||
508 | +++ plugin/json_server/json_handler.cc 2013-08-13 04:20:40 +0000 | |||
509 | @@ -0,0 +1,101 @@ | |||
510 | 1 | /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- | ||
511 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | ||
512 | 3 | * | ||
513 | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava | ||
514 | 5 | * | ||
515 | 6 | * This program is free software; you can redistribute it and/or modify | ||
516 | 7 | * it under the terms of the GNU General Public License as published by | ||
517 | 8 | * the Free Software Foundation; either version 2 of the License, or | ||
518 | 9 | * (at your option) any later version. | ||
519 | 10 | * | ||
520 | 11 | * This program is distributed in the hope that it will be useful, | ||
521 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
522 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
523 | 14 | * GNU General Public License for more details. | ||
524 | 15 | * | ||
525 | 16 | * You should have received a copy of the GNU General Public License | ||
526 | 17 | * along with this program; if not, write to the Free Software | ||
527 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
528 | 19 | */ | ||
529 | 20 | /* | ||
530 | 21 | * @file Implements a class JsonHandler which handles the operations related to Json. | ||
531 | 22 | */ | ||
532 | 23 | #include <plugin/json_server/json_handler.h> | ||
533 | 24 | #include <string.h> | ||
534 | 25 | using namespace drizzled; | ||
535 | 26 | namespace drizzle_plugin | ||
536 | 27 | { | ||
537 | 28 | namespace json_server | ||
538 | 29 | { | ||
539 | 30 | void JsonHandler::generate_input_query(struct evhttp_request *req_arg) | ||
540 | 31 | { | ||
541 | 32 | evhttp_parse_query(evhttp_request_uri(req_arg), req_arg->input_headers); | ||
542 | 33 | if(req_arg->type== EVHTTP_REQ_POST ) | ||
543 | 34 | { | ||
544 | 35 | char buffer[1024]; | ||
545 | 36 | int l=0; | ||
546 | 37 | do | ||
547 | 38 | { | ||
548 | 39 | l= evbuffer_remove(req_arg->input_buffer, buffer, 1024); | ||
549 | 40 | _input_query.append(buffer, l); | ||
550 | 41 | } | ||
551 | 42 | while(l); | ||
552 | 43 | } | ||
553 | 44 | else | ||
554 | 45 | { | ||
555 | 46 | const char* _query; | ||
556 | 47 | _query= (char *)evhttp_find_header(req_arg->input_headers, "query"); | ||
557 | 48 | if(_query == NULL || strcmp(_query,"")==0) | ||
558 | 49 | { | ||
559 | 50 | _query="{}"; | ||
560 | 51 | } | ||
561 | 52 | _input_query.append(_query,strlen(_query)); | ||
562 | 53 | } | ||
563 | 54 | |||
564 | 55 | } | ||
565 | 56 | |||
566 | 57 | void JsonHandler::generate_input_json(struct evhttp_request *req_arg,JsonErrorArea &_json_error_area) | ||
567 | 58 | { | ||
568 | 59 | generate_input_query(req_arg); | ||
569 | 60 | Json::Features _json_conf; | ||
570 | 61 | Json::Reader reader(_json_conf); | ||
571 | 62 | bool retval = reader.parse(_input_query,_json_in); | ||
572 | 63 | if(retval!=true) | ||
573 | 64 | { | ||
574 | 65 | _json_error_area.set_error(JsonErrorArea::ER_JSON,drizzled::EE_OK,reader.getFormatedErrorMessages().c_str()); | ||
575 | 66 | } | ||
576 | 67 | } | ||
577 | 68 | |||
578 | 69 | void JsonHandler::generate_output_json(JsonErrorArea& _json_error_area) | ||
579 | 70 | { | ||
580 | 71 | if(_json_error_area.is_error()) | ||
581 | 72 | { | ||
582 | 73 | if(_json_error_area.is_sqlerror()) | ||
583 | 74 | { | ||
584 | 75 | _json_out["error_type"]=_json_error_area.get_error_type_string(); | ||
585 | 76 | _json_out["error_no"]=_json_error_area.get_error_no(); | ||
586 | 77 | _json_out["error_message"]=_json_error_area.get_error_msg(); | ||
587 | 78 | _json_out["sql_state"]=_json_error_area.get_sql_state(); | ||
588 | 79 | |||
589 | 80 | } | ||
590 | 81 | else | ||
591 | 82 | { | ||
592 | 83 | _json_out["error_type"]=_json_error_area.get_error_type_string(); | ||
593 | 84 | _json_out["error_message"]=_json_error_area.get_error_msg(); | ||
594 | 85 | } | ||
595 | 86 | } | ||
596 | 87 | else | ||
597 | 88 | { | ||
598 | 89 | _json_out["sql_state"]=_json_error_area.get_sql_state(); | ||
599 | 90 | } | ||
600 | 91 | |||
601 | 92 | } | ||
602 | 93 | |||
603 | 94 | void JsonHandler::generate_output_query(JsonErrorArea& _json_error_area) | ||
604 | 95 | { | ||
605 | 96 | generate_output_json(_json_error_area); | ||
606 | 97 | Json::StyledWriter writer; | ||
607 | 98 | _output_query= writer.write(_json_out); | ||
608 | 99 | } | ||
609 | 100 | } | ||
610 | 101 | } | ||
611 | 0 | 102 | ||
612 | === added file 'plugin/json_server/json_handler.h' | |||
613 | --- plugin/json_server/json_handler.h 1970-01-01 00:00:00 +0000 | |||
614 | +++ plugin/json_server/json_handler.h 2013-08-13 04:20:40 +0000 | |||
615 | @@ -0,0 +1,125 @@ | |||
616 | 1 | /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- | ||
617 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | ||
618 | 3 | * | ||
619 | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava | ||
620 | 5 | * | ||
621 | 6 | * This program is free software; you can redistribute it and/or modify | ||
622 | 7 | * it under the terms of the GNU General Public License as published by | ||
623 | 8 | * the Free Software Foundation; either version 2 of the License, or | ||
624 | 9 | * (at your option) any later version. | ||
625 | 10 | * | ||
626 | 11 | * This program is distributed in the hope that it will be useful, | ||
627 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
628 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
629 | 14 | * GNU General Public License for more details. | ||
630 | 15 | * | ||
631 | 16 | * You should have received a copy of the GNU General Public License | ||
632 | 17 | * along with this program; if not, write to the Free Software | ||
633 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
634 | 19 | */ | ||
635 | 20 | /** | ||
636 | 21 | * @file Declare a class JsonHandler which handles the operations related to Json. | ||
637 | 22 | **/ | ||
638 | 23 | |||
639 | 24 | #include <config.h> | ||
640 | 25 | |||
641 | 26 | #include <plugin/json_server/json/json.h> | ||
642 | 27 | #include <evhttp.h> | ||
643 | 28 | #include <event.h> | ||
644 | 29 | #include <drizzled/session.h> | ||
645 | 30 | #include <plugin/json_server/error.h> | ||
646 | 31 | |||
647 | 32 | using namespace drizzled; | ||
648 | 33 | namespace drizzle_plugin | ||
649 | 34 | { | ||
650 | 35 | namespace json_server | ||
651 | 36 | { | ||
652 | 37 | /* | ||
653 | 38 | * a class. | ||
654 | 39 | * used to handle operations related to Json. | ||
655 | 40 | */ | ||
656 | 41 | class JsonHandler | ||
657 | 42 | { | ||
658 | 43 | public: | ||
659 | 44 | /* | ||
660 | 45 | * Generate an input query from http request object. | ||
661 | 46 | * | ||
662 | 47 | * @param req_arg the http request object. | ||
663 | 48 | */ | ||
664 | 49 | void generate_input_query(struct evhttp_request *req_arg); | ||
665 | 50 | /* | ||
666 | 51 | * Generate an input json from http request object. | ||
667 | 52 | * | ||
668 | 53 | * @param req_arg the http request object. | ||
669 | 54 | * @param _json_error_area the JsonErrorArea object to handle error. | ||
670 | 55 | */ | ||
671 | 56 | void generate_input_json(struct evhttp_request *req_arg,JsonErrorArea &_json_error_area); | ||
672 | 57 | /* | ||
673 | 58 | * Generate an output query string. | ||
674 | 59 | * | ||
675 | 60 | * @param _json_error_area the JsonErrorArea object to handle error. | ||
676 | 61 | */ | ||
677 | 62 | void generate_output_query(JsonErrorArea& _json_error_area); | ||
678 | 63 | /* | ||
679 | 64 | * Generate an output Json. | ||
680 | 65 | * | ||
681 | 66 | * @param _json_error_area the JsonErrorArea object to handle error. | ||
682 | 67 | */ | ||
683 | 68 | void generate_output_json(JsonErrorArea& _json_error_area); | ||
684 | 69 | /* | ||
685 | 70 | * Get an output query string. | ||
686 | 71 | * | ||
687 | 72 | * @return a const output query string. | ||
688 | 73 | */ | ||
689 | 74 | const std::string& get_output_query() const | ||
690 | 75 | { | ||
691 | 76 | return _output_query; | ||
692 | 77 | } | ||
693 | 78 | /* | ||
694 | 79 | * Get an input query string. | ||
695 | 80 | * | ||
696 | 81 | * @return a const input query string. | ||
697 | 82 | */ | ||
698 | 83 | const std::string& get_input_query() const | ||
699 | 84 | { | ||
700 | 85 | return _input_query; | ||
701 | 86 | } | ||
702 | 87 | /* | ||
703 | 88 | * Get an output json object. | ||
704 | 89 | * | ||
705 | 90 | * @return a const json object. | ||
706 | 91 | */ | ||
707 | 92 | const Json::Value get_output_json() const | ||
708 | 93 | { | ||
709 | 94 | return _json_out; | ||
710 | 95 | } | ||
711 | 96 | /* | ||
712 | 97 | * Get an input json object. | ||
713 | 98 | * | ||
714 | 99 | * @return a const json object. | ||
715 | 100 | */ | ||
716 | 101 | const Json::Value get_input_json() const | ||
717 | 102 | { | ||
718 | 103 | return _json_in; | ||
719 | 104 | } | ||
720 | 105 | |||
721 | 106 | private: | ||
722 | 107 | /* | ||
723 | 108 | * Stores input json object. | ||
724 | 109 | */ | ||
725 | 110 | Json::Value _json_in; | ||
726 | 111 | /* | ||
727 | 112 | * Stores output json object. | ||
728 | 113 | */ | ||
729 | 114 | Json::Value _json_out; | ||
730 | 115 | /* | ||
731 | 116 | * Stores input string. | ||
732 | 117 | */ | ||
733 | 118 | std::string _input_query; | ||
734 | 119 | /* | ||
735 | 120 | * Stores output string. | ||
736 | 121 | */ | ||
737 | 122 | std::string _output_query; | ||
738 | 123 | }; | ||
739 | 124 | } | ||
740 | 125 | } | ||
741 | 0 | 126 | ||
742 | === modified file 'plugin/json_server/json_server.cc' | |||
743 | --- plugin/json_server/json_server.cc 2012-07-17 09:30:57 +0000 | |||
744 | +++ plugin/json_server/json_server.cc 2013-08-13 04:20:40 +0000 | |||
745 | @@ -1,7 +1,7 @@ | |||
746 | 1 | /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- | 1 | /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- |
747 | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: | 2 | * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: |
748 | 3 | * | 3 | * |
750 | 4 | * Copyright (C) 2011 Stewart Smith, Henrik Ingo, Mohit Srivastava | 4 | * Copyright (C) 2011-2013 Stewart Smith, Henrik Ingo, Mohit Srivastava |
751 | 5 | * | 5 | * |
752 | 6 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify |
753 | 7 | * it under the terms of the GNU General Public License as published by | 7 | * it under the terms of the GNU General Public License as published by |
754 | @@ -46,10 +46,11 @@ | |||
755 | 46 | #include <event.h> | 46 | #include <event.h> |
756 | 47 | #include <drizzled/execute.h> | 47 | #include <drizzled/execute.h> |
757 | 48 | #include <drizzled/sql/result_set.h> | 48 | #include <drizzled/sql/result_set.h> |
759 | 49 | 49 | #include <drizzled/diagnostics_area.h> | |
760 | 50 | #include <drizzled/plugin/listen.h> | 50 | #include <drizzled/plugin/listen.h> |
761 | 51 | #include <drizzled/plugin/client.h> | 51 | #include <drizzled/plugin/client.h> |
762 | 52 | #include <drizzled/catalog/local.h> | 52 | #include <drizzled/catalog/local.h> |
763 | 53 | #include <drizzled/current_session.h> | ||
764 | 53 | 54 | ||
765 | 54 | #include <drizzled/pthread_globals.h> | 55 | #include <drizzled/pthread_globals.h> |
766 | 55 | #include <boost/bind.hpp> | 56 | #include <boost/bind.hpp> |
767 | @@ -60,6 +61,8 @@ | |||
768 | 60 | #include <plugin/json_server/db_access.h> | 61 | #include <plugin/json_server/db_access.h> |
769 | 61 | #include <plugin/json_server/http_handler.h> | 62 | #include <plugin/json_server/http_handler.h> |
770 | 62 | #include <plugin/json_server/http_server.h> | 63 | #include <plugin/json_server/http_server.h> |
771 | 64 | #include <plugin/json_server/ddl/schema.h> | ||
772 | 65 | #include <plugin/json_server/json_handler.h> | ||
773 | 63 | 66 | ||
774 | 64 | namespace po= boost::program_options; | 67 | namespace po= boost::program_options; |
775 | 65 | using namespace drizzled; | 68 | using namespace drizzled; |
776 | @@ -95,6 +98,8 @@ | |||
777 | 95 | extern "C" void process_version_req(struct evhttp_request *req, void* ); | 98 | extern "C" void process_version_req(struct evhttp_request *req, void* ); |
778 | 96 | extern "C" void process_sql_req(struct evhttp_request *req, void* ); | 99 | extern "C" void process_sql_req(struct evhttp_request *req, void* ); |
779 | 97 | extern "C" void process_json_req(struct evhttp_request *req, void* ); | 100 | extern "C" void process_json_req(struct evhttp_request *req, void* ); |
780 | 101 | extern "C" void process_json_ddl_schema_create_req(struct evhttp_request *req, void* ); | ||
781 | 102 | extern "C" void process_json_ddl_schema_drop_req(struct evhttp_request *req, void* ); | ||
782 | 98 | extern "C" void process_request(struct evhttp_request *req, void* ) | 103 | extern "C" void process_request(struct evhttp_request *req, void* ) |
783 | 99 | { | 104 | { |
784 | 100 | struct evbuffer *buf = evbuffer_new(); | 105 | struct evbuffer *buf = evbuffer_new(); |
785 | @@ -357,6 +362,104 @@ | |||
786 | 357 | delete(handler); | 362 | delete(handler); |
787 | 358 | } | 363 | } |
788 | 359 | 364 | ||
789 | 365 | /** | ||
790 | 366 | * Transform a HTTP Request for create schema and returns results based on the input json. | ||
791 | 367 | * | ||
792 | 368 | * @param req a HTTP request parameter, | ||
793 | 369 | * | ||
794 | 370 | */ | ||
795 | 371 | |||
796 | 372 | extern "C" void process_json_ddl_schema_create_req(struct evhttp_request *req, void* ) | ||
797 | 373 | { | ||
798 | 374 | drizzled::Session::shared_ptr _session= drizzled::Session::make_shared(drizzled::plugin::Listen::getNullClient(), | ||
799 | 375 | drizzled::catalog::local()); | ||
800 | 376 | drizzled::identifier::user::mptr user_id= identifier::User::make_shared(); | ||
801 | 377 | _session->main_da().reset_diagnostics_area(); | ||
802 | 378 | setCurrentSession(_session.get()); | ||
803 | 379 | |||
804 | 380 | std::string query; | ||
805 | 381 | std::string db_name; | ||
806 | 382 | std::string output; | ||
807 | 383 | Json::Value json_out; | ||
808 | 384 | Json::Value json_in; | ||
809 | 385 | const char *http_response_text="OK"; | ||
810 | 386 | int http_response_code=HTTP_OK; | ||
811 | 387 | |||
812 | 388 | JsonErrorArea _json_error; | ||
813 | 389 | JsonHandler* _json_handler = new JsonHandler(); | ||
814 | 390 | |||
815 | 391 | _json_handler->generate_input_json(req,_json_error); | ||
816 | 392 | if(!_json_error.is_jsonerror()) | ||
817 | 393 | { | ||
818 | 394 | json_in = _json_handler->get_input_json(); | ||
819 | 395 | db_name=json_in["query"]["name"].asString(); | ||
820 | 396 | Schema *_schema = new Schema(_session.get(),db_name); | ||
821 | 397 | _schema->createSchema(); | ||
822 | 398 | if(_session->main_da().is_error()) | ||
823 | 399 | { | ||
824 | 400 | _json_error.set_error(JsonErrorArea::ER_SQL,_session->main_da().sql_errno(),_session->main_da().message()); | ||
825 | 401 | } | ||
826 | 402 | } | ||
827 | 403 | _json_handler->generate_output_query(_json_error); | ||
828 | 404 | output = _json_handler->get_output_query(); | ||
829 | 405 | struct evbuffer *buf = evbuffer_new(); | ||
830 | 406 | if(buf == NULL) | ||
831 | 407 | { | ||
832 | 408 | return; | ||
833 | 409 | } | ||
834 | 410 | evbuffer_add(buf, output.c_str(), output.length()); | ||
835 | 411 | evhttp_send_reply( req, http_response_code, http_response_text, buf); | ||
836 | 412 | } | ||
837 | 413 | |||
838 | 414 | /** | ||
839 | 415 | * Transform a HTTP Request for create schema and returns results based on the input json. | ||
840 | 416 | * | ||
841 | 417 | * @param req a HTTP request parameter. | ||
842 | 418 | */ | ||
843 | 419 | extern "C" void process_json_ddl_schema_drop_req(struct evhttp_request *req, void* ) | ||
844 | 420 | { | ||
845 | 421 | drizzled::Session::shared_ptr _session= drizzled::Session::make_shared(drizzled::plugin::Listen::getNullClient(), | ||
846 | 422 | drizzled::catalog::local()); | ||
847 | 423 | drizzled::identifier::user::mptr user_id= identifier::User::make_shared(); | ||
848 | 424 | _session->main_da().reset_diagnostics_area(); | ||
849 | 425 | setCurrentSession(_session.get()); | ||
850 | 426 | |||
851 | 427 | std::string query; | ||
852 | 428 | std::string db_name; | ||
853 | 429 | std::string output; | ||
854 | 430 | Json::Value json_out; | ||
855 | 431 | Json::Value json_in; | ||
856 | 432 | const char *http_response_text="OK"; | ||
857 | 433 | int http_response_code=HTTP_OK; | ||
858 | 434 | |||
859 | 435 | JsonErrorArea _json_error; | ||
860 | 436 | JsonHandler* _json_handler = new JsonHandler(); | ||
861 | 437 | |||
862 | 438 | _json_handler->generate_input_json(req,_json_error); | ||
863 | 439 | if(!_json_error.is_jsonerror()) | ||
864 | 440 | { | ||
865 | 441 | json_in = _json_handler->get_input_json(); | ||
866 | 442 | |||
867 | 443 | db_name=json_in["query"]["name"].asString(); | ||
868 | 444 | Schema *_schema = new Schema(_session.get(),db_name); | ||
869 | 445 | _schema->dropSchema(); | ||
870 | 446 | if(_session->main_da().is_error()) | ||
871 | 447 | { | ||
872 | 448 | _json_error.set_error(JsonErrorArea::ER_SQL,_session->main_da().sql_errno(),_session->main_da().message()); | ||
873 | 449 | } | ||
874 | 450 | } | ||
875 | 451 | _json_handler->generate_output_query(_json_error); | ||
876 | 452 | output = _json_handler->get_output_query(); | ||
877 | 453 | struct evbuffer *buf = evbuffer_new(); | ||
878 | 454 | if(buf == NULL) | ||
879 | 455 | { | ||
880 | 456 | return; | ||
881 | 457 | } | ||
882 | 458 | evbuffer_add(buf, output.c_str(), output.length()); | ||
883 | 459 | evhttp_send_reply( req, http_response_code, http_response_text, buf); | ||
884 | 460 | } | ||
885 | 461 | |||
886 | 462 | |||
887 | 360 | static void shutdown_event(int fd, short, void *arg) | 463 | static void shutdown_event(int fd, short, void *arg) |
888 | 361 | { | 464 | { |
889 | 362 | struct event_base *base= (struct event_base *)arg; | 465 | struct event_base *base= (struct event_base *)arg; |
890 | @@ -466,6 +569,8 @@ | |||
891 | 466 | evhttp_set_cb(httpd, "/version", process_version_req, NULL); | 569 | evhttp_set_cb(httpd, "/version", process_version_req, NULL); |
892 | 467 | evhttp_set_cb(httpd, "/sql", process_sql_req, NULL); | 570 | evhttp_set_cb(httpd, "/sql", process_sql_req, NULL); |
893 | 468 | evhttp_set_cb(httpd, "/json", process_json_req, NULL); | 571 | evhttp_set_cb(httpd, "/json", process_json_req, NULL); |
894 | 572 | evhttp_set_cb(httpd,"/json/ddl/schema/create", process_json_ddl_schema_create_req, NULL); | ||
895 | 573 | evhttp_set_cb(httpd,"/json/ddl/schema/drop", process_json_ddl_schema_drop_req, NULL); | ||
896 | 469 | 574 | ||
897 | 470 | 575 | ||
898 | 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); |
899 | 472 | 577 | ||
900 | === modified file 'plugin/json_server/plugin.ini' | |||
901 | --- plugin/json_server/plugin.ini 2013-02-06 08:10:34 +0000 | |||
902 | +++ plugin/json_server/plugin.ini 2013-08-13 04:20:40 +0000 | |||
903 | @@ -5,7 +5,10 @@ | |||
904 | 5 | sql_to_json_generator.h | 5 | sql_to_json_generator.h |
905 | 6 | http_handler.h | 6 | http_handler.h |
906 | 7 | http_server.h | 7 | http_server.h |
907 | 8 | json_handler.h | ||
908 | 9 | error.h | ||
909 | 8 | db_access.h | 10 | db_access.h |
910 | 11 | ddl/schema.h | ||
911 | 9 | json/autolink.h | 12 | json/autolink.h |
912 | 10 | json/config.h | 13 | json/config.h |
913 | 11 | json/features.h | 14 | json/features.h |
914 | @@ -25,7 +28,10 @@ | |||
915 | 25 | sql_to_json_generator.cc | 28 | sql_to_json_generator.cc |
916 | 26 | http_handler.cc | 29 | http_handler.cc |
917 | 27 | http_server.cc | 30 | http_server.cc |
918 | 31 | json_handler.cc | ||
919 | 32 | error.cc | ||
920 | 28 | db_access.cc | 33 | db_access.cc |
921 | 34 | ddl/schema.cc | ||
922 | 29 | json/json_reader.cpp | 35 | json/json_reader.cpp |
923 | 30 | json/json_value.cpp | 36 | json/json_value.cpp |
924 | 31 | json/json_writer.cpp | 37 | json/json_writer.cpp |
925 | 32 | 38 | ||
926 | === modified file 'plugin/json_server/tests/r/basic.result' | |||
927 | --- plugin/json_server/tests/r/basic.result 2012-07-14 14:10:13 +0000 | |||
928 | +++ plugin/json_server/tests/r/basic.result 2013-08-13 04:20:40 +0000 | |||
929 | @@ -287,3 +287,21 @@ | |||
930 | 287 | SET GLOBAL json_server_table=""; | 287 | SET GLOBAL json_server_table=""; |
931 | 288 | SET GLOBAL json_server_schema="test"; | 288 | SET GLOBAL json_server_schema="test"; |
932 | 289 | drop schema json; | 289 | drop schema json; |
933 | 290 | { | ||
934 | 291 | "sql_state" : "00000" | ||
935 | 292 | } | ||
936 | 293 | { | ||
937 | 294 | "error_message" : "Can't create schema 'json'; schema exists", | ||
938 | 295 | "error_no" : 1007, | ||
939 | 296 | "error_type" : "SQL ERROR", | ||
940 | 297 | "sql_state" : "HY000" | ||
941 | 298 | } | ||
942 | 299 | { | ||
943 | 300 | "sql_state" : "00000" | ||
944 | 301 | } | ||
945 | 302 | { | ||
946 | 303 | "error_message" : "Can't drop schema 'json'; schema doesn't exist", | ||
947 | 304 | "error_no" : 1008, | ||
948 | 305 | "error_type" : "SQL ERROR", | ||
949 | 306 | "sql_state" : "HY000" | ||
950 | 307 | } | ||
951 | 290 | 308 | ||
952 | === added file 'plugin/json_server/tests/t/basic.test' | |||
953 | --- plugin/json_server/tests/t/basic.test 1970-01-01 00:00:00 +0000 | |||
954 | +++ plugin/json_server/tests/t/basic.test 2013-08-13 04:20:40 +0000 | |||
955 | @@ -0,0 +1,94 @@ | |||
956 | 1 | create table t1 (a int primary key auto_increment, b varchar(100)); | ||
957 | 2 | --replace_result $JSON_SERVER_PORT PORT | ||
958 | 3 | --eval select http_post("http://localhost:$JSON_SERVER_PORT/sql", 'select * from t1;'); | ||
959 | 4 | insert into t1 (b) values ("from MySQL protocol"); | ||
960 | 5 | --replace_result $JSON_SERVER_PORT PORT | ||
961 | 6 | --eval select http_post('http://localhost:$JSON_SERVER_PORT/sql', 'select * from t1;'); | ||
962 | 7 | --replace_result $JSON_SERVER_PORT PORT | ||
963 | 8 | --eval select http_post('http://localhost:$JSON_SERVER_PORT/sql', 'insert into t1 (b) values (\'from http\');'); | ||
964 | 9 | SELECT * from t1; | ||
965 | 10 | drop table t1; | ||
966 | 11 | |||
967 | 12 | create schema json; | ||
968 | 13 | |||
969 | 14 | use json; | ||
970 | 15 | |||
971 | 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' | ||
972 | 17 | |||
973 | 18 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=1' | ||
974 | 19 | |||
975 | 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' | ||
976 | 21 | |||
977 | 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' | ||
978 | 23 | |||
979 | 24 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=1' | ||
980 | 25 | |||
981 | 26 | --exec curl -X POST 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=2' | ||
982 | 27 | |||
983 | 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' | ||
984 | 29 | |||
985 | 30 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json' | ||
986 | 31 | |||
987 | 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' | ||
988 | 33 | |||
989 | 34 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people' | ||
990 | 35 | |||
991 | 36 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%7D%7D' | ||
992 | 37 | |||
993 | 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' | ||
994 | 39 | |||
995 | 40 | SET GLOBAL json_server_schema="json"; | ||
996 | 41 | |||
997 | 42 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?table=people' | ||
998 | 43 | |||
999 | 44 | SET GLOBAL json_server_table="people"; | ||
1000 | 45 | |||
1001 | 46 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json' | ||
1002 | 47 | |||
1003 | 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' | ||
1004 | 49 | |||
1005 | 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' | ||
1006 | 51 | |||
1007 | 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' | ||
1008 | 53 | |||
1009 | 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' | ||
1010 | 55 | |||
1011 | 56 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=aboutpeople' | ||
1012 | 57 | |||
1013 | 58 | --exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&_id=1' | ||
1014 | 59 | |||
1015 | 60 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people' | ||
1016 | 61 | |||
1017 | 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' | ||
1018 | 63 | |||
1019 | 64 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people' | ||
1020 | 65 | |||
1021 | 66 | --exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json' | ||
1022 | 67 | |||
1023 | 68 | --exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people' | ||
1024 | 69 | |||
1025 | 70 | --exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%7D%7D' | ||
1026 | 71 | |||
1027 | 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' | ||
1028 | 73 | |||
1029 | 74 | SET GLOBAL json_server_allow_drop_table="ON"; | ||
1030 | 75 | |||
1031 | 76 | --exec curl -X DELETE 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people&query=%7B%22query%22%3A%7B%7D%7D' | ||
1032 | 77 | |||
1033 | 78 | --exec curl 'http://localhost:$JSON_SERVER_PORT/json?schema=json&table=people' | ||
1034 | 79 | |||
1035 | 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' | ||
1036 | 81 | |||
1037 | 82 | SET GLOBAL json_server_allow_drop_table="OFF"; | ||
1038 | 83 | SET GLOBAL json_server_table=""; | ||
1039 | 84 | SET GLOBAL json_server_schema="test"; | ||
1040 | 85 | |||
1041 | 86 | drop schema json; | ||
1042 | 87 | |||
1043 | 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' | ||
1044 | 89 | |||
1045 | 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' | ||
1046 | 91 | |||
1047 | 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' | ||
1048 | 93 | |||
1049 | 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' |
Minor fixes: json_server/ ddl/schema. cc that it's mostly just a copy from drizzled/ statement/ create_ schema. cc and friends.
- Copyright date should be 2011-2013 rather than just 2011
- It would be good to have a comment in plugin/
- 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.