23 using System.Collections.Generic;
24 using System.Diagnostics.CodeAnalysis;
30 namespace Ntp.Analyzer.Data.Sql
40 this.hostMapper = hostMapper;
43 private const string SelectSql =
45 "id, time, zone, hostId, timeSinceReset, " +
46 "receiveBuffers, freeReceiveBuffers, usedReceiveBuffers, lowWaterRefills, " +
47 "droppedPackets, ignoredPackets, receivedPackets, " +
48 "packetsSent, packetsNotSent, interruptsHandled, receivedByInt " +
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 ) " +
58 "@time, @zone, @hostId, @timeSinceReset, " +
59 "@receiveBuffers, @freeReceiveBuffers, @usedReceiveBuffers, @lowWaterRefills, " +
60 "@droppedPackets, @ignoredPackets, @receivedPackets, " +
61 "@packetsSent, @packetsNotSent, @interruptsHandled, @receivedByInt );{0};";
65 protected override bool UseCache =>
false;
67 protected override string TableName =>
"hostIoReading";
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) " +
93 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
103 Command.CommandText = PrepareSql(SelectSql);
104 Log.SqlExecute(Command.CommandText);
105 Reader = Command.ExecuteReader();
109 Log.ReadError(TableName, e);
116 while (Reader.Read())
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"]);
137 id, time, zone, host, timeSinceReset,
138 receiveBuffers, freeReceiveBuffers, usedReceiveBuffers, lowWaterRefills,
139 droppedPackets, ignoredPackets, receivedPackets,
140 packetsSent, packetsNotSent,
141 interruptsHandled, receivedByInt);
143 yield
return reading;
150 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
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));
175 Log.SqlExecute(Command.CommandText, Command.Parameters);
176 var idObject = Command.ExecuteScalar();
177 item.
SetId(Convert.ToInt32(idObject));
181 Log.InsertError(TableName, e);
OR/M mapper for tables with a large amount of rows.
OR/M mapper for table host.
readonly HostDatabaseMapper hostMapper
override IEnumerator< HostIoReading > GetEnumerator()
Read all data from table in a sequential manner.
HostIoReadingDatabaseMapper(HostDatabaseMapper hostMapper, LogBase log)
const string DatabaseCacheError
override void ReadContent()
const string DatabaseNoUpdate
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
OR/M mapper for table hostIoReading.
override void Update(HostIoReading item)
override void Insert(HostIoReading item)
int Id
Gets the identifier.