~drizzle-trunk/drizzle/development

1976.7.2 by Brian Aker
Adding execute command to go along with main command.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
22
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/session.h>
24
#include <drizzled/user_var_entry.h>
2116.1.41 by David Shrewsbury
Merge trunk and resolve conflicts
25
#include <drizzled/plugin/client/cached.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
26
#include <drizzled/plugin/client/concurrent.h>
27
#include <drizzled/catalog/local.h>
28
#include <drizzled/execute.h>
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
29
30
namespace drizzled
31
{
32
33
Execute::Execute(Session &arg, bool wait_arg) :
34
  wait(wait_arg),
35
  _session(arg)
36
{
37
}
38
39
Execute::~Execute()
40
{
41
}
42
43
void Execute::run(const char *arg, size_t length)
44
{
45
  std::string execution_string(arg, length);
46
  run(execution_string);
47
}
48
2116.1.17 by David Shrewsbury
Various fixes
49
void Execute::run(std::string &execution_string, sql::ResultSet &result_set)
2116.1.13 by David Shrewsbury
Initial work to access result set.
50
{
51
  boost_thread_shared_ptr thread;
52
  
53
  if (_session.isConcurrentExecuteAllowed())
54
  {
55
    plugin::client::Cached *client= new plugin::client::Cached(result_set);
56
    client->pushSQL(execution_string);
57
    Session::shared_ptr new_session= Session::make_shared(client, catalog::local());
58
    
59
    // We set the current schema.  @todo do the same with catalog
60
    util::string::const_shared_ptr schema(_session.schema());
61
    if (not schema->empty())
62
      new_session->set_db(*schema);
63
    
64
    new_session->setConcurrentExecute(false);
65
    
66
    // Overwrite the context in the next session, with what we have in our
67
    // session. Eventually we will allow someone to change the effective
68
    // user.
69
    new_session->user()= _session.user();
70
    
71
    if (Session::schedule(new_session))
72
    {
73
      Session::unlink(new_session);
74
    }
75
    else if (wait)
76
    {
77
      thread= new_session->getThread();
78
    }
79
  }
80
  else
81
  {
82
    my_error(ER_WRONG_ARGUMENTS, MYF(0), "A Concurrent Execution Session can not launch another session.");
83
    return;
84
  }
85
  
86
  if (wait && thread && thread->joinable())
87
  {
88
    // We want to make sure that we can be killed
89
    if (_session.getThread())
90
    {
91
      boost::this_thread::restore_interruption dl(_session.getThreadInterupt());
92
      
93
      try {
94
        thread->join();
95
      }
96
      catch(boost::thread_interrupted const&)
97
      {
98
        // Just surpress and return the error
99
        my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
100
        return;
101
      }
102
    }
103
    else
104
    {
105
      thread->join();
106
    }
107
  }
108
}
109
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
110
void Execute::run(std::string &execution_string)
111
{
112
  boost_thread_shared_ptr thread;
113
114
  if (_session.isConcurrentExecuteAllowed())
115
  {
2116.1.13 by David Shrewsbury
Initial work to access result set.
116
    plugin::client::Concurrent *client= new plugin::client::Concurrent;
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
117
    client->pushSQL(execution_string);
2039.6.3 by Brian Aker
Update for session to have a catalog object.
118
    Session::shared_ptr new_session= Session::make_shared(client, catalog::local());
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
119
120
    // We set the current schema.  @todo do the same with catalog
121
    util::string::const_shared_ptr schema(_session.schema());
122
    if (not schema->empty())
123
      new_session->set_db(*schema);
124
125
    new_session->setConcurrentExecute(false);
126
127
    // Overwrite the context in the next session, with what we have in our
128
    // session. Eventually we will allow someone to change the effective
129
    // user.
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
130
    new_session->user()= _session.user();
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
131
132
    if (Session::schedule(new_session))
133
    {
134
      Session::unlink(new_session);
135
    }
136
    else if (wait)
137
    {
138
      thread= new_session->getThread();
139
    }
140
  }
141
  else
142
  {
143
    my_error(ER_WRONG_ARGUMENTS, MYF(0), "A Concurrent Execution Session can not launch another session.");
144
    return;
145
  }
146
147
  if (wait && thread && thread->joinable())
148
  {
149
    // We want to make sure that we can be killed
2079.4.1 by Brian Aker
Merge in code to all plugins to do whatever they need to do once all other
150
    if (_session.getThread())
151
    {
152
      boost::this_thread::restore_interruption dl(_session.getThreadInterupt());
153
154
      try {
155
        thread->join();
156
      }
157
      catch(boost::thread_interrupted const&)
158
      {
159
        // Just surpress and return the error
160
        my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
161
        return;
162
      }
163
    }
164
    else
165
    {
1976.7.2 by Brian Aker
Adding execute command to go along with main command.
166
      thread->join();
167
    }
168
  }
169
}
170
171
} /* namespace drizzled */