2116.1.37
by David Shrewsbury
Add license to sql_exector.cc |
1 |
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2011 David Shrewsbury
|
|
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 |
||
2116.1.38
by David Shrewsbury
Change include style |
21 |
#include <config.h> |
22 |
#include <plugin/slave/sql_executor.h> |
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
23 |
#include <drizzled/plugin/listen.h> |
24 |
#include <drizzled/plugin/client.h> |
|
25 |
#include <drizzled/catalog/local.h> |
|
26 |
#include <drizzled/execute.h> |
|
27 |
#include <drizzled/sql/result_set.h> |
|
2116.1.32
by David Shrewsbury
incremental |
28 |
#include <drizzled/errmsg_print.h> |
2239.1.6
by Olaf van der Spek
Refactor includes |
29 |
#include <drizzled/plugin.h> |
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
30 |
|
31 |
using namespace std; |
|
32 |
using namespace drizzled; |
|
33 |
||
34 |
namespace slave |
|
35 |
{
|
|
36 |
||
37 |
SQLExecutor::SQLExecutor(const string &user, const string &schema) |
|
38 |
: _in_error_state(false) |
|
39 |
{
|
|
40 |
/* setup a Session object */
|
|
2385.1.7
by Olaf van der Spek
Rename set_db to set_schema |
41 |
_session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local()); |
2252.1.8
by Olaf van der Spek
Common fwd |
42 |
identifier::user::mptr user_id= identifier::User::make_shared(); |
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
43 |
user_id->setUser(user); |
44 |
_session->setUser(user_id); |
|
2385.1.7
by Olaf van der Spek
Rename set_db to set_schema |
45 |
_session->set_schema(schema); |
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
46 |
}
|
47 |
||
48 |
||
49 |
bool SQLExecutor::executeSQL(vector<string> &sql) |
|
50 |
{
|
|
51 |
if (not _in_error_state) |
|
52 |
_error_message.clear(); |
|
53 |
||
54 |
Execute execute(*(_session.get()), true); |
|
55 |
||
2385.1.7
by Olaf van der Spek
Rename set_db to set_schema |
56 |
string combined_sql; |
57 |
BOOST_FOREACH(string& it, sql) |
|
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
58 |
{
|
2385.1.7
by Olaf van der Spek
Rename set_db to set_schema |
59 |
combined_sql.append(it); |
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
60 |
combined_sql.append("; "); |
61 |
}
|
|
62 |
||
63 |
sql::ResultSet result_set(1); |
|
64 |
||
65 |
/* Execute wraps the SQL to run within a transaction */
|
|
66 |
execute.run(combined_sql, result_set); |
|
67 |
||
68 |
sql::Exception exception= result_set.getException(); |
|
69 |
||
70 |
drizzled::error_t err= exception.getErrorCode(); |
|
71 |
||
72 |
if ((err != drizzled::EE_OK) && (err != drizzled::ER_EMPTY_QUERY)) |
|
73 |
{
|
|
74 |
/* avoid recursive errors */
|
|
75 |
if (_in_error_state) |
|
76 |
return true; |
|
77 |
||
78 |
_in_error_state= true; |
|
79 |
_error_message= "(SQLSTATE "; |
|
80 |
_error_message.append(exception.getSQLState()); |
|
81 |
_error_message.append(") "); |
|
82 |
_error_message.append(exception.getErrorMessage()); |
|
83 |
||
84 |
string bad_sql("Failure while executing:\n"); |
|
85 |
for (size_t y= 0; y < sql.size(); y++) |
|
86 |
{
|
|
87 |
bad_sql.append(sql[y]); |
|
88 |
bad_sql.append("\n"); |
|
89 |
}
|
|
90 |
||
2385.1.7
by Olaf van der Spek
Rename set_db to set_schema |
91 |
errmsg_printf(error::ERROR, _("%s\n%s\n"), _error_message.c_str(), bad_sql.c_str()); |
2116.1.31
by David Shrewsbury
Major refactor of common functionality into new classes. |
92 |
return false; |
93 |
}
|
|
94 |
||
95 |
return true; |
|
96 |
}
|
|
97 |
||
98 |
} /* namespace slave */ |