NTP Analyzer  0.8.2
Analyze the operation of time servers
DatabaseInitializer.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using Ntp.Analyzer.Data.Changes;
25 using Ntp.Analyzer.Data.Log;
26 using Ntp.Analyzer.Data.Sql;
27 using Ntp.Common.Log;
28 using Ntp.Data;
29 using Ntp.Data.Provider;
30 using Ntp.Data.Schema;
31 
32 namespace Ntp.Analyzer.Data
33 {
34  public sealed class DatabaseInitializer
35  {
36  public DatabaseInitializer(bool initialize, bool upgrade, LogBase log)
37  {
38  this.initialize = initialize;
39  this.upgrade = upgrade;
40  this.log = log;
41 
42  updater = new DatabaseUpdater(SqlDatabaseFactory.Instance, log)
43  {
44  new Change01(),
45  new Change02(),
46  new Change03()
47  };
48  }
49 
50  private readonly bool initialize;
51 
52  private readonly LogBase log;
53 
54  private readonly DatabaseUpdater updater;
55 
56  private readonly bool upgrade;
57 
58  public bool Execute()
59  {
60  if (initialize)
61  {
62  CreateDatabase();
63  CreateTables();
64  }
65 
66  if (upgrade)
67  {
68  updater.ApplyChanges();
69  }
70 
71  return updater.CheckLatestVersion();
72  }
73 
74  private void CreateDatabase()
75  {
76  try
77  {
79  }
80  catch (Exception e)
81  {
82  log.CreateDatabaseError(e);
83  }
84  }
85 
86  private void CreateTables()
87  {
88  TimeServerDatabaseMapper timeServerMapper;
89  PeerDatabaseMapper peerMapper;
90  HostDatabaseMapper hostMapper;
91 
92  var initializers = new List<ITableInitializer>();
93  initializers.Add(timeServerMapper = new TimeServerDatabaseMapper(log));
94  initializers.Add(peerMapper = new PeerDatabaseMapper(timeServerMapper, log));
95  initializers.Add(hostMapper = new HostDatabaseMapper(log));
96  initializers.Add(new HostReadingDatabaseMapper(hostMapper, peerMapper, log));
97  initializers.Add(new PeerReadingDatabaseMapper(hostMapper, peerMapper, log));
98  initializers.Add(new HostIoReadingDatabaseMapper(hostMapper, log));
99  initializers.Add(new DriftReadingDatabaseMapper(hostMapper, log));
100  initializers.Add(new PeerActivityDatabaseMapper(hostMapper, peerMapper, log));
101  initializers.Add(new AssociationEntryMapper(log));
102 
103  foreach (var initializer in initializers)
104  {
105  try
106  {
107  initializer.CheckTable();
108  }
109  catch (Exception e)
110  {
111  log.CreateTableError(e);
112  }
113  }
114  }
115  }
116 }
var e
Definition: bootstrap.min.js:6
static SqlDatabaseFactory Instance
DatabaseInitializer(bool initialize, bool upgrade, LogBase log)
OR/M mapper for table associationEntry.