aboutsummaryrefslogtreecommitdiff
path: root/odb/exceptions.cxx
blob: 3ce144952feb2cdeabbe257b568b3625a7dc350b (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// file      : odb/exceptions.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <cstring> // std::strlen
#include <sstream>
#include <cassert>

#include <odb/exceptions.hxx>

using namespace std;

namespace odb
{
  const char* null_pointer::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "NULL pointer";
  }

  null_pointer* null_pointer::
  clone () const
  {
    return new null_pointer (*this);
  }

  const char* already_in_transaction::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "transaction already in progress in this thread";
  }

  already_in_transaction* already_in_transaction::
  clone () const
  {
    return new already_in_transaction (*this);
  }

  const char* not_in_transaction::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "operation can only be performed in transaction";
  }

  not_in_transaction* not_in_transaction::
  clone () const
  {
    return new not_in_transaction (*this);
  }

  const char* transaction_already_finalized::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "transaction already committed or rolled back";
  }

  transaction_already_finalized* transaction_already_finalized::
  clone () const
  {
    return new transaction_already_finalized (*this);
  }

  const char* already_in_session::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "session already in effect in this thread";
  }

  already_in_session* already_in_session::
  clone () const
  {
    return new already_in_session (*this);
  }

  const char* not_in_session::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "session not in effect in this thread";
  }

  not_in_session* not_in_session::
  clone () const
  {
    return new not_in_session (*this);
  }

  const char* session_required::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "session required to load this object relationship";
  }

  session_required* session_required::
  clone () const
  {
    return new session_required (*this);
  }

  const char* deadlock::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "transaction aborted due to deadlock";
  }

  deadlock* deadlock::
  clone () const
  {
    return new deadlock (*this);
  }

  const char* connection_lost::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "connection to database lost";
  }

  connection_lost* connection_lost::
  clone () const
  {
    return new connection_lost (*this);
  }

  const char* timeout::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "database operation timeout";
  }

  timeout* timeout::
  clone () const
  {
    return new timeout (*this);
  }

  const char* object_not_persistent::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "object not persistent";
  }

  object_not_persistent* object_not_persistent::
  clone () const
  {
    return new object_not_persistent (*this);
  }

  const char* object_already_persistent::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "object already persistent";
  }

  object_already_persistent* object_already_persistent::
  clone () const
  {
    return new object_already_persistent (*this);
  }

  const char* object_changed::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "object changed concurrently";
  }

  object_changed* object_changed::
  clone () const
  {
    return new object_changed (*this);
  }

  const char* result_not_cached::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "query result is not cached";
  }

  result_not_cached* result_not_cached::
  clone () const
  {
    return new result_not_cached (*this);
  }

  const char* abstract_class::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "database operation on instance of abstract class";
  }

  abstract_class* abstract_class::
  clone () const
  {
    return new abstract_class (*this);
  }

  const char* no_type_info::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "no type information";
  }

  no_type_info* no_type_info::
  clone () const
  {
    return new no_type_info (*this);
  }

  prepared_already_cached::
  prepared_already_cached (const char* name)
      : name_ (name)
  {
    what_ = "prepared query '";
    what_ += name;
    what_ += "' is already cached";
  }

  prepared_already_cached::
  ~prepared_already_cached () ODB_NOTHROW_NOEXCEPT
  {
  }

  const char* prepared_already_cached::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return what_.c_str ();
  }

  prepared_already_cached* prepared_already_cached::
  clone () const
  {
    return new prepared_already_cached (*this);
  }

  prepared_type_mismatch::
  prepared_type_mismatch (const char* name)
      : name_ (name)
  {
    what_ = "type mismatch while looking up prepared query '";
    what_ += name;
    what_ += "'";
  }

  prepared_type_mismatch::
  ~prepared_type_mismatch () ODB_NOTHROW_NOEXCEPT
  {
  }

  const char* prepared_type_mismatch::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return what_.c_str ();
  }

  prepared_type_mismatch* prepared_type_mismatch::
  clone () const
  {
    return new prepared_type_mismatch (*this);
  }

  unknown_schema::
  unknown_schema (const string& name)
      : name_ (name)
  {
    what_ = "unknown database schema '";
    what_ += name;
    what_ += "'";
  }

  unknown_schema::
  ~unknown_schema () ODB_NOTHROW_NOEXCEPT
  {
  }

  const char* unknown_schema::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return what_.c_str ();
  }

  unknown_schema* unknown_schema::
  clone () const
  {
    return new unknown_schema (*this);
  }

  unknown_schema_version::
  unknown_schema_version (schema_version v)
      : version_ (v)
  {
    ostringstream os;
    os << v;
    what_ = "unknown database schema version ";
    what_ += os.str ();
  }

  unknown_schema_version::
  ~unknown_schema_version () ODB_NOTHROW_NOEXCEPT
  {
  }

  const char* unknown_schema_version::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return what_.c_str ();
  }

  unknown_schema_version* unknown_schema_version::
  clone () const
  {
    return new unknown_schema_version (*this);
  }

  const char* section_not_loaded::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "section is not loaded";
  }

  section_not_loaded* section_not_loaded::
  clone () const
  {
    return new section_not_loaded (*this);
  }

  const char* section_not_in_object::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return "section instance is not part of an object (section was copied?)";
  }

  section_not_in_object* section_not_in_object::
  clone () const
  {
    return new section_not_in_object (*this);
  }

  // multiple_exceptions
  //
  multiple_exceptions::
  ~multiple_exceptions () ODB_NOTHROW_NOEXCEPT {}

  void multiple_exceptions::
  insert (size_t p, bool maybe, const odb::exception& e, bool fatal)
  {
    details::shared_ptr<odb::exception> pe;

    if (common_exception_ti_ != typeid (e))
      pe.reset (e.clone ());
    else
    {
      if (common_exception_ == 0)
        common_exception_.reset (e.clone ());

      pe = common_exception_;
    }

    set_.insert (value_type (delta_ + p, maybe, pe));
    fatal_ = fatal_ || fatal;
  }

  const multiple_exceptions::value_type* multiple_exceptions::
  lookup (size_t p) const
  {
    p += delta_; // Called while populating multiple_exceptions.

    iterator i (set_.find (value_type (p)));
    return i == set_.end () ? 0 : &*i;
  }

  void multiple_exceptions::
  prepare ()
  {
    current_ = 0;
    delta_ = 0;
    common_exception_.reset ();

    ostringstream os;
    os << "multiple exceptions, "
       << attempted_ << " element" << (attempted_ != 1 ? "s" : "") <<
      " attempted, "
       << failed () << " failed"
       << (fatal_ ? ", fatal" : "") << ":";

    for (iterator i (begin ()); i != end ();)
    {
      size_t p (i->position ());
      const odb::exception& e (i->exception ());

      os << '\n';

      if (!i->maybe ())
      {
        os << '[' << p << ']';
        ++i;
      }
      else
      {
        // In this case we will normally have a large number of maybe
        // failures in a row (usually the whole batch). So let's try
        // to represent them all as a single range.
        //
        size_t n (0);
        for (++i; i != end () && i->maybe (); ++i)
        {
          assert (&e == &i->exception ()); // The same common exception.
          n++;
        }

        if (n == 0)
          os << '[' << p << ']';
        else
          os << '[' << p << '-' << (p + n) << "] (some)";
      }

      os << ' ' << e.what ();
    }

    what_ = os.str ();
  }

  const char* multiple_exceptions::
  what () const ODB_NOTHROW_NOEXCEPT
  {
    return what_.c_str ();
  }

  multiple_exceptions* multiple_exceptions::
  clone () const
  {
    return new multiple_exceptions (*this);
  }
}