-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsql_stmt.cpp
More file actions
75 lines (65 loc) · 2.33 KB
/
sql_stmt.cpp
File metadata and controls
75 lines (65 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/// sql_stmt.cpp -- sql statement impl
/// Author: Zhang Yichao <echaozh@gmail.com>
/// Created: 2011-08-03
///
#include "exception.hpp"
#include "sql_stmt.hpp"
using namespace std;
struct json_putter
{
json_putter (struct json_object *p) : p_ (p) {}
~json_putter () {json_object_put (p_);}
private:
struct json_object *p_;
};
sql_stmt::sql_stmt (cppzmq::packet_t &&a, const cppzmq::message_t &sql,
const tr1::unordered_map<string, tr1::shared_ptr<mysql_stmt>
> &stmts)
: addr (a), id (0), err (success), txn_seq (0), builtin (none), params (0)
{
try {
struct json_tokener *parser = json_tokener_new ();
if (!parser)
throw bad_alloc ();
struct json_object *parsed =
json_tokener_parse_ex (parser, (char *) sql.data (), sql.size ());
json_tokener_free (parser);
if (!parsed)
throw coded_error (bad_req, "malformed json");
json_putter jf (parsed);
struct json_object *p = json_object_object_get (parsed, "id");
if (!p)
throw coded_error (bad_req, "no id specified");
id = json_object_get_int (p);
if (!id)
throw coded_error (bad_req, "no id specified");
p = json_object_object_get (parsed, "sql");
if (!p)
throw coded_error (bad_req, "no statement specified");
string name = json_object_get_string (p);
if (name == "begin")
builtin = begin;
else if (name == "commit")
builtin = commit;
else if (name == "rollback")
builtin = rollback;
else {
if (stmts.find (name) == stmts.end ())
throw coded_error (bad_req, "unknown statement");
stmt = stmts.find (name)->second;
}
p = json_object_object_get (parsed, "txn");
if (p)
txn_seq = json_object_get_int (p);
params = json_object_object_get (parsed, "params");
if (params) {
if (!json_object_is_type (params, json_type_array))
throw coded_error (bad_arg, "params must be an array");
json_object_get (params);
json_object_object_del (parsed, "params");
}
} catch (const coded_error &e) {
err = e.code ();
msg = e.what ();
}
}