Drizzled Public API Documentation

sql_executor.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2011 David Shrewsbury
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 #include <drizzled/plugin.h>
00030 
00031 using namespace std;
00032 using namespace drizzled;
00033 
00034 namespace slave
00035 {
00036 
00037 SQLExecutor::SQLExecutor(const string &user, const string &schema)
00038   : _in_error_state(false)
00039 {
00040   /* setup a Session object */
00041   _session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
00042   identifier::user::mptr user_id= identifier::User::make_shared();
00043   user_id->setUser(user);
00044   _session->setUser(user_id);
00045   _session->set_schema(schema);
00046 }
00047 
00048 
00049 bool SQLExecutor::executeSQL(vector<string> &sql)
00050 {
00051   if (not _in_error_state)
00052     _error_message.clear();
00053 
00054   Execute execute(*(_session.get()), true);
00055 
00056   string combined_sql;
00057   BOOST_FOREACH(string& it, sql)
00058   {
00059     combined_sql.append(it);
00060     combined_sql.append("; ");
00061   }
00062 
00063   sql::ResultSet result_set(1);
00064 
00065   /* Execute wraps the SQL to run within a transaction */
00066   execute.run(combined_sql, result_set);
00067 
00068   sql::Exception exception= result_set.getException();
00069 
00070   drizzled::error_t err= exception.getErrorCode();
00071 
00072   if ((err != drizzled::EE_OK) && (err != drizzled::ER_EMPTY_QUERY))
00073   {
00074     /* avoid recursive errors */
00075     if (_in_error_state)
00076       return true;
00077 
00078     _in_error_state= true;
00079     _error_message= "(SQLSTATE ";
00080     _error_message.append(exception.getSQLState());
00081     _error_message.append(") ");
00082     _error_message.append(exception.getErrorMessage());
00083 
00084     string bad_sql("Failure while executing:\n");
00085     for (size_t y= 0; y < sql.size(); y++)
00086     {
00087       bad_sql.append(sql[y]);
00088       bad_sql.append("\n");
00089     }
00090 
00091     errmsg_printf(error::ERROR, _("%s\n%s\n"), _error_message.c_str(), bad_sql.c_str());
00092     return false;
00093   }
00094 
00095   return true;
00096 }
00097 
00098 } /* namespace slave */