~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/diagnostics_area.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
 
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; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "drizzled/session.h"
 
22
#include "drizzled/diagnostics_area.h"
 
23
 
 
24
/**
 
25
  Clear this diagnostics area.
 
26
 
 
27
  Normally called at the end of a statement.
 
28
*/
 
29
void Diagnostics_area::reset_diagnostics_area()
 
30
{
 
31
  can_overwrite_status= false;
 
32
  /** Don't take chances in production */
 
33
  m_message[0]= '\0';
 
34
  m_sql_errno= 0;
 
35
  m_server_status= 0;
 
36
  m_affected_rows= 0;
 
37
  m_found_rows= 0;
 
38
  m_last_insert_id= 0;
 
39
  m_total_warn_count= 0;
 
40
  is_sent= false;
 
41
  /** Tiny reset in debug mode to see garbage right away */
 
42
  m_status= DA_EMPTY;
 
43
}
 
44
 
 
45
const char *Diagnostics_area::message() const
 
46
{
 
47
  assert(m_status == DA_ERROR || m_status == DA_OK);
 
48
  return m_message;
 
49
}
 
50
 
 
51
 
 
52
uint32_t Diagnostics_area::sql_errno() const
 
53
{
 
54
  assert(m_status == DA_ERROR);
 
55
  return m_sql_errno;
 
56
}
 
57
 
 
58
uint32_t Diagnostics_area::server_status() const
 
59
{
 
60
  assert(m_status == DA_OK || m_status == DA_EOF);
 
61
  return m_server_status;
 
62
}
 
63
 
 
64
ha_rows Diagnostics_area::affected_rows() const
 
65
{ assert(m_status == DA_OK); return m_affected_rows; }
 
66
 
 
67
ha_rows Diagnostics_area::found_rows() const
 
68
{ assert(m_status == DA_OK); return m_found_rows; }
 
69
 
 
70
uint64_t Diagnostics_area::last_insert_id() const
 
71
{ assert(m_status == DA_OK); return m_last_insert_id; }
 
72
 
 
73
uint32_t Diagnostics_area::total_warn_count() const
 
74
{
 
75
  assert(m_status == DA_OK || m_status == DA_EOF);
 
76
  return m_total_warn_count;
 
77
}
 
78
 
 
79
/**
 
80
  Set OK status -- ends commands that do not return a
 
81
  result set, e.g. INSERT/UPDATE/DELETE.
 
82
*/
 
83
void Diagnostics_area::set_ok_status(Session *session,
 
84
                                     ha_rows affected_rows_arg,
 
85
                                     ha_rows found_rows_arg,
 
86
                                     uint64_t last_insert_id_arg,
 
87
                                     const char *message_arg)
 
88
{
 
89
  assert(! is_set());
 
90
  /*
 
91
    In production, refuse to overwrite an error or a custom response
 
92
    with an OK packet.
 
93
  */
 
94
  if (is_error() || is_disabled())
 
95
    return;
 
96
  /** Only allowed to report success if has not yet reported an error */
 
97
 
 
98
  m_server_status= session->server_status;
 
99
  m_total_warn_count= session->total_warn_count;
 
100
  m_affected_rows= affected_rows_arg;
 
101
  m_found_rows= found_rows_arg;
 
102
  m_last_insert_id= last_insert_id_arg;
 
103
  if (message_arg)
 
104
    strncpy(m_message, message_arg, sizeof(m_message) - 1);
 
105
  else
 
106
    m_message[0]= '\0';
 
107
  m_status= DA_OK;
 
108
}
 
109
 
 
110
/**
 
111
  Set EOF status.
 
112
*/
 
113
void Diagnostics_area::set_eof_status(Session *session)
 
114
{
 
115
  /** Only allowed to report eof if has not yet reported an error */
 
116
 
 
117
  assert(! is_set());
 
118
  /*
 
119
    In production, refuse to overwrite an error or a custom response
 
120
    with an EOF packet.
 
121
  */
 
122
  if (is_error() || is_disabled())
 
123
    return;
 
124
 
 
125
  m_server_status= session->server_status;
 
126
  /*
 
127
    If inside a stored procedure, do not return the total
 
128
    number of warnings, since they are not available to the client
 
129
    anyway.
 
130
  */
 
131
  m_total_warn_count= session->total_warn_count;
 
132
 
 
133
  m_status= DA_EOF;
 
134
}
 
135
 
 
136
/**
 
137
  Set ERROR status.
 
138
*/
 
139
void Diagnostics_area::set_error_status(uint32_t sql_errno_arg,
 
140
                                   const char *message_arg)
 
141
{
 
142
  /*
 
143
    Only allowed to report error if has not yet reported a success
 
144
    The only exception is when we flush the message to the client,
 
145
    an error can happen during the flush.
 
146
  */
 
147
  assert(! is_set() || can_overwrite_status);
 
148
  /*
 
149
    In production, refuse to overwrite a custom response with an
 
150
    ERROR packet.
 
151
  */
 
152
  if (is_disabled())
 
153
    return;
 
154
 
 
155
  m_sql_errno= sql_errno_arg;
 
156
  strncpy(m_message, message_arg, sizeof(m_message) - 1);
 
157
 
 
158
  m_status= DA_ERROR;
 
159
}
 
160
 
 
161
/**
 
162
  Mark the diagnostics area as 'DISABLED'.
 
163
 
 
164
  This is used in rare cases when the COM_ command at hand sends a response
 
165
  in a custom format. One example is the query cache, another is
 
166
  COM_STMT_PREPARE.
 
167
*/
 
168
void Diagnostics_area::disable_status()
 
169
{
 
170
  assert(! is_set());
 
171
  m_status= DA_DISABLED;
 
172
}