NTP Analyzer  0.8.2
Analyze the operation of time servers
DriftReadingDatabaseMapper.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 System.Diagnostics.CodeAnalysis;
25 using Ntp.Analyzer.Data.Log;
26 using Ntp.Analyzer.Objects;
27 using Ntp.Common.Log;
28 using Ntp.Data.Log;
29 
30 namespace Ntp.Analyzer.Data.Sql
31 {
35  public sealed class DriftReadingDatabaseMapper : FilteredSqlDatabaseMapper<DriftReading>
36  {
38  : base(log)
39  {
40  this.hostMapper = hostMapper;
41  }
42 
43  private const string SelectSql =
44  "SELECT " +
45  "id, time, zone, hostId, drift " +
46  "FROM driftReading";
47 
48  private const string InsertSql =
49  "INSERT INTO driftReading ( time, zone, hostId, drift ) " +
50  "VALUES ( @time, @zone, @hostId, @drift );{0};";
51 
52  private readonly HostDatabaseMapper hostMapper;
53 
54  protected override bool UseCache => false;
55 
56  protected override string TableName => "driftReading";
57 
58  protected override string CreateSql => "CREATE TABLE driftReading ( " +
59  " id {0} PRIMARY KEY, " +
60  " time TIMESTAMP NOT NULL, " +
61  " zone INT NOT NULL, " +
62  " hostId INT NOT NULL, " +
63  " drift DOUBLE PRECISION NOT NULL, " +
64  " FOREIGN KEY (hostId) REFERENCES host(id) " +
65  "){1};";
66 
71  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
72  public override IEnumerator<DriftReading> GetEnumerator()
73  {
74  lock (MapperLocker)
75  {
76  bool error = false;
77  string sql = PrepareSql(SelectSql);
78 
79  try
80  {
81  Open();
82  Command.CommandText = sql;
83  Log.SqlExecute(Command.CommandText);
84  Reader = Command.ExecuteReader();
85  }
86  catch (Exception e)
87  {
88  Log.ReadError(TableName, e);
89  error = true;
90  }
91 
92  if (error)
93  yield break;
94 
95  while (Reader.Read())
96  {
97  int id = Convert.ToInt32(Reader["id"]);
98  var time = Convert.ToDateTime(Reader["time"]);
99  int zone = Convert.ToInt32(Reader["zone"]);
100  int hostId = Convert.ToInt32(Reader["hostId"]);
101  var host = hostMapper[hostId];
102  double drift = Convert.ToDouble(Reader["drift"]);
103  yield return new DriftReading(id, time, zone, host, drift);
104  }
105 
106  Close();
107  }
108  }
109 
110  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
111  protected override void Insert(DriftReading item)
112  {
113  lock (MapperLocker)
114  {
115  try
116  {
117  Open();
118  Command.CommandText = PrepareInsertSql(InsertSql);
119  Command.Parameters.Add(CreateParameter("@time", item.Time));
120  Command.Parameters.Add(CreateParameter("@zone", item.UtcOffset));
121  Command.Parameters.Add(CreateParameter("@hostId", item.Host.Id));
122  Command.Parameters.Add(CreateParameter("@drift", item.Value));
123  Command.Prepare();
124  Log.SqlExecute(Command.CommandText, Command.Parameters);
125  var idObject = Command.ExecuteScalar();
126  item.SetId(Convert.ToInt32(idObject));
127  }
128  catch (Exception e)
129  {
130  Log.InsertError(TableName, e);
131  }
132  finally
133  {
134  Close();
135  }
136  }
137  }
138 
139  protected override void ReadContent()
140  {
141  throw new NotSupportedException(LogMessages.DatabaseCacheError);
142  }
143 
144  protected override void Update(DriftReading item)
145  {
146  throw new NotSupportedException(string.Format(LogMessages.DatabaseNoUpdate, TableName));
147  }
148  }
149 }
override IEnumerator< DriftReading > GetEnumerator()
Read all data from table in a sequential manner.
DriftReadingDatabaseMapper(HostDatabaseMapper hostMapper, LogBase log)
OR/M mapper for tables with a large amount of rows.
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
var e
Definition: bootstrap.min.js:6
Value from NTP drift file.
Definition: DriftReading.cs:29
double Value
Gets the drift value.
Definition: DriftReading.cs:47