1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
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
23
#include <drizzled/statement/execute.h>
24
#include <drizzled/session.h>
25
#include <drizzled/execute.h>
26
#include <drizzled/user_var_entry.h>
27
#include <drizzled/plugin/listen.h>
28
#include <drizzled/plugin/client.h>
29
#include <drizzled/plugin/null_client.h>
30
#include <drizzled/plugin/client/concurrent.h>
31
#include <drizzled/sql_lex.h>
35
void parse(drizzled::Session&, const char *inBuf, uint32_t length);
39
Execute::Execute(Session *in_session,
40
execute_string_t to_execute_arg,
42
bool is_concurrent_arg,
43
bool should_wait_arg) :
44
Statement(in_session),
45
is_quiet(is_quiet_arg),
46
is_concurrent(is_concurrent_arg),
47
should_wait(should_wait_arg),
48
to_execute(to_execute_arg)
53
bool statement::Execute::parseVariable()
55
if (to_execute.isVariable())
57
user_var_entry *var= session().getVariable(to_execute, false);
59
if (var && var->length && var->value && var->type == STRING_RESULT)
61
lex_string_t tmp_for_var;
62
tmp_for_var.assign(var->value, var->length);
63
to_execute.set(tmp_for_var);
73
bool statement::Execute::runStatement(plugin::NullClient& client, const std::string &arg)
76
if (not session().executeStatement())
79
if (session().is_error())
86
bool statement::Execute::execute()
88
bool ret= execute_shell();
90
// We have to restore ourselves at the top for delete() to work.
91
lex().statement= this;
97
bool statement::Execute::execute_shell()
99
if (to_execute.size() == 0)
101
my_error(ER_WRONG_ARGUMENTS, MYF(0), "Invalid Variable");
105
if (to_execute.isVariable())
107
if (not parseVariable())
109
my_error(ER_WRONG_ARGUMENTS, MYF(0), "Invalid Variable");
116
if (not session().isConcurrentExecuteAllowed())
118
my_error(ER_WRONG_ARGUMENTS, MYF(0), "A Concurrent Execution Session can not launch another session.");
122
drizzled::Execute executer(session(), should_wait);
123
executer.run(to_execute);
125
else // Non-concurrent run.
129
plugin::Client *temp= session().getClient();
130
boost::scoped_ptr<plugin::NullClient> null_client(new plugin::NullClient);
132
session().setClient(null_client.get());
134
bool error_occured= false;
135
bool is_savepoint= false;
137
std::string start_sql;
138
if (session().inTransaction())
140
// @todo Figure out something a bit more solid then this.
141
start_sql= "SAVEPOINT execute_internal_savepoint";
146
start_sql= "START TRANSACTION";
149
error_occured= runStatement(*null_client, start_sql);
152
// @note this is copied from code in NULL client, all of this belongs
153
// in the pluggable parser pieces.
154
if (not error_occured)
156
typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
157
std::string full_string(to_execute.data(), to_execute.size());
158
Tokenizer tok(full_string, boost::escaped_list_separator<char>("\\", ";", "\""));
160
for (Tokenizer::iterator iter= tok.begin(); iter != tok.end(); ++iter)
162
if (session().getKilled() == Session::KILL_CONNECTION)
164
if (runStatement(*null_client, *iter))
171
// @todo Encapsulate logic later to method
173
std::string final_sql;
176
final_sql= error_occured ?
177
"ROLLBACK TO SAVEPOINT execute_internal_savepoint" :
178
"RELEASE SAVEPOINT execute_internal_savepoint";
182
final_sql= error_occured ? "ROLLBACK" : "COMMIT";
185
// Run the cleanup command, we currently ignore if an error occurs
187
(void)runStatement(*null_client, final_sql);
191
session().setClient(temp);
192
if (session().is_error())
194
session().clear_error(true);
198
session().clearDiagnostics();
203
null_client->close();
207
parse(session(), to_execute.data(), to_execute.size());
214
} /* namespace statement */
215
} /* namespace drizzled */