Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
schema-catalog.hxx
Go to the documentation of this file.
1 // file : odb/schema-catalog.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_SCHEMA_CATALOG_HXX
6 #define ODB_SCHEMA_CATALOG_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <odb/details/config.hxx> // ODB_CXX11
11 
12 #include <string>
13 #include <cstddef> // std::size_t
14 
15 #ifdef ODB_CXX11
16 # include <functional> // std::function
17 #endif
18 
19 #include <odb/forward.hxx> // schema_version, odb::core
20 
21 #include <odb/details/export.hxx>
22 #include <odb/details/unused.hxx>
23 #include <odb/details/meta/static-assert.hxx>
24 
25 namespace odb
26 {
27  class LIBODB_EXPORT schema_catalog
28  {
29  public:
30  // Schema creation.
31  //
32  static void
33  create_schema (database&, const std::string& name = "", bool drop = true);
34 
35  static void
36  drop_schema (database&, const std::string& name = "");
37 
38  // Schema migration.
39  //
40  public:
41  static void
44  const std::string& name = "")
45  {
46  migrate_schema_impl (db, v, name, migrate_pre);
47  }
48 
49  static void
52  const std::string& name = "")
53  {
54  migrate_schema_impl (db, v, name, migrate_post);
55  }
56 
57  static void
60  const std::string& name = "")
61  {
62  migrate_schema_impl (db, v, name, migrate_both);
63  }
64 
65  // Data migration.
66  //
67  public:
68  // If version is 0, then use the current database version and also
69  // check whether we are in migration. Returns the number of calls made.
70  //
71  static std::size_t
72  migrate_data (database&,
73  schema_version = 0,
74  const std::string& name = "");
75 
76 #ifdef ODB_CXX11
77  typedef std::function<void (database&)> data_migration_function_type;
78 #else
79  typedef void (*data_migration_function_type) (database&);
80 #endif
81 
82  // The following three variants of the registration functions make
83  // sure that the version is greater that the base model version.
84  // This helps with identifying and removing data migration function
85  // that are no longer required.
86  //
87  // Data migration functions are called in the order of registration.
88  //
89  template <schema_version v, schema_version base>
90  static void
91  data_migration_function (data_migration_function_type f,
92  const std::string& name = "")
93  {
94  data_migration_function<v, base> (id_common, f, name);
95  }
96 
97  // Database-specific data migration.
98  //
99  template <schema_version v, schema_version base>
100  static void
102  data_migration_function_type f,
103  const std::string& name = "")
104  {
105  data_migration_function<v, base> (db.id (), f, name);
106  }
107 
108  template <schema_version v, schema_version base>
109  static void
111  data_migration_function_type f,
112  const std::string& name = "")
113  {
114  // If the data migration version is below the base model version
115  // then it will never be called.
116  //
117 #ifdef ODB_CXX11
118  static_assert (v > base || base == 0,
119  "data migration function is no longer necessary");
120 #else
121  // Poor man's static_assert.
122  //
123  typedef details::meta::static_assert_test<(v > base || base == 0)>
124  data_migration_function_is_no_longer_necessary;
125 
126  char sa [sizeof (data_migration_function_is_no_longer_necessary)];
127  ODB_POTENTIALLY_UNUSED (sa);
128 #endif
129 
130  data_migration_function (id, v, f, name);
131  }
132 
133  // The same as above but take the version as an argument and do
134  // not check whether it is greater than the base model version.
135  //
136  static void
138  data_migration_function_type f,
139  const std::string& name = "")
140  {
141  data_migration_function (id_common, v, f, name);
142  }
143 
144  static void
146  schema_version v,
147  data_migration_function_type f,
148  const std::string& name = "")
149  {
150  data_migration_function (db.id (), v, f, name);
151  }
152 
153  static void
154  data_migration_function (database_id,
156  data_migration_function_type,
157  const std::string& name = "");
158 
159  // Combined schema and data migration.
160  //
161  public:
162  // Migrate both schema and data to the specified version. If version
163  // is not specified, then migrate to the current model version.
164  //
165  static void
166  migrate (database&, schema_version = 0, const std::string& name = "");
167 
168  // Schema version information.
169  //
170  public:
171  // Return the base model version.
172  //
173  static schema_version
174  base_version (const database& db, const std::string& name = "")
175  {
176  return base_version (db.id (), name);
177  }
178 
179  static schema_version
180  base_version (database_id, const std::string& name = "");
181 
182  // Return the current model version.
183  //
184  static schema_version
185  current_version (const database& db, const std::string& name = "")
186  {
187  return current_version (db.id (), name);
188  }
189 
190  static schema_version
191  current_version (database_id, const std::string& name = "");
192 
193  // Return current model version + 1 (that is, one past current) if
194  // the passed version is equal to or greater than current. If the
195  // version is not specified, then use the current database version.
196  //
197  static schema_version
198  next_version (const database& db,
199  schema_version v = 0,
200  const std::string& name = "")
201  {
202  return next_version (db.id (), v == 0 ? db.schema_version () : v, name);
203  }
204 
205  static schema_version
206  next_version (database_id,
208  const std::string& name = "");
209 
210  // Schema existence.
211  //
212  public:
213  // Test for presence of a schema with a specific name.
214  //
215  static bool
216  exists (const database& db, const std::string& name = "")
217  {
218  return exists (db.id (), name);
219  }
220 
221  static bool
222  exists (database_id, const std::string& name = "");
223 
224  private:
225  enum migrate_mode
226  {
227  migrate_pre,
228  migrate_post,
229  migrate_both
230  };
231 
232  static void
233  migrate_schema_impl (database&,
235  const std::string& name,
236  migrate_mode);
237  };
238 
239  // Static data migration function registration.
240  //
241  template <schema_version v, schema_version base>
243  {
245 
246  data_migration_entry (function_type f, const std::string& name = "")
247  {
248  schema_catalog::data_migration_function<v, base> (f, name);
249  }
250 
252  function_type f,
253  const std::string& name = "")
254  {
255  schema_catalog::data_migration_function<v, base> (id, v, f, name);
256  }
257  };
258 
259  namespace common
260  {
261  using odb::schema_catalog;
263  }
264 }
265 
266 #include <odb/post.hxx>
267 
268 #endif // ODB_SCHEMA_CATALOG_HXX
Definition: schema-catalog.hxx:27
static void data_migration_function(database &db, data_migration_function_type f, const std::string &name="")
Definition: schema-catalog.hxx:101
Definition: schema-catalog.hxx:242
static schema_version current_version(const database &db, const std::string &name="")
Definition: schema-catalog.hxx:185
void(* data_migration_function_type)(database &)
Definition: schema-catalog.hxx:79
static schema_version next_version(const database &db, schema_version v=0, const std::string &name="")
Definition: schema-catalog.hxx:198
schema_version_type schema_version(const std::string &schema_name="") const
static void migrate_schema(database &db, schema_version v, const std::string &name="")
Definition: schema-catalog.hxx:58
Definition: forward.hxx:81
data_migration_entry(function_type f, const std::string &name="")
Definition: schema-catalog.hxx:246
unsigned long long schema_version
Definition: forward.hxx:30
static bool exists(const database &db, const std::string &name="")
Definition: schema-catalog.hxx:216
Definition: database.hxx:38
static void data_migration_function(database_id id, data_migration_function_type f, const std::string &name="")
Definition: schema-catalog.hxx:110
database_id
Definition: forward.hxx:74
static void migrate_schema_post(database &db, schema_version v, const std::string &name="")
Definition: schema-catalog.hxx:50
static void migrate_schema_pre(database &db, schema_version v, const std::string &name="")
Definition: schema-catalog.hxx:42
static void data_migration_function(data_migration_function_type f, const std::string &name="")
Definition: schema-catalog.hxx:91
static void data_migration_function(schema_version v, data_migration_function_type f, const std::string &name="")
Definition: schema-catalog.hxx:137
static schema_version base_version(const database &db, const std::string &name="")
Definition: schema-catalog.hxx:174
data_migration_entry(database_id id, function_type f, const std::string &name="")
Definition: schema-catalog.hxx:251
static void data_migration_function(database &db, schema_version v, data_migration_function_type f, const std::string &name="")
Definition: schema-catalog.hxx:145
schema_catalog::data_migration_function_type function_type
Definition: schema-catalog.hxx:244
database_id id() const