drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
sql_generator.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2012 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 */
25
26#include <plugin/json_server/sql_generator.h>
27#include <cstdio>
28
29using namespace std;
30namespace drizzle_plugin
31{
32namespace json_server
33{
34namespace
35{
36 // jsoncpp 0.6's Value::asString() throws on a non-string scalar, so
37 // render numbers and booleans into their SQL literal form here.
38 std::string jsonScalarToString(const Json::Value &value)
39 {
40 if (value.isString())
41 return value.asString();
42 if (value.isNull())
43 return "";
44 Json::FastWriter writer;
45 std::string rendered= writer.write(value); // FastWriter appends '\n'
46 std::string::size_type newline= rendered.find('\n');
47 if (newline != std::string::npos)
48 rendered.erase(newline);
49 return rendered;
50 }
51}
52
53 SQLGenerator::SQLGenerator(const Json::Value json_in ,const char* schema ,const char* table)
54 {
55 _json_in=json_in["query"];
56 _sql="";
57 _schema=schema;
58 _table=table;
59 }
60
61 void SQLGenerator::generateSql(enum evhttp_cmd_type type)
62 {
63 if(type==EVHTTP_REQ_GET)
65 else if(type==EVHTTP_REQ_POST)
67 else if(type==EVHTTP_REQ_DELETE)
69 }
70
72 {
73 _sql="SELECT * FROM `";
74 _sql.append(_schema);
75 _sql.append("`.`");
76 _sql.append(_table);
77 _sql.append("`");
78 const Json::Value& id_val= _json_in["_id"];
79 if (!id_val.isNull() && !(id_val.isString() && id_val.asString().empty()))
80 {
81 _sql.append(" WHERE _id = ");
82 _sql.append(jsonScalarToString(id_val));
83 }
84 _sql.append(";");
85
86 }
87
89 {
90 _sql="COMMIT;";
91 _sql.append("CREATE TABLE ");
92 _sql.append(_schema);
93 _sql.append(".");
94 _sql.append(_table);
95 _sql.append(" (_id BIGINT PRIMARY KEY auto_increment,");
96 // Iterate over json_in keys
97 Json::Value::Members createKeys(_json_in.getMemberNames() );
98 for ( Json::Value::Members::iterator it = createKeys.begin(); it != createKeys.end(); ++it )
99 {
100 const std::string &key = *it;
101 if(key=="_id")
102 {
103 continue;
104 }
105 _sql.append(key);
106 _sql.append(" TEXT");
107 if( it !=createKeys.end()-1 && key !="_id")
108 {
109 _sql.append(",");
110 }
111 }
112 _sql.append(")");
113 _sql.append(";");
114 }
115
117 {
118 _sql="COMMIT;";
119 _sql.append("REPLACE INTO `");
120 _sql.append(_schema);
121 _sql.append("`.`");
122 _sql.append(_table);
123 _sql.append("` SET ");
124
125 Json::Value::Members keys( _json_in.getMemberNames() );
126
127 for ( Json::Value::Members::iterator it = keys.begin(); it != keys.end(); ++it )
128 {
129 if ( it != keys.begin() )
130 {
131 _sql.append(", ");
132 }
133 const std::string &key = *it;
134 _sql.append(key);
135 _sql.append("=");
136 Json::StyledWriter writeobject;
137 switch ( _json_in[key].type() )
138 {
139 case Json::nullValue:
140 _sql.append("NULL");
141 break;
142 case Json::intValue:
143 case Json::uintValue:
144 case Json::realValue:
145 case Json::booleanValue:
146 _sql.append(jsonScalarToString(_json_in[key]));
147 break;
148 case Json::stringValue:
149 _sql.append("'\"");
150 _sql.append(_json_in[key].asString());
151 _sql.append("\"'");
152 break;
153 case Json::arrayValue:
154 case Json::objectValue:
155 _sql.append("'");
156 _sql.append(writeobject.write(_json_in[key]));
157 _sql.append("'");
158 break;
159 default:
160 break;
161 }
162 _sql.append(" ");
163 }
164 _sql.append(";");
165 }
166
168 {
169 const Json::Value& id_val= _json_in["_id"];
170 if (!id_val.isNull() && !(id_val.isString() && id_val.asString().empty()))
171 {
172 _sql= "DELETE FROM `";
173 _sql.append(_schema);
174 _sql.append("`.`");
175 _sql.append(_table);
176 _sql.append("`");
177 _sql.append(" WHERE _id = ");
178 _sql.append(jsonScalarToString(id_val));
179 _sql.append(";");
180 }
181 else
182 {
183 _sql="COMMIT ;";
184 _sql.append("DROP TABLE `");
185 _sql.append(_schema);
186 _sql.append("`.`");
187 _sql.append(_table);
188 _sql.append("`;");
189 }
190
191 }
192
193}
194}
SQLGenerator(const Json::Value json_in, const char *schema, const char *table)
void generateSql(enum evhttp_cmd_type type)
Convert from decimal to binary representation.
Definition common_fwd.h:215
STL namespace.