~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/slave/sql_executor.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-28 14:09:50 UTC
  • mfrom: (2207 bootstrap)
  • mto: (2209.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2210.
  • Revision ID: olafvdspek@gmail.com-20110228140950-2nu0hyzhuww3wssx
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
21
#include <config.h>
 
22
#include <plugin/slave/sql_executor.h>
 
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>
 
28
#include <drizzled/errmsg_print.h>
 
29
 
 
30
using namespace std;
 
31
using namespace drizzled;
 
32
 
 
33
namespace slave
 
34
{
 
35
 
 
36
SQLExecutor::SQLExecutor(const string &user, const string &schema)
 
37
  : _in_error_state(false)
 
38
{
 
39
  /* setup a Session object */
 
40
  _session= Session::make_shared(plugin::Listen::getNullClient(),
 
41
                                 catalog::local());
 
42
  identifier::User::shared_ptr user_id= identifier::User::make_shared();
 
43
  user_id->setUser(user);
 
44
  _session->setUser(user_id);
 
45
  _session->set_db(schema);
 
46
}
 
47
 
 
48
 
 
49
bool SQLExecutor::executeSQL(vector<string> &sql)
 
50
{
 
51
  string combined_sql;
 
52
 
 
53
  if (not _in_error_state)
 
54
    _error_message.clear();
 
55
 
 
56
  Execute execute(*(_session.get()), true);
 
57
 
 
58
  vector<string>::iterator iter= sql.begin();
 
59
 
 
60
  while (iter != sql.end())
 
61
  {
 
62
    combined_sql.append(*iter);
 
63
    combined_sql.append("; ");
 
64
    ++iter;
 
65
  }
 
66
 
 
67
  sql::ResultSet result_set(1);
 
68
 
 
69
  /* Execute wraps the SQL to run within a transaction */
 
70
  execute.run(combined_sql, result_set);
 
71
 
 
72
  sql::Exception exception= result_set.getException();
 
73
 
 
74
  drizzled::error_t err= exception.getErrorCode();
 
75
 
 
76
  if ((err != drizzled::EE_OK) && (err != drizzled::ER_EMPTY_QUERY))
 
77
  {
 
78
    /* avoid recursive errors */
 
79
    if (_in_error_state)
 
80
      return true;
 
81
 
 
82
    _in_error_state= true;
 
83
    _error_message= "(SQLSTATE ";
 
84
    _error_message.append(exception.getSQLState());
 
85
    _error_message.append(") ");
 
86
    _error_message.append(exception.getErrorMessage());
 
87
 
 
88
    string bad_sql("Failure while executing:\n");
 
89
    for (size_t y= 0; y < sql.size(); y++)
 
90
    {
 
91
      bad_sql.append(sql[y]);
 
92
      bad_sql.append("\n");
 
93
    }
 
94
 
 
95
    errmsg_printf(error::ERROR, _("%s\n%s\n"),
 
96
                  _error_message.c_str(), bad_sql.c_str());
 
97
    return false;
 
98
  }
 
99
 
 
100
  return true;
 
101
}
 
102
 
 
103
} /* namespace slave */