~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/execute.cc

  • Committer: Lee Bieber
  • Date: 2010-11-14 05:18:07 UTC
  • mfrom: (1921.4.12 catalogs)
  • Revision ID: kalebral@gmail.com-20101114051807-p69h40jbsn1byf84
Merge Brian - add execute with no return

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "drizzled/statement/execute.h"
24
24
#include "drizzled/session.h"
25
25
#include "drizzled/user_var_entry.h"
 
26
#include "drizzled/plugin/listen.h"
 
27
#include "drizzled/plugin/client.h"
 
28
#include "drizzled/plugin/null_client.h"
 
29
#include "drizzled/plugin/client/concurrent.h"
26
30
 
27
31
namespace drizzled
28
32
{
32
36
namespace statement
33
37
{
34
38
 
35
 
Execute::Execute(Session *in_session) :
 
39
Execute::Execute(Session *in_session,
 
40
                 drizzled::execute_string_t to_execute_arg,
 
41
                 bool is_quiet_arg,
 
42
                 bool is_concurrent_arg) :
36
43
  Statement(in_session),
37
 
  is_var(false)
 
44
  is_quiet(is_quiet_arg),
 
45
  is_concurrent(is_concurrent_arg),
 
46
  to_execute(to_execute_arg)
38
47
{
39
48
}
40
49
  
41
50
 
42
51
bool statement::Execute::parseVariable()
43
52
{
44
 
  if (is_var)
 
53
  if (to_execute.isVariable())
45
54
  {
46
55
    user_var_entry *var= getSession()->getVariable(to_execute, false);
47
56
 
50
59
      LEX_STRING tmp_for_var;
51
60
      tmp_for_var.str= var->value; 
52
61
      tmp_for_var.length= var->length; 
53
 
      to_execute= tmp_for_var;
 
62
      to_execute.set(tmp_for_var);
 
63
 
54
64
      return true;
55
65
    }
56
66
  }
58
68
  return false;
59
69
}
60
70
 
 
71
 
 
72
bool statement::Execute::runStatement(plugin::NullClient *client, const std::string &arg)
 
73
{
 
74
  client->pushSQL(arg);
 
75
  if (not getSession()->executeStatement())
 
76
    return true;
 
77
 
 
78
  if (getSession()->is_error())
 
79
    return true;
 
80
 
 
81
  return false;
 
82
}
 
83
 
 
84
 
61
85
bool statement::Execute::execute()
62
86
{
63
87
  if (to_execute.length == 0)
65
89
    my_error(ER_WRONG_ARGUMENTS, MYF(0), "Invalid Variable");
66
90
    return false;
67
91
  }
68
 
  if (is_var)
 
92
  if (to_execute.isVariable())
69
93
  {
70
94
    if (not parseVariable())
71
95
    {
74
98
    }
75
99
  }
76
100
 
77
 
  mysql_parse(getSession(), to_execute.str, to_execute.length);
78
 
 
79
 
  // We have to restore ourselves at the top for delete[] to work.
 
101
  if (is_concurrent)
 
102
  {
 
103
    if (getSession()->isConcurrentExecuteAllowed())
 
104
    {
 
105
      plugin::client::Concurrent *client= new plugin::client::Concurrent;
 
106
      std::string execution_string(to_execute.str, to_execute.length);
 
107
      client->pushSQL(execution_string);
 
108
      Session *new_session= new Session(client);
 
109
 
 
110
      // We set the current schema.  @todo do the same with catalog
 
111
      if (not getSession()->getSchema().empty())
 
112
        new_session->set_db(getSession()->getSchema());
 
113
 
 
114
      new_session->setConcurrentExecute(false);
 
115
 
 
116
      // Overwrite the context in the next session, with what we have in our
 
117
      // session. Eventually we will allow someone to change the effective
 
118
      // user.
 
119
      new_session->getSecurityContext()= getSession()->getSecurityContext();
 
120
 
 
121
      if (new_session->schedule())
 
122
        Session::unlink(new_session);
 
123
    }
 
124
    else
 
125
    {
 
126
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "A Concurrent Execution Session can not launch another session.");
 
127
      return false;
 
128
    }
 
129
  }
 
130
  else 
 
131
  {
 
132
    if (is_quiet)
 
133
    {
 
134
      plugin::Client *temp= getSession()->getClient();
 
135
      plugin::NullClient *null_client= new plugin::NullClient;
 
136
 
 
137
      getSession()->setClient(null_client);
 
138
      
 
139
      bool error_occured= false;
 
140
      bool is_savepoint= false;
 
141
      {
 
142
        std::string start_sql;
 
143
        if (getSession()->inTransaction())
 
144
        {
 
145
          // @todo Figure out something a bit more solid then this.
 
146
          start_sql.append("SAVEPOINT execute_internal_savepoint");
 
147
          is_savepoint= true;
 
148
        }
 
149
        else
 
150
        {
 
151
          start_sql.append("START TRANSACTION");
 
152
        }
 
153
 
 
154
        error_occured= runStatement(null_client, start_sql);
 
155
      }
 
156
 
 
157
      // @note this is copied from code in NULL client, all of this belongs
 
158
      // in the pluggable parser pieces.  
 
159
      if (not error_occured)
 
160
      {
 
161
        typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
 
162
        std::string full_string(to_execute.str, to_execute.length);
 
163
        Tokenizer tok(full_string, boost::escaped_list_separator<char>("\\", ";", "\""));
 
164
 
 
165
        for (Tokenizer::iterator iter= tok.begin();
 
166
             iter != tok.end() and getSession()->getKilled() != Session::KILL_CONNECTION;
 
167
             ++iter)
 
168
        {
 
169
          if (runStatement(null_client, *iter))
 
170
          {
 
171
            error_occured= true;
 
172
            break;
 
173
          }
 
174
        }
 
175
 
 
176
        // @todo Encapsulate logic later to method
 
177
        {
 
178
          std::string final_sql;
 
179
          if (is_savepoint)
 
180
          {
 
181
            if (error_occured)
 
182
            {
 
183
              final_sql.append("ROLLBACK TO SAVEPOINT execute_internal_savepoint");
 
184
            }
 
185
            else
 
186
            {
 
187
              final_sql.append("RELEASE SAVEPOINT execute_internal_savepoint");
 
188
            }
 
189
          }
 
190
          else
 
191
          {
 
192
            if (error_occured)
 
193
            {
 
194
              final_sql.append("ROLLBACK");
 
195
            }
 
196
            else
 
197
            {
 
198
              final_sql.append("COMMIT");
 
199
            }
 
200
          }
 
201
 
 
202
          // Run the cleanup command, we currently ignore if an error occurs
 
203
          // here.
 
204
          (void)runStatement(null_client, final_sql);
 
205
        }
 
206
      }
 
207
 
 
208
      getSession()->setClient(temp);
 
209
      if (getSession()->is_error())
 
210
      {
 
211
        getSession()->clear_error(true);
 
212
      }
 
213
      else
 
214
      {
 
215
        getSession()->clearDiagnostics();
 
216
      }
 
217
 
 
218
      getSession()->my_ok();
 
219
 
 
220
      null_client->close();
 
221
      delete null_client;
 
222
    }
 
223
    else
 
224
    {
 
225
      mysql_parse(getSession(), to_execute.str, to_execute.length);
 
226
    }
 
227
  }
 
228
 
 
229
 
 
230
  // We have to restore ourselves at the top for delete() to work.
80
231
  getSession()->getLex()->statement= this;
81
232
 
82
233
  return true;