~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
/* - 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/execute.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>
#include <drizzled/sql_lex.h>

namespace drizzled {

void parse(drizzled::Session&, const char *inBuf, uint32_t length);

namespace statement {

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

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

    if (var && var->length && var->value && var->type == STRING_RESULT)
    {
      lex_string_t 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 session().executeStatement())
    return true;

  if (session().is_error())
    return true;

  return false;
}


bool statement::Execute::execute()
{
  bool ret= execute_shell();

  // We have to restore ourselves at the top for delete() to work.
  lex().statement= this;

  return ret;
}


bool statement::Execute::execute_shell()
{
  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 (not session().isConcurrentExecuteAllowed())
    {
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "A Concurrent Execution Session can not launch another session.");
      return false;
    }

    drizzled::Execute executer(session(), should_wait);
    executer.run(to_execute);
  }
  else // Non-concurrent run.
  {
    if (is_quiet)
    {
      plugin::Client *temp= session().getClient();
      plugin::NullClient *null_client= new plugin::NullClient;

      session().setClient(null_client);
      
      bool error_occured= false;
      bool is_savepoint= false;
      {
        std::string start_sql;
        if (session().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 session().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)
          {
            final_sql.append(error_occured ? 
              "ROLLBACK TO SAVEPOINT execute_internal_savepoint" : 
              "RELEASE SAVEPOINT execute_internal_savepoint");
          }
          else
          {
            final_sql.append(error_occured ? "ROLLBACK" : "COMMIT");
          }

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

      session().setClient(temp);
      if (session().is_error())
      {
        session().clear_error(true);
      }
      else
      {
        session().clearDiagnostics();
      }

      session().my_ok();

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

  return true;
}

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