NTP Analyzer  0.8.2
Analyze the operation of time servers
AssociationEntryMapper.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 AssociationEntryMapper : SqlDatabaseMapper<AssociationEntry>
36  {
38  : base(log)
39  {
40  }
41 
42  private const string SelectSql =
43  "SELECT " +
44  "id, tallyChar, remote, refid, stratus, type, lastPoll, poll, reach, delay, [offset], jitter, hostId " +
45  "FROM associationEntry";
46 
47  private const string InsertSql =
48  "INSERT INTO associationEntry ( tallyChar, hostId, remote, refid, stratus, type, lastPoll, poll, reach, delay, [offset], jitter ) " +
49  "VALUES ( @tallyChar, @hostId, @remote, @refid, @stratus, @type, @lastPoll, @poll, @reach, @delay, @offset, @jitter );{0};";
50 
51  private const string DeleteUniqueSql =
52  "DELETE FROM associationEntry WHERE remote = @remote AND hostId = @hostId";
53 
54  private const string DeleteHostSql =
55  "DELETE FROM associationEntry WHERE hostId = @hostId";
56 
57  protected override bool UseCache => false;
58 
59  protected override string TableName => "associationEntry";
60 
61  protected override string CreateSql => "CREATE TABLE associationEntry ( " +
62  " id {0} PRIMARY KEY, " +
63  " tallyChar CHAR(1) NOT NULL, " +
64  " remote VARCHAR(45) NOT NULL, " +
65  " refid VARCHAR(45) NOT NULL, " +
66  " stratus INT NOT NULL, " +
67  " type CHAR(1) NOT NULL, " +
68  " lastPoll INT NOT NULL, " +
69  " poll INT NOT NULL, " +
70  " reach INT NOT NULL, " +
71  " delay DOUBLE PRECISION NOT NULL, " +
72  " [offset] DOUBLE PRECISION NOT NULL, " +
73  " jitter DOUBLE PRECISION NOT NULL, " +
74  " hostId INT NOT NULL, " +
75  " UNIQUE KEY (hostId, remote), " +
76  " FOREIGN KEY (hostId) REFERENCES host(id) " +
77  "){1};";
78 
79  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
80  public void Delete(string ip, int hostId)
81  {
82  lock (MapperLocker)
83  {
84  try
85  {
86  Open();
87  Command.CommandText = PrepareSql(DeleteUniqueSql);
88  Command.Parameters.Add(CreateParameter("@remote", ip));
89  Command.Parameters.Add(CreateParameter("@hostId", hostId));
90  Command.Prepare();
91  Log.SqlExecute(Command.CommandText, Command.Parameters);
92  Command.ExecuteNonQuery();
93  }
94  catch (Exception e)
95  {
96  Log.DeleteError(TableName, e);
97  }
98  finally
99  {
100  Close();
101  }
102  }
103  }
104 
105  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
106  public void Delete(int hostId)
107  {
108  lock (MapperLocker)
109  {
110  try
111  {
112  Open();
113  Command.CommandText = PrepareSql(DeleteHostSql);
114  Command.Parameters.Add(CreateParameter("@hostId", hostId));
115  Command.Prepare();
116  Log.SqlExecute(Command.CommandText, Command.Parameters);
117  Command.ExecuteNonQuery();
118  }
119  catch (Exception e)
120  {
121  Log.DeleteError(TableName, e);
122  }
123  finally
124  {
125  Close();
126  }
127  }
128  }
129 
134  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
135  public override IEnumerator<AssociationEntry> GetEnumerator()
136  {
137  lock (MapperLocker)
138  {
139  bool error = false;
140 
141  try
142  {
143  Open();
144  Command.CommandText = PrepareSql(SelectSql);
145  Log.SqlExecute(Command.CommandText);
146  Reader = Command.ExecuteReader();
147  }
148  catch (Exception e)
149  {
150  Log.ReadError(TableName, e);
151  error = true;
152  }
153 
154  if (error)
155  yield break;
156 
157  while (Reader.Read())
158  {
159  int id = Convert.ToInt32(Reader["id"]);
160  string tallyChar = Convert.ToString(Reader["tallyChar"]);
161  string remote = Convert.ToString(Reader["remote"]);
162  string refid = Convert.ToString(Reader["refid"]);
163  int stratus = Convert.ToInt32(Reader["stratus"]);
164  char type = Convert.ToChar(Reader["type"]);
165  int lastPoll = Convert.ToInt32(Reader["lastPoll"]);
166  int poll = Convert.ToInt32(Reader["poll"]);
167  int reach = Convert.ToInt32(Reader["reach"]);
168  double delay = Convert.ToDouble(Reader["delay"]);
169  double offset = Convert.ToDouble(Reader["offset"]);
170  double jitter = Convert.ToDouble(Reader["jitter"]);
171  int hostId = Convert.ToInt32(Reader["hostId"]);
172 
173  if (tallyChar == string.Empty)
174  tallyChar = " ";
175 
176  var entry = new AssociationEntry(
177  id, hostId, tallyChar[0], remote, refid, stratus, type,
178  lastPoll, poll, reach, delay, offset, jitter);
179 
180  yield return entry;
181  }
182 
183  Close();
184  }
185  }
186 
187  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
188  protected override void Insert(AssociationEntry item)
189  {
190  lock (MapperLocker)
191  {
192  try
193  {
194  Open();
195  Command.CommandText = PrepareInsertSql(InsertSql);
196  Command.Parameters.Add(CreateParameter("@tallyChar", item.TallyChar));
197  Command.Parameters.Add(CreateParameter("@remote", item.Remote));
198  Command.Parameters.Add(CreateParameter("@refid", item.Refid));
199  Command.Parameters.Add(CreateParameter("@stratus", item.Stratus));
200  Command.Parameters.Add(CreateParameter("@type", item.PeerType));
201  Command.Parameters.Add(CreateParameter("@lastPoll", item.LastPoll));
202  Command.Parameters.Add(CreateParameter("@poll", item.Poll));
203  Command.Parameters.Add(CreateParameter("@reach", item.Reach));
204  Command.Parameters.Add(CreateParameter("@delay", item.Delay));
205  Command.Parameters.Add(CreateParameter("@offset", item.Offset));
206  Command.Parameters.Add(CreateParameter("@jitter", item.Jitter));
207  Command.Parameters.Add(CreateParameter("@hostId", item.HostId));
208  Command.Prepare();
209  Log.SqlExecute(Command.CommandText, Command.Parameters);
210  var idObject = Command.ExecuteScalar();
211  item.SetId(Convert.ToInt32(idObject));
212  }
213  catch (Exception e)
214  {
215  Log.InsertError(TableName, e);
216  }
217  finally
218  {
219  Close();
220  }
221  }
222  }
223 
224  protected override void ReadContent()
225  {
226  throw new NotSupportedException(LogMessages.DatabaseCacheError);
227  }
228 
229  protected override void Update(AssociationEntry item)
230  {
231  throw new NotSupportedException(string.Format(LogMessages.DatabaseNoUpdate, TableName));
232  }
233  }
234 }
string Refid
Gets the reference ID (0.0.0.0 if this is unknown)
double Jitter
Gets the dispersion of the peer in milliseconds.
int Poll
Gets the the polling interval in seconds.
int Stratus
Gets the stratum of the remote peer.
char PeerType
Gets the type of the peer (local, unicast, multicast or broadcast)
override void Insert(AssociationEntry item)
double Offset
Gets the offset of the peer in milliseconds.
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
override void Update(AssociationEntry item)
var e
Definition: bootstrap.min.js:6
string Remote
Gets the address of the remote peer.
char TallyChar
Gets the tally char.
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
int HostId
Gets the host ID of this association entry.
double Delay
Gets the current estimated delay.
override IEnumerator< AssociationEntry > GetEnumerator()
Read all data from table in a sequential manner.
int LastPoll
Gets when the last packet was received, the polling interval, in seconds.
int Reach
Gets the reachability register in octal.
OR/M mapper for table associationEntry.