aboutsummaryrefslogtreecommitdiff
path: root/odb/mssql/connection.cxx
blob: 50bc8dbd88d349a629fb0989e6af737af66e2273 (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
// file      : odb/mssql/connection.cxx
// author    : Constantin Michael <constantin@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : ODB NCUEL; see accompanying LICENSE file

//@@ disabled functionality

//#include <string>

#include <odb/mssql/mssql.hxx>
#include <odb/mssql/database.hxx>
#include <odb/mssql/connection.hxx>
//#include <odb/mssql/transaction.hxx>
//#include <odb/mssql/statement.hxx>
//#include <odb/mssql/statement-cache.hxx>
#include <odb/mssql/error.hxx>

using namespace std;

namespace odb
{
  namespace mssql
  {
    connection::
    connection (database_type& db)
        : odb::connection (db),
          db_ (db),
          state_ (state_disconnected)
          // statement_cache_ (new statement_cache_type (*this))
    {
      SQLRETURN r;

      // Allocate the connection handle.
      //
      {
        SQLHANDLE h;
        r = SQLAllocHandle (SQL_HANDLE_DBC, db_.environment (), &h);

        if (!SQL_SUCCEEDED (r))
          translate_error (db_.environment (), SQL_HANDLE_ENV);

        handle_.reset (h);
      }

      // Connect.
      //
      {
        SQLSMALLINT out_conn_str_size;
        r = SQLDriverConnect (handle_,
                              0, // Parent windows handle.
                              (SQLCHAR*) db_.connect_string ().c_str (),
                              SQL_NTS,
                              0, // Output connection string buffer.
                              0, // Size of output connection string buffer.
                              &out_conn_str_size,
                              SQL_DRIVER_NOPROMPT);

        if (!SQL_SUCCEEDED (r))
          // Still use the handle version of translate_error since there
          // is no connection.
          //
          translate_error (handle_, SQL_HANDLE_DBC);

        state_ = state_connected;
      }

      // If an exception is thrown after this line, we will not disconnect
      // the connection.
      //
    }

    connection::
    connection (database_type& db, SQLHDBC handle)
        : odb::connection (db),
          db_ (db),
          handle_ (handle),
          state_ (state_connected)
          // statement_cache_ (new statement_cache_type (*this))
    {
    }

    connection::
    ~connection ()
    {
      // Deallocate prepared statements before we close the connection.
      //
      //statement_cache_.reset ();

      if (state_ != state_disconnected)
        SQLDisconnect (handle_); // Ignore any errors.
    }

    transaction_impl* connection::
    begin ()
    {
      //@@
      //return new transaction_impl (connection_ptr (inc_ref (this)));
      return 0;
    }

    unsigned long long connection::
    execute (const char* /*s*/, std::size_t /*n*/)
    {
      //@@
      //generic_statement st (*this, string (s, n));
      //return st.execute ();
      return 0;
    }
  }
}