Drizzled Public API Documentation

sql_executor.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 #include <config.h>
26 #include <plugin/json_server/sql_executor.h>
27 #include <drizzled/plugin/listen.h>
28 #include <drizzled/plugin/client.h>
29 #include <drizzled/catalog/local.h>
30 #include <drizzled/execute.h>
31 #include <drizzled/sql/result_set.h>
32 #include <drizzled/errmsg_print.h>
33 #include <drizzled/plugin.h>
34 
35 using namespace std;
36 using namespace drizzled;
37 
38 namespace drizzle_plugin
39 {
40 namespace json_server
41 {
42 SQLExecutor::SQLExecutor(const string &schema)
43  : _in_error_state(false)
44 {
45  /* setup a Session object */
46  _session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
47  identifier::user::mptr user_id= identifier::User::make_shared();
48  user_id->setUser("");
49  _session->setUser(user_id);
50  _session->set_schema(schema);
51  _sql="";
52  _result_set=NULL;
53 }
54 
55 bool SQLExecutor::executeSQL(string &sql)
56 {
57  _sql=sql;
58  if(_result_set)
59  {
60  delete(_result_set);
61  }
63  Execute execute(*(_session.get()), true);
64  /*wraps the SQL to run within a transaction */
65  execute.run(_sql, *_result_set);
66 
67  _exception= _result_set->getException();
68 
69  _err= _exception.getErrorCode();
70 
71  if ((_err != EE_OK) && (_err != ER_EMPTY_QUERY))
72  {
73  /* avoid recursive errors */
74  if (_in_error_state)
75  {
76  return true;
77  }
78 
79  return false;
80  }
81 
82  return true;
83 }
84 } /* namespace json_server*/
85 }