~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Brian Aker
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include "drizzled/statement/execute.h"
#include "drizzled/session.h"
#include "drizzled/user_var_entry.h"
#include "drizzled/plugin/listen.h"
#include "drizzled/plugin/client.h"
#include "drizzled/plugin/null_client.h"
#include "drizzled/plugin/client/concurrent.h"

namespace drizzled
{

void mysql_parse(drizzled::Session *session, const char *inBuf, uint32_t length);

namespace statement
{

Execute::Execute(Session *in_session,
                 drizzled::execute_string_t to_execute_arg,
                 bool is_quiet_arg,
                 bool is_concurrent_arg) :
  Statement(in_session),
  is_quiet(is_quiet_arg),
  is_concurrent(is_concurrent_arg),
  to_execute(to_execute_arg)
{
}
  

bool statement::Execute::parseVariable()
{
  if (to_execute.isVariable())
  {
    user_var_entry *var= getSession()->getVariable(to_execute, false);

    if (var && var->length && var->value && var->type == STRING_RESULT)
    {
      LEX_STRING tmp_for_var;
      tmp_for_var.str= var->value; 
      tmp_for_var.length= var->length; 
      to_execute.set(tmp_for_var);

      return true;
    }
  }

  return false;
}


bool statement::Execute::runStatement(plugin::NullClient *client, const std::string &arg)
{
  client->pushSQL(arg);
  if (not getSession()->executeStatement())
    return true;

  if (getSession()->is_error())
    return true;

  return false;
}


bool statement::Execute::execute()
{
  if (to_execute.length == 0)
  {
    my_error(ER_WRONG_ARGUMENTS, MYF(0), "Invalid Variable");
    return false;
  }
  if (to_execute.isVariable())
  {
    if (not parseVariable())
    {
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "Invalid Variable");
      return false;
    }
  }

  if (is_concurrent)
  {
    if (getSession()->isConcurrentExecuteAllowed())
    {
      plugin::client::Concurrent *client= new plugin::client::Concurrent;
      std::string execution_string(to_execute.str, to_execute.length);
      client->pushSQL(execution_string);
      Session::shared_ptr new_session(new Session(client));

      // We set the current schema.  @todo do the same with catalog
      if (not getSession()->getSchema().empty())
        new_session->set_db(getSession()->getSchema());

      new_session->setConcurrentExecute(false);

      // Overwrite the context in the next session, with what we have in our
      // session. Eventually we will allow someone to change the effective
      // user.
      new_session->getSecurityContext()= getSession()->getSecurityContext();

      if (Session::schedule(new_session))
        Session::unlink(new_session);
    }
    else
    {
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "A Concurrent Execution Session can not launch another session.");
      return false;
    }
  }
  else 
  {
    if (is_quiet)
    {
      plugin::Client *temp= getSession()->getClient();
      plugin::NullClient *null_client= new plugin::NullClient;

      getSession()->setClient(null_client);
      
      bool error_occured= false;
      bool is_savepoint= false;
      {
        std::string start_sql;
        if (getSession()->inTransaction())
        {
          // @todo Figure out something a bit more solid then this.
          start_sql.append("SAVEPOINT execute_internal_savepoint");
          is_savepoint= true;
        }
        else
        {
          start_sql.append("START TRANSACTION");
        }

        error_occured= runStatement(null_client, start_sql);
      }

      // @note this is copied from code in NULL client, all of this belongs
      // in the pluggable parser pieces.  
      if (not error_occured)
      {
        typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
        std::string full_string(to_execute.str, to_execute.length);
        Tokenizer tok(full_string, boost::escaped_list_separator<char>("\\", ";", "\""));

        for (Tokenizer::iterator iter= tok.begin();
             iter != tok.end() and getSession()->getKilled() != Session::KILL_CONNECTION;
             ++iter)
        {
          if (runStatement(null_client, *iter))
          {
            error_occured= true;
            break;
          }
        }

        // @todo Encapsulate logic later to method
        {
          std::string final_sql;
          if (is_savepoint)
          {
            if (error_occured)
            {
              final_sql.append("ROLLBACK TO SAVEPOINT execute_internal_savepoint");
            }
            else
            {
              final_sql.append("RELEASE SAVEPOINT execute_internal_savepoint");
            }
          }
          else
          {
            if (error_occured)
            {
              final_sql.append("ROLLBACK");
            }
            else
            {
              final_sql.append("COMMIT");
            }
          }

          // Run the cleanup command, we currently ignore if an error occurs
          // here.
          (void)runStatement(null_client, final_sql);
        }
      }

      getSession()->setClient(temp);
      if (getSession()->is_error())
      {
        getSession()->clear_error(true);
      }
      else
      {
        getSession()->clearDiagnostics();
      }

      getSession()->my_ok();

      null_client->close();
      delete null_client;
    }
    else
    {
      mysql_parse(getSession(), to_execute.str, to_execute.length);
    }
  }


  // We have to restore ourselves at the top for delete() to work.
  getSession()->getLex()->statement= this;

  return true;
}

} /* namespace statement */
} /* namespace drizzled */