Drizzled Public API Documentation

sql_to_json_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  */
24 #include <plugin/json_server/sql_to_json_generator.h>
25 
26 using namespace std;
27 using namespace drizzled;
28 
29 namespace drizzle_plugin
30 {
31 namespace json_server
32 {
33  SQLToJsonGenerator::SQLToJsonGenerator(Json::Value& json_out,const char* schema,const char* table,SQLExecutor* sqlExecutor)
34  {
35  _schema=schema;
36  _table=table;
37  _sql_executor=sqlExecutor;
38  _json_out=json_out;
39 
40  }
41 
42  void SQLToJsonGenerator::generateSQLErrorJson()
43  {
44  sql::Exception _exception= _sql_executor->getException();
45  _json_out["error_type"]= "sql error";
46  _json_out["error_message"]= _exception.getErrorMessage();
47  _json_out["error_code"]= _exception.getErrorCode();
48  _json_out["internal_sql_query"]= _sql_executor->getSql();
49  _json_out["schema"]= _schema;
50  _json_out["sqlstate"]= _exception.getSQLState();
51  _json_out["table"]= _table;
52  }
53 
54  void SQLToJsonGenerator::generateJson(enum evhttp_cmd_type type)
55  {
56  if(type==EVHTTP_REQ_GET)
57  generateGetJson();
58  else if(type==EVHTTP_REQ_POST)
59  generatePostJson();
60  else if(type==EVHTTP_REQ_DELETE)
61  generateDeleteJson();
62  }
63 
64  void SQLToJsonGenerator::generateGetJson()
65  {
66  sql::ResultSet *_result_set= _sql_executor->getResultSet();
67  sql::Exception exception= _sql_executor->getException();
68  // Handle each row of the result set
69  while (_result_set->next())
70  {
71  Json::Value json_row;
72  bool got_error = false;
73  // Handle each column of a row
74  for (size_t x= 0; x < _result_set->getMetaData().getColumnCount() && got_error == false; x++)
75  {
76  // Only output non-null rows
77  if (not _result_set->isNull(x))
78  {
79  Json::Value json_doc;
80  Json::Features json_conf;
81  Json::Reader readrow(json_conf);
82  std::string col_name = _result_set->getColumnInfo(x).col_name;
83  bool r = readrow.parse(_result_set->getString(x), json_doc);
84  if (r != true)
85  {
86  _json_out["error_type"]="json parse error on row value";
87  _json_out["error_internal_sql_column"]=col_name;
88  // Just put the string there as it is, better than nothing.
89  json_row[col_name]= _result_set->getString(x);
90  got_error=true;
91  break;
92  }
93  else
94  {
95  json_row[col_name]= json_doc;
96  }
97  }
98  }
99  // When done, append this row to result set tree
100  _json_out["result_set"].append(json_row);
101  }
102  _json_out["sqlstate"]= exception.getSQLState();
103  }
104 
105  void SQLToJsonGenerator::generatePostJson()
106  {
107  sql::Exception exception= _sql_executor->getException();
108  _json_out["sqlstate"]= exception.getSQLState();
109  }
110 
111  void SQLToJsonGenerator::generateDeleteJson()
112  {
113  sql::Exception exception= _sql_executor->getException();
114  _json_out["sqlstate"]= exception.getSQLState();
115  }
116 
117 }
118 }
TODO: Rename this file - func.h is stupid.
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
Represents a JSON value.
Definition: value.h:149
Configuration passed to reader and writer. This configuration object can be used to force the Reader ...
Definition: features.h:50
SendField getColumnInfo(size_t column_number)
Get object that holds column meta data.
Definition: result_set.h:134
Unserialize a JSON document into a Value.
Definition: reader.h:52
Value & append(const Value &value)
Append value to array at the end.