23 using System.Collections.Generic;
24 using System.Diagnostics.CodeAnalysis;
30 namespace Ntp.Analyzer.Data.Sql
43 this.hostMapper = hostMapper;
44 this.peerMapper = peerMapper;
47 private const string SelectSql =
49 "id, time, zone, hostId, peerId, [offset], jitter, frequency, stability " +
52 private const string InsertSql =
53 "INSERT INTO hostReading ( time, zone, hostId, peerId, [offset], jitter, frequency, stability ) " +
54 "VALUES ( @time, @zone, @hostId, @peerId, @offset, @jitter, @frequency, @stability );{0};";
59 protected override bool UseCache =>
false;
61 protected override string TableName =>
"hostReading";
63 protected override string CreateSql =>
"CREATE TABLE hostReading ( " +
64 " id {0} PRIMARY KEY, " +
65 " time TIMESTAMP NOT NULL, " +
66 " zone INT NOT NULL, " +
67 " hostId INT NOT NULL, " +
68 " peerId INT NOT NULL, " +
69 " [offset] DOUBLE PRECISION NOT NULL, " +
70 " jitter DOUBLE PRECISION NOT NULL, " +
71 " frequency DOUBLE PRECISION NOT NULL, " +
72 " stability DOUBLE PRECISION NOT NULL, " +
73 " FOREIGN KEY (hostId) REFERENCES host(id), " +
74 " FOREIGN KEY (peerId) REFERENCES peer(id) " +
81 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
91 Command.CommandText = PrepareSql(SelectSql);
92 Log.SqlExecute(Command.CommandText);
93 Reader = Command.ExecuteReader();
97 Log.ReadError(TableName, e);
104 while (Reader.Read())
106 int id = Convert.ToInt32(Reader[
"id"]);
107 var time = Convert.ToDateTime(Reader[
"time"]);
108 int zone = Convert.ToInt32(Reader[
"zone"]);
109 int hostId = Convert.ToInt32(Reader[
"hostId"]);
110 var host = hostMapper[hostId];
111 int peerId = Convert.ToInt32(Reader[
"peerId"]);
112 var peer = peerMapper[peerId];
113 double offset = Convert.ToDouble(Reader[
"offset"]);
114 double jitter = Convert.ToDouble(Reader[
"jitter"]);
115 double frequency = Convert.ToDouble(Reader[
"frequency"]);
116 double stability = Convert.ToDouble(Reader[
"stability"]);
119 id, time, zone, host, peer,
120 offset, jitter, frequency, stability);
122 yield
return reading;
129 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
137 Command.CommandText = PrepareInsertSql(InsertSql);
138 Command.Parameters.Add(CreateParameter(
"@time", item.
Time));
139 Command.Parameters.Add(CreateParameter(
"@zone", item.
UtcOffset));
140 Command.Parameters.Add(CreateParameter(
"@hostId", item.
Host.
Id));
141 Command.Parameters.Add(CreateParameter(
"@peerId", item.
Peer.
Id));
142 Command.Parameters.Add(CreateParameter(
"@offset", item.
Offset));
143 Command.Parameters.Add(CreateParameter(
"@jitter", item.
Jitter));
144 Command.Parameters.Add(CreateParameter(
"@frequency", item.
Frequency));
145 Command.Parameters.Add(CreateParameter(
"@stability", item.
Stability));
147 Log.SqlExecute(Command.CommandText, Command.Parameters);
148 var idObject = Command.ExecuteScalar();
149 item.
SetId(Convert.ToInt32(idObject));
153 Log.InsertError(TableName, e);
override void ReadContent()
OR/M mapper for tables with a large amount of rows.
OR/M mapper for table host.
override void Update(HostReading item)
const string DatabaseCacheError
const string DatabaseNoUpdate
HostReadingDatabaseMapper(HostDatabaseMapper hostMapper, PeerDatabaseMapper peerMapper, LogBase log)
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
OR/M mapper for table peer.
readonly PeerDatabaseMapper peerMapper
override void Insert(HostReading item)
int Id
Gets the identifier.
OR/M mapper for table hostReading.
readonly HostDatabaseMapper hostMapper
override IEnumerator< HostReading > GetEnumerator()
Read all data from table in a sequential manner.