~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/execute.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:28:23 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103032823-44k21f0njmk97omr
fix docs warning: Title underline (and overline) is too short in brief_history_of_drizzle.rst

Show diffs side-by-side

added added

removed removed

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