Files
ntpa/Ntp.Analyzer.Data/DatabaseInitializer.cs
T

107 lines
3.7 KiB
C#

//
// Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
using System;
using System.Collections.Generic;
using Ntp.Analyzer.Data.Changes;
using Ntp.Analyzer.Data.Sql;
using Ntp.Common.Log;
using Ntp.Data;
using Ntp.Data.Provider;
using Ntp.Data.Schema;
namespace Ntp.Analyzer.Data
{
public sealed class DatabaseInitializer
{
public DatabaseInitializer(bool upgrade, LogBase log)
{
this.upgrade = upgrade;
this.log = log;
}
private readonly LogBase log;
private readonly bool upgrade;
public void Execute()
{
CreateDatabase();
CreateTables();
if (upgrade)
ApplyChanges();
}
private void ApplyChanges()
{
var updater = new DatabaseUpdater(SqlDatabaseFactory.Instance, log)
{
new Change01()
};
updater.ApplyChanges();
}
private void CreateDatabase()
{
try
{
SqlDatabaseFactory.Instance.CreateDatabase();
}
catch (Exception e)
{
log.WriteLine(LogMessage.DatabaseCreateError, Severity.Error);
log.WriteLine(e, Severity.Trace);
}
}
private void CreateTables()
{
TimeServerDatabaseMapper timeServerMapper;
PeerDatabaseMapper peerMapper;
HostDatabaseMapper hostMapper;
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));
initializers.Add(new PeerActivityDatabaseMapper(hostMapper, peerMapper, log));
initializers.Add(new AssociationEntryMapper(log));
foreach (ITableInitializer initializer in initializers)
{
try
{
initializer.CheckTable();
}
catch (Exception e)
{
log.WriteLine(LogMessage.DatabaseCreateTableError, Severity.Error);
log.WriteLine(e, Severity.Trace);
}
}
}
}
}