1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2012 Mohit Srivastava
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.
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.
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
21
* @file Implements the various memver function of class SQLGenerator
22
* which helps to generate the SQL strings corresponding to type of HTTP Request.
26
#include <plugin/json_server/sql_generator.h>
30
namespace drizzle_plugin
34
SQLGenerator::SQLGenerator(const Json::Value json_in ,const char* schema ,const char* table)
36
_json_in=json_in["query"];
42
void SQLGenerator::generateSql(enum evhttp_cmd_type type)
44
if(type==EVHTTP_REQ_GET)
46
else if(type==EVHTTP_REQ_POST)
48
else if(type==EVHTTP_REQ_DELETE)
52
void SQLGenerator::generateGetSql()
54
_sql="SELECT * FROM `";
59
if ( _json_in["_id"].asBool() )
61
_sql.append(" WHERE _id = ");
62
_sql.append(_json_in["_id"].asString());
68
void SQLGenerator::generateCreateTableSql()
71
_sql.append("CREATE TABLE ");
75
_sql.append(" (_id BIGINT PRIMARY KEY auto_increment,");
76
// Iterate over json_in keys
77
Json::Value::Members createKeys(_json_in.getMemberNames() );
78
for ( Json::Value::Members::iterator it = createKeys.begin(); it != createKeys.end(); ++it )
80
const std::string &key = *it;
87
if( it !=createKeys.end()-1 && key !="_id")
96
void SQLGenerator::generatePostSql()
99
_sql.append("REPLACE INTO `");
100
_sql.append(_schema);
103
_sql.append("` SET ");
105
Json::Value::Members keys( _json_in.getMemberNames() );
107
for ( Json::Value::Members::iterator it = keys.begin(); it != keys.end(); ++it )
109
if ( it != keys.begin() )
113
const std::string &key = *it;
116
Json::StyledWriter writeobject;
117
switch ( _json_in[key].type() )
119
case Json::nullValue:
123
case Json::uintValue:
124
case Json::realValue:
125
case Json::booleanValue:
126
_sql.append(_json_in[key].asString());
128
case Json::stringValue:
130
_sql.append(_json_in[key].asString());
133
case Json::arrayValue:
134
case Json::objectValue:
136
_sql.append(writeobject.write(_json_in[key]));
147
void SQLGenerator::generateDeleteSql()
149
if ( _json_in["_id"].asBool() )
151
_sql= "DELETE FROM `";
152
_sql.append(_schema);
156
_sql.append(" WHERE _id = ");
157
_sql.append(_json_in["_id"].asString());
163
_sql.append("DROP TABLE `");
164
_sql.append(_schema);