drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
db_access.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/db_access.h>
25
26namespace drizzle_plugin
27{
28namespace json_server
29{
30 DBAccess::DBAccess(Json::Value &json_in,Json::Value &json_out,enum evhttp_cmd_type type,const char* schema,const char* table)
31 {
32 _json_in= json_in;
33 _json_out= json_out;
34 _type=type;
35 _schema=schema;
36 _table=table;
37 }
38
40 {
41 std::string sql;
43 generator->generateSql(_type);
44 sql= generator->getSQL();
45
46 SQLExecutor* executor = new SQLExecutor(_schema);
47 SQLToJsonGenerator* jsonGenerator = new SQLToJsonGenerator(_json_out,_schema,_table,executor);
48 if(executor->executeSQL(sql))
49 {
50 jsonGenerator->generateJson(_type);
51 }
52 else
53 {
54 // For POST request, if a table didn't exist, we create the table
55 // transparently then retry the INSERT.
56 if(_type == EVHTTP_REQ_POST && executor->getErr() == drizzled::ER_TABLE_UNKNOWN)
57 {
58 generator->generateCreateTableSql();
59 sql= generator->getSQL();
60 if(executor->executeSQL(sql))
61 {
62 generator->generateSql(_type);
63 sql= generator->getSQL();
64 if(executor->executeSQL(sql))
65 {
66 jsonGenerator->generateJson(_type);
67 }
68 else
69 {
70 jsonGenerator->generateSQLErrorJson();
71 }
72 }
73 else
74 {
75 jsonGenerator->generateSQLErrorJson();
76 }
77 }
78 else
79 {
80 jsonGenerator->generateSQLErrorJson();
81 }
82 }
83 _json_out= jsonGenerator->getJson();
84 delete(jsonGenerator);
85 delete(executor);
86 delete(generator);
87 }
88
89}
90}
DBAccess(Json::Value &json_in, Json::Value &json_out, enum evhttp_cmd_type type, const char *schema, const char *table)
Definition db_access.cc:30
Convert from decimal to binary representation.
Definition common_fwd.h:215