1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-10-05 18:41:13 +00:00
Files
ntpa/Ntp.Analyzer.Data/DatabaseInitializer.cs

116 lines
3.8 KiB
C#
Raw Normal View History

2016-08-06 16:19:35 +02:00
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>
//
2016-04-02 11:22:42 +02:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2016-08-06 16:19:35 +02:00
//
2016-04-02 11:22:42 +02:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2016-08-06 16:19:35 +02:00
//
2016-04-02 11:22:42 +02:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2016-04-10 11:50:35 +02:00
2016-04-02 11:22:42 +02:00
using System;
2016-04-28 19:29:22 +02:00
using System.Collections.Generic;
2016-05-22 23:40:47 +02:00
using Ntp.Analyzer.Data.Changes;
2016-08-27 13:44:19 +02:00
using Ntp.Analyzer.Data.Log;
2016-04-28 19:29:22 +02:00
using Ntp.Analyzer.Data.Sql;
2016-08-18 19:18:52 +02:00
using Ntp.Common.Log;
2016-04-28 19:29:22 +02:00
using Ntp.Data;
2016-05-22 23:40:47 +02:00
using Ntp.Data.Provider;
using Ntp.Data.Schema;
2016-04-02 11:22:42 +02:00
namespace Ntp.Analyzer.Data
{
2016-04-28 19:29:22 +02:00
public sealed class DatabaseInitializer
2016-04-02 11:22:42 +02:00
{
2016-09-09 22:44:02 +02:00
public DatabaseInitializer(bool initialize, bool upgrade, LogBase log)
2016-04-02 11:22:42 +02:00
{
2016-09-09 22:44:02 +02:00
this.initialize = initialize;
2016-05-27 21:20:01 +02:00
this.upgrade = upgrade;
2016-04-02 11:22:42 +02:00
this.log = log;
2016-09-09 22:44:02 +02:00
updater = new DatabaseUpdater(SqlDatabaseFactory.Instance, log)
{
new Change01(),
new Change02(),
new Change03()
};
2016-04-02 11:22:42 +02:00
}
2016-09-09 22:44:02 +02:00
private readonly bool initialize;
2016-04-02 11:22:42 +02:00
private readonly LogBase log;
2016-09-09 22:44:02 +02:00
private readonly DatabaseUpdater updater;
2016-08-06 16:19:35 +02:00
private readonly bool upgrade;
2016-09-09 22:44:02 +02:00
public bool Execute()
2016-04-02 11:22:42 +02:00
{
2016-09-09 22:44:02 +02:00
if (initialize)
{
CreateDatabase();
CreateTables();
}
2016-05-27 21:20:01 +02:00
if (upgrade)
2016-08-18 19:18:52 +02:00
{
2016-09-09 22:44:02 +02:00
updater.ApplyChanges();
}
2016-08-18 19:18:52 +02:00
2016-09-09 22:44:02 +02:00
return updater.CheckLatestVersion();
2016-08-18 19:18:52 +02:00
}
2016-05-22 23:40:47 +02:00
private void CreateDatabase()
2016-04-02 11:22:42 +02:00
{
2016-05-22 23:40:47 +02:00
try
{
SqlDatabaseFactory.Instance.CreateDatabase();
2016-04-28 19:29:22 +02:00
}
2016-05-22 23:40:47 +02:00
catch (Exception e)
{
2016-08-27 13:44:19 +02:00
log.CreateDatabaseError(e);
2016-04-02 11:22:42 +02:00
}
}
2016-05-22 23:40:47 +02:00
private void CreateTables()
2016-04-02 11:22:42 +02:00
{
TimeServerDatabaseMapper timeServerMapper;
PeerDatabaseMapper peerMapper;
HostDatabaseMapper hostMapper;
2016-05-22 23:40:47 +02:00
var initializers = new List<ITableInitializer>();
initializers.Add(timeServerMapper = new TimeServerDatabaseMapper(log));
initializers.Add(peerMapper = new PeerDatabaseMapper(timeServerMapper, log));
initializers.Add(hostMapper = new HostDatabaseMapper(log));
initializers.Add(new HostReadingDatabaseMapper(hostMapper, peerMapper, log));
initializers.Add(new PeerReadingDatabaseMapper(hostMapper, peerMapper, log));
initializers.Add(new HostIoReadingDatabaseMapper(hostMapper, log));
2016-09-03 18:33:27 +02:00
initializers.Add(new DriftReadingDatabaseMapper(hostMapper, log));
2016-05-22 23:40:47 +02:00
initializers.Add(new PeerActivityDatabaseMapper(hostMapper, peerMapper, log));
initializers.Add(new AssociationEntryMapper(log));
2016-04-02 11:22:42 +02:00
2016-08-27 13:44:19 +02:00
foreach (var initializer in initializers)
2016-05-22 23:40:47 +02:00
{
try
{
initializer.CheckTable();
}
catch (Exception e)
{
2016-08-27 13:44:19 +02:00
log.CreateTableError(e);
2016-04-02 11:22:42 +02:00
}
}
}
}
}