NTP Analyzer  0.8.2
Analyze the operation of time servers
HostIoReadingDatabaseMapper.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 HostIoReadingDatabaseMapper : FilteredSqlDatabaseMapper<HostIoReading>
36  {
38  : base(log)
39  {
40  this.hostMapper = hostMapper;
41  }
42 
43  private const string SelectSql =
44  "SELECT " +
45  "id, time, zone, hostId, timeSinceReset, " +
46  "receiveBuffers, freeReceiveBuffers, usedReceiveBuffers, lowWaterRefills, " +
47  "droppedPackets, ignoredPackets, receivedPackets, " +
48  "packetsSent, packetsNotSent, interruptsHandled, receivedByInt " +
49  "FROM hostIoReading";
50 
51  private const string InsertSql =
52  "INSERT INTO hostIoReading ( " +
53  "time, zone, hostId, timeSinceReset, " +
54  "receiveBuffers, freeReceiveBuffers, usedReceiveBuffers, lowWaterRefills, " +
55  "droppedPackets, ignoredPackets, receivedPackets, " +
56  "packetsSent, packetsNotSent, interruptsHandled, receivedByInt ) " +
57  "VALUES ( " +
58  "@time, @zone, @hostId, @timeSinceReset, " +
59  "@receiveBuffers, @freeReceiveBuffers, @usedReceiveBuffers, @lowWaterRefills, " +
60  "@droppedPackets, @ignoredPackets, @receivedPackets, " +
61  "@packetsSent, @packetsNotSent, @interruptsHandled, @receivedByInt );{0};";
62 
63  private readonly HostDatabaseMapper hostMapper;
64 
65  protected override bool UseCache => false;
66 
67  protected override string TableName => "hostIoReading";
68 
69  protected override string CreateSql => "CREATE TABLE hostIoReading ( " +
70  " id {0} PRIMARY KEY, " +
71  " time TIMESTAMP NOT NULL, " +
72  " zone INT NOT NULL, " +
73  " hostId INT NOT NULL, " +
74  " timeSinceReset INT NOT NULL, " +
75  " receiveBuffers INT NOT NULL, " +
76  " freeReceiveBuffers INT NOT NULL, " +
77  " usedReceiveBuffers INT NOT NULL, " +
78  " lowWaterRefills INT NOT NULL, " +
79  " droppedPackets BIGINT NOT NULL, " +
80  " ignoredPackets BIGINT NOT NULL, " +
81  " receivedPackets BIGINT NOT NULL, " +
82  " packetsSent BIGINT NOT NULL, " +
83  " packetsNotSent BIGINT NOT NULL, " +
84  " interruptsHandled INT NOT NULL, " +
85  " receivedByInt INT NOT NULL, " +
86  " FOREIGN KEY (hostId) REFERENCES host(id) " +
87  "){1};";
88 
93  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
94  public override IEnumerator<HostIoReading> GetEnumerator()
95  {
96  lock (MapperLocker)
97  {
98  bool error = false;
99 
100  try
101  {
102  Open();
103  Command.CommandText = PrepareSql(SelectSql);
104  Log.SqlExecute(Command.CommandText);
105  Reader = Command.ExecuteReader();
106  }
107  catch (Exception e)
108  {
109  Log.ReadError(TableName, e);
110  error = true;
111  }
112 
113  if (error)
114  yield break;
115 
116  while (Reader.Read())
117  {
118  int id = Convert.ToInt32(Reader["id"]);
119  var time = Convert.ToDateTime(Reader["time"]);
120  int zone = Convert.ToInt32(Reader["zone"]);
121  int hostId = Convert.ToInt32(Reader["hostId"]);
122  var host = hostMapper[hostId];
123  int timeSinceReset = Convert.ToInt32(Reader["timeSinceReset"]);
124  int receiveBuffers = Convert.ToInt32(Reader["receiveBuffers"]);
125  int freeReceiveBuffers = Convert.ToInt32(Reader["freeReceiveBuffers"]);
126  int usedReceiveBuffers = Convert.ToInt32(Reader["usedReceiveBuffers"]);
127  int lowWaterRefills = Convert.ToInt32(Reader["lowWaterRefills"]);
128  long droppedPackets = Convert.ToInt64(Reader["droppedPackets"]);
129  long ignoredPackets = Convert.ToInt64(Reader["ignoredPackets"]);
130  long receivedPackets = Convert.ToInt64(Reader["receivedPackets"]);
131  long packetsSent = Convert.ToInt64(Reader["packetsSent"]);
132  long packetsNotSent = Convert.ToInt64(Reader["packetsNotSent"]);
133  int interruptsHandled = Convert.ToInt32(Reader["interruptsHandled"]);
134  int receivedByInt = Convert.ToInt32(Reader["receivedByInt"]);
135 
136  var reading = new HostIoReading(
137  id, time, zone, host, timeSinceReset,
138  receiveBuffers, freeReceiveBuffers, usedReceiveBuffers, lowWaterRefills,
139  droppedPackets, ignoredPackets, receivedPackets,
140  packetsSent, packetsNotSent,
141  interruptsHandled, receivedByInt);
142 
143  yield return reading;
144  }
145 
146  Close();
147  }
148  }
149 
150  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
151  protected override void Insert(HostIoReading item)
152  {
153  lock (MapperLocker)
154  {
155  try
156  {
157  Open();
158  Command.CommandText = PrepareInsertSql(InsertSql);
159  Command.Parameters.Add(CreateParameter("@time", item.Time));
160  Command.Parameters.Add(CreateParameter("@zone", item.UtcOffset));
161  Command.Parameters.Add(CreateParameter("@hostId", item.Host.Id));
162  Command.Parameters.Add(CreateParameter("@timeSinceReset", item.TimeSinceReset));
163  Command.Parameters.Add(CreateParameter("@receiveBuffers", item.ReceiveBuffers));
164  Command.Parameters.Add(CreateParameter("@freeReceiveBuffers", item.FreeReceiveBuffers));
165  Command.Parameters.Add(CreateParameter("@usedReceiveBuffers", item.UsedReceiveBuffers));
166  Command.Parameters.Add(CreateParameter("@lowWaterRefills", item.LowWaterRefills));
167  Command.Parameters.Add(CreateParameter("@droppedPackets", item.DroppedPackets));
168  Command.Parameters.Add(CreateParameter("@ignoredPackets", item.IgnoredPackets));
169  Command.Parameters.Add(CreateParameter("@receivedPackets", item.ReceivedPackets));
170  Command.Parameters.Add(CreateParameter("@packetsSent", item.PacketsSent));
171  Command.Parameters.Add(CreateParameter("@packetsNotSent", item.PacketsNotSent));
172  Command.Parameters.Add(CreateParameter("@interruptsHandled", item.InterruptsHandled));
173  Command.Parameters.Add(CreateParameter("@receivedByInt", item.ReceivedByInt));
174  Command.Prepare();
175  Log.SqlExecute(Command.CommandText, Command.Parameters);
176  var idObject = Command.ExecuteScalar();
177  item.SetId(Convert.ToInt32(idObject));
178  }
179  catch (Exception e)
180  {
181  Log.InsertError(TableName, e);
182  }
183  finally
184  {
185  Close();
186  }
187  }
188  }
189 
190  protected override void ReadContent()
191  {
192  throw new NotSupportedException(LogMessages.DatabaseCacheError);
193  }
194 
195  protected override void Update(HostIoReading item)
196  {
197  throw new NotSupportedException(string.Format(LogMessages.DatabaseNoUpdate, TableName));
198  }
199  }
200 }
OR/M mapper for tables with a large amount of rows.
override IEnumerator< HostIoReading > GetEnumerator()
Read all data from table in a sequential manner.
HostIoReadingDatabaseMapper(HostDatabaseMapper hostMapper, LogBase log)
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
var e
Definition: bootstrap.min.js:6