aboutsummaryrefslogtreecommitdiff
path: root/odb/mssql/error.cxx
blob: 897d4159aeb04fbf055d5115d8752cbcf455a8c0 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// file      : odb/mssql/error.cxx
// license   : ODB NCUEL; see accompanying LICENSE file

#include <string>
#include <cstring> // std::strlen

#include <odb/mssql/mssql.hxx>
#include <odb/mssql/error.hxx>
#include <odb/mssql/connection.hxx>
#include <odb/mssql/exceptions.hxx>

using namespace std;

namespace odb
{
  namespace mssql
  {
    static void
    translate_error (SQLRETURN r,
                     SQLHANDLE h,
                     SQLSMALLINT htype,
                     connection* conn,
                     bool end_tran,
                     size_t pos,
                     multiple_exceptions* mex)
    {
      // First see if we have one of the errors indicated via the
      // return error code.
      //
      switch (r)
      {
      case SQL_STILL_EXECUTING:
        {
          throw database_exception (0, "?????", "statement still executing");
          break;
        }
      case SQL_NEED_DATA:
      case SQL_NO_DATA:
#if ODBCVER >= 0x0380
      case SQL_PARAM_DATA_AVAILABLE:
#endif
        {
          throw database_exception (
            0, "?????", "unhandled SQL_*_DATA condition");
          break;
        }
      case SQL_INVALID_HANDLE:
        {
          throw database_exception (0, "?????", "invalid handle");
          break;
        }
      }

      // Otherwise the diagnostics is stored in the handle.
      //
      char sqlstate[SQL_SQLSTATE_SIZE + 1];
      SQLINTEGER native_code; // Will be 0 if no natve code.
      char msg[512];          // Will be truncated if doesn't fit.
      SQLSMALLINT msg_size;

      // We need to translate certain sqlstate codes to special exceptions,
      // such as deadlock, timeout, etc. The problem is we can have multiple
      // records potentially with different sqlstate codes. If we have both,
      // say, a deadlock code and some other code, then we should probably
      // throw database_exception, which is more severe. To implement this
      // we are going to pre-scan the records looking for the codes we are
      // interested in. If in the process we see any other code, then we
      // stop and go ahead to prepare and throw database_exception.
      //
      enum code
      {
        code_none,
        code_deadlock,
        code_timeout,
        code_connection_lost
      };

      code c (code_none);

      for (SQLSMALLINT i (1);; ++i)
      {
        r = SQLGetDiagRecA (htype,
                            h,
                            i,
                            (SQLCHAR*) sqlstate,
                            &native_code,
                            0,
                            0,
                            &msg_size);

        if (r == SQL_NO_DATA)
          break;
        else if (SQL_SUCCEEDED (r))
        {
          code nc;
          string s (sqlstate);

          if (s == "40001")      // Serialization failure (native code 1205).
            nc = code_deadlock;
          else if (s == "HYT00") // Timeout expired.
            nc = code_timeout;
          else if (s == "HYT01") // Connection timeout expired.
          {
            nc = code_timeout;

            if (conn != 0)
              conn->mark_failed ();
          }
          else if (s == "08S01") // Link failure.
          {
            nc = code_connection_lost;

            if (conn != 0)
              conn->mark_failed ();
          }
          else if (s == "01000") // General warning.
            continue;
          else
          {
            c = code_none;
            break;
          }

          // If a call to SQLEndTran() fails, then the connection is
          // put into the so called "suspended state" and should be
          // disconnected unless we know the transaction was rolled
          // back. See SQLEndTran() documentation for details.
          //
          if (end_tran &&
              s != "25S03" && // Transaction is rolled back.
              s != "40001" && // Serialization failure.
              s != "40002" && // Integrity constraint.
              s != "HYC00")   // Optional feature not implemented.
            conn->mark_failed ();

          if (c != code_none && c != nc)
          {
            // Several different codes.
            //
            c = code_none;
            break;
          }

          c = nc;
        }
        else
        {
          c = code_none;
          break;
        }
      }

      switch (c)
      {
      case code_deadlock:
        throw deadlock ();
      case code_timeout:
        throw timeout ();
      case code_connection_lost:
        throw connection_lost ();
      case code_none:
        break;
      }

      // Some other error code. Prepare database_exception.
      //
      database_exception e;

      for (SQLSMALLINT i (1);; ++i)
      {
        // If this is for a batch, filter out based on row association.
        // Here we only ignore records that have the associated row
        // number and this number doesn't match ours. In particular,
        // this means that all the un-associated records which will be
        // duplicated for all the failed rows, which seems like the
        // correct thing to do.
        //
        if (mex != 0)
        {
          SQLLEN n;
          r = SQLGetDiagField (htype,
                               h,
                               i,
                               SQL_DIAG_ROW_NUMBER,
                               &n,
                               0,
                               0);

          if (r == SQL_NO_DATA)
              break;
          else if (SQL_SUCCEEDED (r) &&
                   n != SQL_NO_ROW_NUMBER &&
                   n != SQL_ROW_NUMBER_UNKNOWN &&
                   n != static_cast<SQLLEN> (pos + 1)) // 1-based
            continue;
        }

        r = SQLGetDiagRecA (htype,
                            h,
                            i,
                            (SQLCHAR*) sqlstate,
                            &native_code,
                            (SQLCHAR*) msg,
                            sizeof (msg),
                            &msg_size);

        if (r == SQL_NO_DATA)
          break;
        else if (SQL_SUCCEEDED (r))
        {
          if (conn != 0)
          {
            string s (sqlstate);

            if (s == "08S01" || // Link failure.
                s == "HYT01" || // Connection timeout.
                (end_tran &&
                 s != "25S03" &&
                 s != "40001" &&
                 s != "40002" &&
                 s != "HYC00"))
              conn->mark_failed ();
          }

          // Get rid of a trailing newline if there is one.
          //
          size_t n (strlen (msg));
          if (n != 0 && msg[n - 1] == '\n')
            msg[n - 1] = '\0';

          e.append (native_code, sqlstate, msg);
        }
        else
          e.append (0, "?????", "unable to extract information for this "
                    "diagnostic record");
      }

      if (e.size () == 0)
        e.append (0, "?????", "no diagnostic record (using wrong handle?)");

      if (mex == 0)
        throw e;
      else
        // It could be that some of these errors are fatal. I guess we
        // will just have to learn from experience which ones are. The
        // client code can always treat specific error codes as fatal.
        //
        mex->insert (pos, e);
    }

    void
    translate_error (SQLRETURN r, connection& c, bool end_tran)
    {
      translate_error (r, c.handle (), SQL_HANDLE_DBC, &c, end_tran, 0, 0);
    }

    void
    translate_error (SQLRETURN r,
                     connection& c,
                     const auto_handle<SQL_HANDLE_STMT>& h,
                     size_t pos,
                     multiple_exceptions* mex)
    {
      translate_error (r, h, SQL_HANDLE_STMT, &c, false, pos, mex);
    }

    void
    translate_error (SQLRETURN r, SQLHANDLE h, SQLSMALLINT htype)
    {
      translate_error (r, h, htype, 0, false, 0, 0);
    }
  }
}