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

#include <new> // std::bad_alloc

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

namespace odb
{
  namespace mysql
  {
    connection::
    connection (database& db)
        : handle_ (&mysql_), statement_cache_ (*this)
    {
      if (mysql_init (handle_) == 0)
        throw std::bad_alloc ();

      if (mysql_real_connect (handle_,
                              db.host (),
                              db.user (),
                              db.passwd (),
                              db.db (),
                              db.port (),
                              db.socket (),
                              db.client_flags ()) == 0)
      {
        database_exception e (handle_);
        mysql_close (handle_);
        throw e;
      }
    }

    connection::
    ~connection ()
    {
      mysql_close (handle_);
    }
  }
}