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

#include <cstddef> // std::size_t
#include <cstring> // std::memset

#include <odb/session.hxx>
#include <odb/callback.hxx>
#include <odb/exceptions.hxx>

#include <odb/sqlite/connection.hxx>

namespace odb
{
  namespace sqlite
  {
    template <typename T>
    object_statements<T>::
    ~object_statements ()
    {
    }

    template <typename T>
    object_statements<T>::
    object_statements (connection_type& conn)
        : object_statements_base (conn),
          container_statement_cache_ (conn),
          select_image_binding_ (select_image_bind_, select_column_count),
          insert_image_binding_ (insert_image_bind_, insert_column_count),
          update_image_binding_ (update_image_bind_, update_column_count),
          id_image_binding_ (update_image_bind_ + update_column_count,
                             id_column_count)
    {
      image_.version = 0;
      select_image_version_ = 0;
      insert_image_version_ = 0;
      update_image_version_ = 0;

      id_image_.version = 0;
      id_image_version_ = 0;

      std::memset (insert_image_bind_, 0, sizeof (insert_image_bind_));
      std::memset (update_image_bind_, 0, sizeof (update_image_bind_));
      std::memset (select_image_bind_, 0, sizeof (select_image_bind_));
      std::memset (
        select_image_truncated_, 0, sizeof (select_image_truncated_));

      for (std::size_t i (0); i < select_column_count; ++i)
        select_image_bind_[i].truncated = select_image_truncated_ + i;
    }

    template <typename T>
    void object_statements<T>::
    load_delayed_ ()
    {
      database& db (connection ().database ());

      delayed_loads dls;
      swap_guard sg (*this, dls);

      while (!dls.empty ())
      {
        delayed_load l (dls.back ());
        typename object_cache_traits::insert_guard g (l.pos);
        dls.pop_back ();

        if (!object_traits::find_ (*this, l.id))
          throw object_not_persistent ();

        object_traits::callback (db, *l.obj, callback_event::pre_load);

        // Our calls to init/load below can result in additional delayed
        // loads being added to the delayed_ vector. We need to process
        // those before we call the post callback.
        //
        object_traits::init (*l.obj, image (), db);
        object_traits::load_ (*this, *l.obj); // Load containers, etc.

        if (!delayed_.empty ())
          load_delayed_ ();

        // Temporarily unlock the statement for the post_load call so that
        // it can load objects of this type recursively. This is safe to do
        // because we have completely loaded the current object. Also the
        // delayed_ list is clear before the unlock and should be clear on
        // re-lock (since a callback can only call public API functions
        // which will make sure all the delayed loads are processed before
        // returning).
        //
        {
          auto_unlock u (*this);
          object_traits::callback (db, *l.obj, callback_event::post_load);
        }

        g.release ();
      }
    }

    template <typename T>
    void object_statements<T>::
    clear_delayed_ ()
    {
      // Remove the objects from the session cache.
      //
      if (session::has_current ())
      {
        for (typename delayed_loads::iterator i (delayed_.begin ()),
               e (delayed_.end ()); i != e; ++i)
        {
          object_cache_traits::erase (i->pos);
        }
      }

      delayed_.clear ();
    }
  }
}