aboutsummaryrefslogtreecommitdiff
path: root/odb/exceptions.hxx
blob: 531f8c0ea8c67c1bd23b7ebf22500b51978592e9 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// file      : odb/exceptions.hxx
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_EXCEPTIONS_HXX
#define ODB_EXCEPTIONS_HXX

#include <odb/pre.hxx>

#include <set>
#include <string>
#include <cstddef>  // std::size_t
#include <typeinfo>

#include <odb/forward.hxx>    // schema_version, odb::core
#include <odb/exception.hxx>

#include <odb/details/config.hxx> // ODB_NOTHROW_NOEXCEPT
#include <odb/details/export.hxx>
#include <odb/details/shared-ptr.hxx>

namespace odb
{
  struct LIBODB_EXPORT null_pointer: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual null_pointer*
    clone () const;
  };

  // Transaction exceptions.
  //
  struct LIBODB_EXPORT already_in_transaction: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual already_in_transaction*
    clone () const;
  };

  struct LIBODB_EXPORT not_in_transaction: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual not_in_transaction*
    clone () const;
  };

  struct LIBODB_EXPORT transaction_already_finalized: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual transaction_already_finalized*
    clone () const;
  };

  // Session exceptions.
  //
  struct LIBODB_EXPORT already_in_session: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual already_in_session*
    clone () const;
  };

  struct LIBODB_EXPORT not_in_session: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual not_in_session*
    clone () const;
  };

  struct LIBODB_EXPORT session_required: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual session_required*
    clone () const;
  };

  // Database operations exceptions.
  //
  struct LIBODB_EXPORT recoverable: odb::exception
  {
    // Abstract.
  };

  struct LIBODB_EXPORT connection_lost: recoverable
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual connection_lost*
    clone () const;
  };

  struct LIBODB_EXPORT timeout: recoverable
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual timeout*
    clone () const;
  };

  struct LIBODB_EXPORT deadlock: recoverable
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual deadlock*
    clone () const;
  };

  struct LIBODB_EXPORT object_not_persistent: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual object_not_persistent*
    clone () const;
  };

  struct LIBODB_EXPORT object_already_persistent: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual object_already_persistent*
    clone () const;
  };

  struct LIBODB_EXPORT object_changed: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual object_changed*
    clone () const;
  };

  struct LIBODB_EXPORT result_not_cached: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual result_not_cached*
    clone () const;
  };

  struct LIBODB_EXPORT database_exception: odb::exception
  {
    // Abstract.
  };

  // Polymorphism support exceptions.
  //
  struct LIBODB_EXPORT abstract_class: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual abstract_class*
    clone () const;
  };

  struct LIBODB_EXPORT no_type_info: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual no_type_info*
    clone () const;
  };

  // Prepared query support exceptions.
  //
  struct LIBODB_EXPORT prepared_already_cached: odb::exception
  {
    prepared_already_cached (const char* name);
    ~prepared_already_cached () ODB_NOTHROW_NOEXCEPT;

    const char*
    name () const
    {
      return name_;
    }

    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual prepared_already_cached*
    clone () const;

  private:
    const char* name_;
    std::string what_;
  };

  struct LIBODB_EXPORT prepared_type_mismatch: odb::exception
  {
    prepared_type_mismatch (const char* name);
    ~prepared_type_mismatch () ODB_NOTHROW_NOEXCEPT;

    const char*
    name () const {return name_;}

    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual prepared_type_mismatch*
    clone () const;

  private:
    const char* name_;
    std::string what_;
  };

  // Schema catalog exceptions.
  //
  struct LIBODB_EXPORT unknown_schema: odb::exception
  {
    unknown_schema (const std::string& name);
    ~unknown_schema () ODB_NOTHROW_NOEXCEPT;

    const std::string&
    name () const {return name_;}

    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual unknown_schema*
    clone () const;

  private:
    std::string name_;
    std::string what_;
  };

  struct LIBODB_EXPORT unknown_schema_version: odb::exception
  {
    unknown_schema_version (schema_version);
    ~unknown_schema_version () ODB_NOTHROW_NOEXCEPT;

    schema_version
    version () const {return version_;}

    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual unknown_schema_version*
    clone () const;

  private:
    schema_version version_;
    std::string what_;
  };

  // Section exceptions.
  //
  struct LIBODB_EXPORT section_not_loaded: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual section_not_loaded*
    clone () const;
  };

  struct LIBODB_EXPORT section_not_in_object: odb::exception
  {
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual section_not_in_object*
    clone () const;
  };

  // Bulk operation exceptions.
  //
  struct LIBODB_EXPORT multiple_exceptions: odb::exception
  {
    struct value_type
    {
      std::size_t
      position () const {return p_;}

      // If true, then this means that some positions in the batch have
      // triggered the exception but it is not possible, due to the
      // limitations of the underlying database API, to discern exactly
      // which ones. As a result, all the positions in the batch are
      // marked as "maybe failed".
      //
      bool
      maybe () const {return m_;}

      const odb::exception&
      exception () const {return *e_;}

      // Implementation details.
      //
    public:
      value_type (std::size_t p,
                  bool maybe,
                  details::shared_ptr<odb::exception> e)
          : m_ (maybe), p_ (p), e_ (e) {}

      value_type (std::size_t p): p_ (p) {} // "Key" for set lookup.

    private:
      bool m_;
      std::size_t p_;
      details::shared_ptr<odb::exception> e_;
    };

    struct comparator_type
    {
      bool
      operator() (const value_type& x, const value_type& y) const
      {
        return x.position () < y.position ();
      }
    };

    typedef std::set<value_type, comparator_type> set_type;

    // Iteration.
    //
  public:
    typedef set_type::const_iterator iterator;
    typedef set_type::const_iterator const_iterator; // For pedantic types.

    iterator
    begin () const {return set_.begin ();}

    iterator
    end () const {return set_.end ();}

    // Lookup.
    //
  public:
    // Return NULL if the element at this position has no exception. Note
    // that the returned value is value_type* and not odb::exception* in
    // order to provide access to maybe(); see value_type::maybe() for
    // details.
    //
    const value_type*
    operator[] (std::size_t p) const
    {
      return set_.empty () ? 0 : lookup (p);
    }

    // Severity, failed and attempt counts.
    //
  public:
    // Return the number of elements for which the operation has been
    // attempted.
    //
    std::size_t
    attempted () const {return attempted_;}

    // Return the number of positions for which the operation has failed.
    // Note that this count includes the maybe failed positions.
    //
    std::size_t
    failed () const {return set_.size ();}

    // If fatal() returns true, then (some of) the exceptions were fatal.
    // In this case, even for elements that were processed but did not
    // cause the exception, no attempts were made to complete the bulk
    // operation and the transaction must be aborted.
    //
    // If fatal() returns false, then the operation on the elements that
    // don't have an exception has succeeded. The application can try to
    // correct the errors and re-attempt the operation on the elements
    // that did cause an exception. In either case, the transaction can
    // be committed.
    //
    bool
    fatal () const {return fatal_;}

    // Normally you shouldn't need to do this explicitly but you can
    // "upgrade" an exception to fatal, for example, for specific
    // database error codes.
    //
    void
    fatal (bool f) {fatal_ = fatal_ || f;}

    // odb::exception interface.
    //
  public:
    virtual const char*
    what () const ODB_NOTHROW_NOEXCEPT;

    virtual multiple_exceptions*
    clone () const;

    // Direct set access.
    //
  public:
    const set_type&
    set () const {return set_;}

    // Implementation details.
    //
  public:
    ~multiple_exceptions () ODB_NOTHROW_NOEXCEPT;

    // All instances of the common exception must be equal since we are
    // going to create and share just one.
    //
    multiple_exceptions (const std::type_info& common_exception_ti)
        : common_exception_ti_ (common_exception_ti),
          fatal_ (false),
          delta_ (0),
          current_ (0) {}

    // Set the attempted count as (delta + n).
    //
    void
    attempted (std::size_t n) {attempted_ = delta_ + n;}

    // Increment the position of the current batch. Also resets the
    // current position in the batch.
    //
    void
    delta (std::size_t d) {delta_ += d; current_ = 0;}

    // Current position in the batch.
    //
    std::size_t
    current () const {return current_;}

    void
    current (std::size_t c) {current_ = c;}

    void
    insert (std::size_t p,
            bool maybe,
            const odb::exception& e,
            bool fatal = false);

    void
    insert (std::size_t p, const odb::exception& e, bool fatal = false)
    {
      insert (p, false, e, fatal);
    }

    void
    insert (const odb::exception& e, bool fatal = false)
    {
      insert (current_, e, fatal);
    }

    bool
    empty () const {return set_.empty ();}

    void
    prepare ();

  private:
    const value_type*
    lookup (std::size_t p) const;

  private:
    const std::type_info& common_exception_ti_;
    details::shared_ptr<odb::exception> common_exception_;

    set_type set_;
    bool fatal_;
    std::size_t attempted_;
    std::size_t delta_;     // Position of the batch.
    std::size_t current_;   // Position in the batch.
    std::string what_;
  };

  namespace common
  {
    using odb::null_pointer;

    using odb::already_in_transaction;
    using odb::not_in_transaction;
    using odb::transaction_already_finalized;

    using odb::already_in_session;
    using odb::not_in_session;
    using odb::session_required;

    using odb::recoverable;
    using odb::deadlock;
    using odb::connection_lost;
    using odb::timeout;
    using odb::object_not_persistent;
    using odb::object_already_persistent;
    using odb::object_changed;
    using odb::result_not_cached;
    using odb::database_exception;

    using odb::abstract_class;
    using odb::no_type_info;

    using odb::unknown_schema;
    using odb::unknown_schema_version;

    using odb::section_not_loaded;
    using odb::section_not_in_object;

    using odb::multiple_exceptions;
  }
}

#include <odb/post.hxx>

#endif // ODB_EXCEPTIONS_HXX