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

#ifndef ODB_SQLITE_STATEMENT_CACHE_HXX
#define ODB_SQLITE_STATEMENT_CACHE_HXX

#include <odb/pre.hxx>

#include <map>
#include <typeinfo>

#include <odb/forward.hxx>
#include <odb/details/shared-ptr.hxx>
#include <odb/details/type-info.hxx>

#include <odb/sqlite/version.hxx>
#include <odb/sqlite/statement.hxx>
#include <odb/sqlite/object-statements.hxx>
#include <odb/sqlite/details/export.hxx>

namespace odb
{
  namespace sqlite
  {
    class connection;

    class LIBODB_SQLITE_EXPORT statement_cache
    {
    public:
      statement_cache (connection&);

      simple_statement&
      begin_statement () const
      {
        return *begin_;
      }

      simple_statement&
      begin_immediate_statement () const
      {
        if (!begin_immediate_)
          begin_immediate_statement_ ();

        return *begin_immediate_;
      }

      simple_statement&
      begin_exclusive_statement () const
      {
        if (!begin_exclusive_)
          begin_exclusive_statement_ ();

        return *begin_exclusive_;
      }

      simple_statement&
      commit_statement () const
      {
        return *commit_;
      }

      simple_statement&
      rollback_statement () const
      {
        return *rollback_;
      }

      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);

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

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

    private:
      void
      begin_immediate_statement_ () const;

      void
      begin_exclusive_statement_ () const;

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

      connection& conn_;

      details::shared_ptr<simple_statement> begin_;
      mutable details::shared_ptr<simple_statement> begin_immediate_;
      mutable details::shared_ptr<simple_statement> begin_exclusive_;
      details::shared_ptr<simple_statement> commit_;
      details::shared_ptr<simple_statement> rollback_;

      map map_;
    };
  }
}

#include <odb/post.hxx>

#endif // ODB_SQLITE_STATEMENT_CACHE_HXX