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 
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   /* setup a Session object */
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   /* Execute wraps the SQL to run within a transaction */
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     /* avoid recursive errors */
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 } /* namespace slave */