NTP Analyzer  0.8.2
Analyze the operation of time servers
VersionController.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.Data;
24 using System.Diagnostics.CodeAnalysis;
25 using Ntp.Common.Log;
26 using Ntp.Data.Log;
27 
28 namespace Ntp.Data.Schema
29 {
30  public sealed class VersionController
31  {
33  {
34  this.factory = factory;
35  this.log = log;
36  }
37 
38  private const string CreateSql = "CREATE TABLE version (number INT NOT NULL PRIMARY KEY);";
39  private const string SelectSql = "SELECT number FROM version;";
40  private const string UpdateSql = "UPDATE version SET number=@number;";
41  private const string InsertSql = "INSERT INTO version (number) VALUES (0);";
42  private readonly ISqlFactory factory;
43  private readonly LogBase log;
44 
45  public int FindCurrentVersion()
46  {
47  if (CheckTable())
48  return SelectVersion();
49 
50  CreateTable();
51  InsertTable();
52  return -1;
53  }
54 
56  {
57  if (version == null)
58  {
59  log.VersionTableParamError();
60  return;
61  }
62 
63  IDbConnection connection = null;
64 
65  try
66  {
67  connection = factory.CreateConnection();
68  connection.Open();
69  var command = factory.CreateCommand();
70  command.Connection = connection;
71  command.CommandText = UpdateSql;
72  command.Parameters.Add(factory.CreateParameter("@number", version.VersionNumber));
73  command.Prepare();
74  log.SqlExecute(command.CommandText, command.Parameters);
75  command.ExecuteNonQuery();
76  }
77  catch (Exception e)
78  {
79  log.VersionTableUpdateError(e);
80  }
81  finally
82  {
83  if (connection != null && connection.State == ConnectionState.Open)
84  connection.Close();
85  }
86 
87  log.SchemaUpdated(version.VersionText);
88  }
89 
90  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
91  private bool CheckTable()
92  {
93  IDbConnection connection = null;
94 
95  try
96  {
97  connection = factory.CreateConnection();
98  connection.Open();
99  var command = factory.CreateCommand();
100  command.Connection = connection;
101  command.CommandText = factory.PrepareCheckTableSql("version");
102  command.Prepare();
103  log.SqlExecute(command.CommandText);
104  var reader = command.ExecuteReader();
105  return reader.Read();
106  }
107  catch (Exception e)
108  {
109  log.VersionTableCreateError(e);
110  return false;
111  }
112  finally
113  {
114  if (connection != null && connection.State == ConnectionState.Open)
115  connection.Close();
116  }
117  }
118 
119  private void CreateTable()
120  {
121  IDbConnection connection = null;
122 
123  try
124  {
125  connection = factory.CreateConnection();
126  connection.Open();
127  var command = factory.CreateCommand();
128  command.Connection = connection;
129  command.CommandText = CreateSql;
130  command.Prepare();
131  log.SqlExecute(command.CommandText);
132  command.ExecuteNonQuery();
133  }
134  catch (Exception e)
135  {
136  log.VersionTableCreateError(e);
137  }
138  finally
139  {
140  if (connection != null && connection.State == ConnectionState.Open)
141  connection.Close();
142  }
143 
144  log.VersionTableCreated();
145  }
146 
147  private void InsertTable()
148  {
149  IDbConnection connection = null;
150 
151  try
152  {
153  connection = factory.CreateConnection();
154  connection.Open();
155  var command = factory.CreateCommand();
156  command.Connection = connection;
157  command.CommandText = InsertSql;
158  command.Prepare();
159  log.SqlExecute(command.CommandText);
160  command.ExecuteNonQuery();
161  }
162  catch (Exception e)
163  {
164  log.VersionTableInsertError(e);
165  }
166  finally
167  {
168  if (connection != null && connection.State == ConnectionState.Open)
169  connection.Close();
170  }
171  }
172 
173  private int SelectVersion()
174  {
175  IDbConnection connection = null;
176 
177  try
178  {
179  connection = factory.CreateConnection();
180  connection.Open();
181  var command = factory.CreateCommand();
182  command.Connection = connection;
183  command.CommandText = SelectSql;
184  command.Prepare();
185  log.SqlExecute(command.CommandText);
186  var result = command.ExecuteScalar();
187  return Convert.ToInt32(result);
188  }
189  catch (Exception e)
190  {
191  log.VersionTableFetchError(e);
192  return -1;
193  }
194  finally
195  {
196  if (connection != null && connection.State == ConnectionState.Open)
197  connection.Close();
198  }
199  }
200  }
201 }
VersionController(ISqlFactory factory, LogBase log)
function factory
Definition: jquery.js:15
var e
Definition: bootstrap.min.js:6
void SetCurrentVersion(IVersionChanges version)
var version
Definition: bootstrap.js:13