~ubuntu-branches/ubuntu/vivid/drizzle/vivid-proposed

« back to all changes in this revision

Viewing changes to plugin/json_server/sql_generator.cc

  • Committer: Package Import Robot
  • Author(s): Tobias Frost
  • Date: 2013-08-22 20:18:31 UTC
  • mto: (20.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20130822201831-gn3ozsh7o7wmc5tk
Tags: upstream-7.2.3
ImportĀ upstreamĀ versionĀ 7.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
20
/**
 
21
 * @file Implements the various memver function of class SQLGenerator 
 
22
 * which helps to generate the SQL strings corresponding to type of HTTP Request.
 
23
 *  
 
24
 */
 
25
 
 
26
#include <plugin/json_server/sql_generator.h>
 
27
#include <cstdio>
 
28
 
 
29
using namespace std;
 
30
namespace drizzle_plugin
 
31
{
 
32
namespace json_server
 
33
{
 
34
  SQLGenerator::SQLGenerator(const Json::Value json_in ,const char* schema ,const char* table)
 
35
  {
 
36
    _json_in=json_in["query"];
 
37
    _sql="";
 
38
    _schema=schema;
 
39
    _table=table;
 
40
  }
 
41
 
 
42
  void SQLGenerator::generateSql(enum evhttp_cmd_type type)
 
43
  {
 
44
    if(type==EVHTTP_REQ_GET)
 
45
      generateGetSql();
 
46
    else if(type==EVHTTP_REQ_POST)
 
47
      generatePostSql();
 
48
    else if(type==EVHTTP_REQ_DELETE)
 
49
      generateDeleteSql();
 
50
  }
 
51
 
 
52
  void SQLGenerator::generateGetSql()
 
53
  {
 
54
    _sql="SELECT * FROM `";
 
55
    _sql.append(_schema);
 
56
    _sql.append("`.`");
 
57
    _sql.append(_table);
 
58
    _sql.append("`"); 
 
59
    if ( _json_in["_id"].asBool() )
 
60
    {
 
61
          _sql.append(" WHERE _id = ");
 
62
            _sql.append(_json_in["_id"].asString());
 
63
    }
 
64
    _sql.append(";");
 
65
  
 
66
  }
 
67
 
 
68
  void SQLGenerator::generateCreateTableSql()
 
69
  {     
 
70
    _sql="COMMIT;";
 
71
    _sql.append("CREATE TABLE ");
 
72
    _sql.append(_schema);
 
73
    _sql.append(".");
 
74
    _sql.append(_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 )
 
79
    {
 
80
      const std::string &key = *it;
 
81
      if(key=="_id") 
 
82
      {
 
83
        continue;
 
84
      }
 
85
      _sql.append(key);
 
86
      _sql.append(" TEXT");
 
87
      if( it !=createKeys.end()-1 && key !="_id")
 
88
      {
 
89
        _sql.append(",");
 
90
      }
 
91
    }
 
92
    _sql.append(")");
 
93
    _sql.append(";");
 
94
  }
 
95
 
 
96
  void SQLGenerator::generatePostSql()
 
97
  {
 
98
    _sql="COMMIT;";
 
99
          _sql.append("REPLACE INTO `");
 
100
          _sql.append(_schema);
 
101
    _sql.append("`.`");
 
102
    _sql.append(_table);
 
103
    _sql.append("` SET ");
 
104
        
 
105
          Json::Value::Members keys( _json_in.getMemberNames() );
 
106
    
 
107
          for ( Json::Value::Members::iterator it = keys.begin(); it != keys.end(); ++it )
 
108
    {
 
109
      if ( it != keys.begin() )
 
110
      {
 
111
        _sql.append(", ");
 
112
                }
 
113
      const std::string &key = *it;
 
114
      _sql.append(key); 
 
115
      _sql.append("=");
 
116
      Json::StyledWriter writeobject;
 
117
      switch ( _json_in[key].type() )
 
118
      {
 
119
        case Json::nullValue:
 
120
          _sql.append("NULL");
 
121
          break;
 
122
        case Json::intValue:
 
123
        case Json::uintValue:
 
124
        case Json::realValue:
 
125
        case Json::booleanValue:
 
126
          _sql.append(_json_in[key].asString());
 
127
          break;
 
128
        case Json::stringValue:
 
129
          _sql.append("'\"");
 
130
          _sql.append(_json_in[key].asString());
 
131
          _sql.append("\"'");
 
132
          break;
 
133
        case Json::arrayValue:
 
134
        case Json::objectValue:
 
135
          _sql.append("'");
 
136
          _sql.append(writeobject.write(_json_in[key]));
 
137
          _sql.append("'");
 
138
          break;
 
139
        default:        
 
140
          break;
 
141
      }
 
142
      _sql.append(" ");
 
143
    }
 
144
    _sql.append(";");
 
145
  }
 
146
 
 
147
  void SQLGenerator::generateDeleteSql()
 
148
  {
 
149
    if ( _json_in["_id"].asBool() )
 
150
    {
 
151
      _sql= "DELETE FROM `";
 
152
      _sql.append(_schema);
 
153
      _sql.append("`.`");
 
154
      _sql.append(_table);
 
155
      _sql.append("`");
 
156
      _sql.append(" WHERE _id = ");
 
157
      _sql.append(_json_in["_id"].asString());
 
158
      _sql.append(";");
 
159
    }
 
160
    else
 
161
    {
 
162
      _sql="COMMIT ;";
 
163
      _sql.append("DROP TABLE `");
 
164
      _sql.append(_schema);
 
165
      _sql.append("`.`");
 
166
      _sql.append(_table);
 
167
      _sql.append("`;");
 
168
    }
 
169
 
 
170
  }
 
171
 
 
172
}
 
173
}