aboutsummaryrefslogtreecommitdiff
path: root/odb/schema-catalog.cxx
blob: 87d8b90d3ecca9d8486f1a2de645298c4da3f6a7 (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
// file      : odb/schema-catalog.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <map>
#include <vector>

#include <odb/exceptions.hxx>
#include <odb/schema-catalog.hxx>
#include <odb/schema-catalog-impl.hxx>

using namespace std;

namespace odb
{
  // It is important we use vector to store the list of create
  // functions since schema generators for some databases may
  // rely on the DDL statements executing in a specific order,
  // for example, for foreign key generation.
  //
  typedef bool (*create_function) (database&, unsigned short pass, bool drop);
  typedef vector<create_function> create_functions;
  struct schema_catalog_impl: map<string, create_functions> {};

  schema_catalog_impl* schema_catalog_init::catalog = 0;
  size_t schema_catalog_init::count = 0;

  void schema_catalog::
  create_schema (database& db, const string& name)
  {
    const schema_catalog_impl& c (*schema_catalog_init::catalog);

    schema_catalog_impl::const_iterator i (c.find (name));

    if (i == c.end ())
      throw unknown_schema (name);

    const create_functions& fs (i->second);

    // Run the passes until we ran them all or all the functions
    // return false, which means no more passes necessary. Do that
    // first for drop passes, then for create.
    //

    for (unsigned short pass (1); pass < 3; ++pass)
    {
      bool done (true);

      for (create_functions::const_iterator j (fs.begin ()), e (fs.end ());
           j != e; ++j)
      {
        if ((*j) (db, pass, true))
          done = false;
      }

      if (done)
        break;
    }

    for (unsigned short pass (1); pass < 3; ++pass)
    {
      bool done (true);

      for (create_functions::const_iterator j (fs.begin ()), e (fs.end ());
           j != e; ++j)
      {
        if ((*j) (db, pass, false))
          done = false;
      }

      if (done)
        break;
    }
  }

  // schema_catalog_init
  //
  schema_catalog_init::
  schema_catalog_init ()
  {
    if (count == 0)
      catalog = new schema_catalog_impl;

    ++count;
  }

  schema_catalog_init::
  ~schema_catalog_init ()
  {
    if (--count == 0)
      delete catalog;
  }

  // schema_catalog_entry
  //
  schema_catalog_entry::
  schema_catalog_entry (const char* name, create_function entry)
  {
    schema_catalog_impl& c (*schema_catalog_init::catalog);
    c[name].push_back (entry);
  }
}