NTP Analyzer  0.8.2
Analyze the operation of time servers
DatabaseUpdater.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;
24 using System.Collections.Generic;
25 using System.Data;
26 using System.Diagnostics.CodeAnalysis;
27 using System.Linq;
28 using Ntp.Common.Log;
29 using Ntp.Data.Log;
30 
31 namespace Ntp.Data.Schema
32 {
33  public class DatabaseUpdater : IEnumerable<IVersionChanges>
34  {
36  {
37  this.factory = factory;
38  this.log = log;
39  changes = new List<IVersionChanges>();
40  }
41 
42  private readonly List<IVersionChanges> changes;
43  private readonly ISqlFactory factory;
44  private readonly LogBase log;
45 
46  IEnumerator IEnumerable.GetEnumerator()
47  {
48  return changes.GetEnumerator();
49  }
50 
51  public IEnumerator<IVersionChanges> GetEnumerator()
52  {
53  return changes.GetEnumerator();
54  }
55 
56  public void Add(IVersionChanges item)
57  {
58  changes.Add(item);
59  }
60 
61  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
62  public void ApplyChanges()
63  {
64  var controller = new VersionController(factory, log);
65  int databaseVersion = controller.FindCurrentVersion();
66 
67  if (databaseVersion <= 0)
68  {
69  var last = changes.OrderBy(c => c.VersionNumber).LastOrDefault();
70  if (last != null)
71  controller.SetCurrentVersion(last);
72 
73  return;
74  }
75 
76  IVersionChanges lastVersion = null;
77  var versionChanges = changes.
78  OrderBy(c => c.VersionNumber).
79  Where(c => c.VersionNumber > databaseVersion);
80 
81  var changeList = versionChanges.ToList();
82  if (changeList.Count == 0)
83  {
84  log.SchemaUpToDate();
85  return;
86  }
87 
88  log.ApplySchemaChanges();
89 
90  var connection = factory.CreateConnection();
91  connection.Open();
92 
93  foreach (var version in changeList)
94  {
95  var transaction = connection.BeginTransaction();
96  try
97  {
98  foreach (var change in version)
99  {
100  var command = factory.CreateCommand();
101  command.Connection = connection;
102  command.CommandText = change.Sql;
103  command.Transaction = transaction;
104  command.Prepare();
105  log.SqlExecute(command.CommandText);
106  command.ExecuteNonQuery();
107  }
108  transaction.Commit();
109  }
110  catch (Exception e)
111  {
112  log.SchemaUpdateError(e);
113  transaction.Rollback();
114  return;
115  }
116 
117  lastVersion = version;
118  log.SchemaChangesApplied(lastVersion.VersionText);
119  }
120 
121  if (connection.State == ConnectionState.Open)
122  connection.Close();
123 
124  controller.SetCurrentVersion(lastVersion);
125  }
126 
127  public bool CheckLatestVersion()
128  {
129  var controller = new VersionController(factory, log);
130  int databaseVersion = controller.FindCurrentVersion();
131  var latest = changes.OrderBy(c => c.VersionNumber).Last();
132 
133  if (databaseVersion == -1)
134  {
135  controller.SetCurrentVersion(latest);
136  return true;
137  }
138 
139  if (databaseVersion == latest.VersionNumber)
140  return true;
141 
142  log.SchemaVersionError(databaseVersion, latest.VersionNumber);
143  return false;
144  }
145  }
146 }
function factory
Definition: jquery.js:15
var e
Definition: bootstrap.min.js:6
void Add(IVersionChanges item)
IEnumerator< IVersionChanges > GetEnumerator()
DatabaseUpdater(ISqlFactory factory, LogBase log)
var c
Definition: bootstrap.min.js:6
readonly ISqlFactory factory
var version
Definition: bootstrap.js:13
readonly List< IVersionChanges > changes