00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022 #include <plugin/slave/sql_executor.h>
00023 #include <drizzled/plugin/listen.h>
00024 #include <drizzled/plugin/client.h>
00025 #include <drizzled/catalog/local.h>
00026 #include <drizzled/execute.h>
00027 #include <drizzled/sql/result_set.h>
00028 #include <drizzled/errmsg_print.h>
00029
00030 using namespace std;
00031 using namespace drizzled;
00032
00033 namespace slave
00034 {
00035
00036 SQLExecutor::SQLExecutor(const string &user, const string &schema)
00037 : _in_error_state(false)
00038 {
00039
00040 _session= Session::make_shared(plugin::Listen::getNullClient(),
00041 catalog::local());
00042 identifier::User::shared_ptr user_id= identifier::User::make_shared();
00043 user_id->setUser(user);
00044 _session->setUser(user_id);
00045 _session->set_db(schema);
00046 }
00047
00048
00049 bool SQLExecutor::executeSQL(vector<string> &sql)
00050 {
00051 string combined_sql;
00052
00053 if (not _in_error_state)
00054 _error_message.clear();
00055
00056 Execute execute(*(_session.get()), true);
00057
00058 vector<string>::iterator iter= sql.begin();
00059
00060 while (iter != sql.end())
00061 {
00062 combined_sql.append(*iter);
00063 combined_sql.append("; ");
00064 ++iter;
00065 }
00066
00067 sql::ResultSet result_set(1);
00068
00069
00070 execute.run(combined_sql, result_set);
00071
00072 sql::Exception exception= result_set.getException();
00073
00074 drizzled::error_t err= exception.getErrorCode();
00075
00076 if ((err != drizzled::EE_OK) && (err != drizzled::ER_EMPTY_QUERY))
00077 {
00078
00079 if (_in_error_state)
00080 return true;
00081
00082 _in_error_state= true;
00083 _error_message= "(SQLSTATE ";
00084 _error_message.append(exception.getSQLState());
00085 _error_message.append(") ");
00086 _error_message.append(exception.getErrorMessage());
00087
00088 string bad_sql("Failure while executing:\n");
00089 for (size_t y= 0; y < sql.size(); y++)
00090 {
00091 bad_sql.append(sql[y]);
00092 bad_sql.append("\n");
00093 }
00094
00095 errmsg_printf(error::ERROR, _("%s\n%s\n"),
00096 _error_message.c_str(), bad_sql.c_str());
00097 return false;
00098 }
00099
00100 return true;
00101 }
00102
00103 }