aboutsummaryrefslogtreecommitdiff
path: root/odb/mysql/error.cxx
blob: 5c609595636e3002918ddea3638aca69c3c5f846 (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
// file      : odb/mysql/error.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <new>    // std::bad_alloc
#include <string>

#include <odb/mysql/mysql.hxx>
#include <odb/mysql/connection.hxx>
#include <odb/mysql/exceptions.hxx>

using namespace std;

namespace odb
{
  namespace mysql
  {
    void
    translate_error (connection& c,
                     unsigned int e,
                     const string& sqlstate,
                     const string& message)
    {
      switch (e)
      {
      case CR_OUT_OF_MEMORY:
        {
          throw bad_alloc ();
        }
      case ER_LOCK_DEADLOCK:
        {
          throw deadlock ();
        }
      case CR_SERVER_LOST:
      case CR_SERVER_GONE_ERROR:
        {
          c.mark_failed ();
          throw connection_lost ();
        }
      case CR_UNKNOWN_ERROR:
        {
          c.mark_failed ();
          // Fall through.
        }
      default:
        {
          throw database_exception (e, sqlstate, message);
        }
      }
    }

    void
    translate_error (connection& c)
    {
      MYSQL* h (c.handle ());
      translate_error (c,
                       mysql_errno (h),
                       mysql_sqlstate (h),
                       mysql_error (h));
    }

    void
    translate_error (connection& c, MYSQL_STMT* h)
    {
      translate_error (c,
                       mysql_stmt_errno (h),
                       mysql_stmt_sqlstate (h),
                       mysql_stmt_error (h));
    }
  }
}