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

#ifndef ODB_MYSQL_STATEMENT_HXX
#define ODB_MYSQL_STATEMENT_HXX

#include <mysql/mysql.h>

#include <map>
#include <string>
#include <typeinfo>

#include <odb/forward.hxx>
#include <odb/traits.hxx>
#include <odb/shared-ptr.hxx>

namespace odb
{
  namespace mysql
  {
    class connection;

    class statement: public shared_base
    {
    public:
      virtual
      ~statement () = 0;

    protected:
      statement (connection&);

    protected:
      connection& conn_;
      MYSQL_STMT* stmt_;
    };

    template <typename T>
    class insert_statement: public statement
    {
    public:
      typedef T object_type;
      typedef odb::object_traits<T> object_traits;
      typedef typename object_traits::image_type image_type;

    public:
      virtual
      ~insert_statement ();

      insert_statement (connection& conn,
                        const std::string& query,
                        image_type&);

      image_type&
      image ()
      {
        return image_;
      }

      void
      execute ();

    private:
      insert_statement (const insert_statement&);
      insert_statement& operator= (const insert_statement&);

    private:
      image_type& image_;
      MYSQL_BIND bind_[object_traits::column_count];
    };

    // Statement cache.
    //

    class object_statements_base: public shared_base
    {
    public:
      virtual
      ~object_statements_base ();

    protected:
      object_statements_base (connection& conn)
          : conn_ (conn)
      {
      }

    protected:
      connection& conn_;
    };

    template <typename T>
    class object_statements: public object_statements_base
    {
    public:
      typedef typename object_traits<T>::image_type image_type;

      object_statements (connection& conn)
          : object_statements_base (conn)
      {
      }

      insert_statement<T>&
      insert ()
      {
        if (insert_ == 0)
          insert_.reset (
            new (shared) insert_statement<T> (
              conn_, object_traits<T>::insert_query, image_));

        return *insert_;
      }

    private:
      image_type image_;
      odb::shared_ptr<insert_statement<T> > insert_;
    };

    struct type_info_comparator
    {
      bool
      operator() (const std::type_info* x, const std::type_info* y) const
      {
        // XL C++ on AIX has buggy type_info::before() in that
        // it returns true for two different type_info objects
        // that happened to be for the same type.
        //
#if defined(__xlC__) && defined(_AIX)
        return *x != *y && x->before (*y);
#else
        return x->before (*y);
#endif
      }
    };

    class statement_cache
    {
    public:
      statement_cache (connection& conn)
          : conn_ (conn)
      {
      }

      template <typename T>
      object_statements<T>&
      find ()
      {
        map::iterator i (map_.find (&typeid (T)));

        if (i != map_.end ())
          return static_cast<object_statements<T>&> (*i->second);

        shared_ptr<object_statements<T> > p (
          new (shared) object_statements<T> (conn_));

        map_.insert (map::value_type (&typeid (T), p));
        return *p;
      }

    private:
      typedef std::map<const std::type_info*,
                       shared_ptr<object_statements_base>,
                       type_info_comparator> map;

      connection& conn_;
      map map_;
    };
  }
}

#include <odb/mysql/statement.txx>

#endif // ODB_MYSQL_STATEMENT_HXX