~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/statement/rollback_to_savepoint.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-10-02 02:15:04 UTC
  • mto: This revision was merged to the branch mainline in revision 1167.
  • Revision ID: osullivan.padraig@gmail.com-20091002021504-6zho7rfkdhn0g1jy
Added a test suite for the memcached stats I_S tables. The test suite is
pretty simple at the moment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2009 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
22
 
#include "drizzled/show.h"
23
 
#include "drizzled/session.h"
24
 
#include "drizzled/statement/rollback_to_savepoint.h"
25
 
#include "drizzled/transaction_services.h"
26
 
#include "drizzled/named_savepoint.h"
27
 
#include "drizzled/util/functors.h"
28
 
 
29
 
#include <string>
30
 
 
31
 
using namespace std;
32
 
 
33
 
namespace drizzled
34
 
{
 
21
#include <drizzled/server_includes.h>
 
22
#include <drizzled/show.h>
 
23
#include <drizzled/session.h>
 
24
#include <drizzled/statement/rollback_to_savepoint.h>
 
25
 
 
26
using namespace drizzled;
35
27
 
36
28
bool statement::RollbackToSavepoint::execute()
37
29
{
38
 
  /*
39
 
   * If AUTOCOMMIT is off and resource contexts are empty then we need
40
 
   * to start a transaction. It will be empty when ROLLBACK TO SAVEPOINT
41
 
   * starts the transaction. Table affecting statements do this work in
42
 
   * lockTables() by calling startStatement().
43
 
   */
44
 
  if ( (session->options & OPTION_NOT_AUTOCOMMIT) &&
45
 
       (session->transaction.all.getResourceContexts().empty() == true) )
46
 
  {
47
 
    if (session->startTransaction() == false)
48
 
    {
49
 
      return false;
50
 
    }
51
 
  }
52
 
 
53
 
  /*
54
 
   * Handle these situations:
55
 
   *
56
 
   * If the first savepoint on the deck matches the
57
 
   * name to find, simply call rollback_to_savepoint
58
 
   * for the resource managers.
59
 
   *
60
 
   * If the first savepoint on the deck does NOT match
61
 
   * the name to find, then we need to search through
62
 
   * the deque to find the savepoint we need.  If we
63
 
   * don't find it, we return an error.  If we do
64
 
   * find it, we must restructure the deque by removing
65
 
   * all savepoints "above" the one we find.
66
 
   */
67
 
  deque<NamedSavepoint> &savepoints= session->transaction.savepoints;
68
 
  TransactionServices &transaction_services= TransactionServices::singleton();
69
 
 
70
 
  /* Short-circuit for no savepoints */
71
 
  if (savepoints.empty())
72
 
  {
73
 
    my_error(ER_SP_DOES_NOT_EXIST, 
74
 
             MYF(0), 
75
 
             "SAVEPOINT", 
76
 
             session->lex->ident.str);
77
 
    return false;
78
 
  }
79
 
 
80
 
  {
81
 
    /* Short-circuit for first savepoint */
82
 
    NamedSavepoint &first_savepoint= savepoints.front();
83
 
    const string &first_savepoint_name= first_savepoint.getName();
 
30
  SAVEPOINT *sv;
 
31
  for (sv= session->transaction.savepoints; sv; sv= sv->prev)
 
32
  {
84
33
    if (my_strnncoll(system_charset_info,
85
34
                     (unsigned char *) session->lex->ident.str, 
86
35
                     session->lex->ident.length,
87
 
                     (unsigned char *) first_savepoint_name.c_str(), 
88
 
                     first_savepoint_name.size()) == 0)
89
 
    {
90
 
      /* Found the named savepoint we want to rollback to */
91
 
      (void) transaction_services.rollbackToSavepoint(session, first_savepoint);
92
 
 
93
 
      if (session->transaction.all.hasModifiedNonTransData())
 
36
                     (unsigned char *)sv->name, 
 
37
                     sv->length) == 0)
 
38
    {
 
39
      break;
 
40
    }
 
41
  }
 
42
  if (sv)
 
43
  {
 
44
    if (ha_rollback_to_savepoint(session, sv))
 
45
    {
 
46
      return true; /* cannot happen */
 
47
    }
 
48
    else
 
49
    {
 
50
      if ((session->options & OPTION_KEEP_LOG) || 
 
51
          session->transaction.all.modified_non_trans_table)
94
52
      {
95
53
        push_warning(session, 
96
54
                     DRIZZLE_ERROR::WARN_LEVEL_WARN,
98
56
                     ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
99
57
      }
100
58
      session->my_ok();
101
 
      return false;
102
 
    }
103
 
  }
104
 
 
105
 
  /* 
106
 
   * OK, from here on out it means that we have savepoints
107
 
   * but that the first savepoint isn't the one we're looking
108
 
   * for.  We need to search through the savepoints and find
109
 
   * the one we're looking for, removing all savepoints "above"
110
 
   * the one we need.
111
 
   */
112
 
  bool found= false;
113
 
  deque<NamedSavepoint> copy_savepoints(savepoints); /* used to restore if not found */
114
 
  deque<NamedSavepoint> new_savepoints;
115
 
  while (savepoints.empty() == false)
116
 
  {
117
 
    NamedSavepoint &sv= savepoints.front();
118
 
    const string &sv_name= sv.getName();
119
 
    if (! found && 
120
 
        my_strnncoll(system_charset_info,
121
 
                     (unsigned char *) session->lex->ident.str, 
122
 
                     session->lex->ident.length,
123
 
                     (unsigned char *) sv_name.c_str(), 
124
 
                     sv_name.size()) == 0)
125
 
    {
126
 
      /* Found the named savepoint we want to rollback to */
127
 
      found= true;
128
 
 
129
 
      (void) transaction_services.rollbackToSavepoint(session, sv);
130
 
    }
131
 
    if (found)
132
 
    {
133
 
      /* 
134
 
       * We push all savepoints "below" the found savepoint
135
 
       * to our new savepoint list, in reverse order to keep
136
 
       * the stack order correct. 
137
 
       */
138
 
      new_savepoints.push_back(sv);
139
 
    }
140
 
    savepoints.pop_front();
141
 
  }
142
 
  if (found)
143
 
  {
144
 
    if (session->transaction.all.hasModifiedNonTransData())
145
 
    {
146
 
      push_warning(session, 
147
 
                   DRIZZLE_ERROR::WARN_LEVEL_WARN,
148
 
                   ER_WARNING_NOT_COMPLETE_ROLLBACK,
149
 
                   ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
150
 
    }
151
 
    /* Store new savepoints list */
152
 
    session->transaction.savepoints= new_savepoints;
153
 
    session->my_ok();
 
59
    }
 
60
    session->transaction.savepoints= sv;
154
61
  }
155
62
  else
156
63
  {
157
 
    /* restore the original savepoint list */
158
 
    session->transaction.savepoints= copy_savepoints;
159
64
    my_error(ER_SP_DOES_NOT_EXIST, 
160
65
             MYF(0), 
161
66
             "SAVEPOINT", 
163
68
  }
164
69
  return false;
165
70
}
166
 
 
167
 
} /* namespace drizzled */