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, last, reach, delay, [offset], jitter " +
52 private const string InsertSql =
53 "INSERT INTO peerReading ( time, zone, hostId, peerId, last, reach, delay, [offset], jitter ) " +
54 "VALUES ( @time, @zone, @hostId, @peerId, @last, @reach, @delay, @offset, @jitter );{0};";
59 protected override bool UseCache =>
false;
61 protected override string TableName =>
"peerReading";
63 protected override string CreateSql =>
"CREATE TABLE peerReading ( " +
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 " last 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 " FOREIGN KEY (hostId) REFERENCES host(id), " +
75 " FOREIGN KEY (peerId) REFERENCES peer(id) " +
82 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
92 Command.CommandText = PrepareSql(SelectSql);
93 Log.SqlExecute(Command.CommandText);
94 Reader = Command.ExecuteReader();
98 Log.ReadError(TableName, e);
105 while (Reader.Read())
107 int id = Convert.ToInt32(Reader[
"id"]);
108 var time = Convert.ToDateTime(Reader[
"time"]);
109 int zone = Convert.ToInt32(Reader[
"zone"]);
110 int hostId = Convert.ToInt32(Reader[
"hostId"]);
111 var host = hostMapper[hostId];
112 int peerId = Convert.ToInt32(Reader[
"peerId"]);
113 var peer = peerMapper[peerId];
114 int last = Convert.ToInt32(Reader[
"last"]);
115 int reach = Convert.ToInt32(Reader[
"reach"]);
116 double delay = Convert.ToDouble(Reader[
"delay"]);
117 double offset = Convert.ToDouble(Reader[
"offset"]);
118 double jitter = Convert.ToDouble(Reader[
"jitter"]);
121 id, time, zone, host, peer,
122 last, reach, delay, offset, jitter);
124 yield
return reading;
131 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
139 Command.CommandText = PrepareInsertSql(InsertSql);
140 Command.Parameters.Add(CreateParameter(
"@time", item.
Time));
141 Command.Parameters.Add(CreateParameter(
"@zone", item.
UtcOffset));
142 Command.Parameters.Add(CreateParameter(
"@hostId", item.
Host.
Id));
143 Command.Parameters.Add(CreateParameter(
"@peerId", item.
Peer.
Id));
144 Command.Parameters.Add(CreateParameter(
"@last", item.
LastPoll));
145 Command.Parameters.Add(CreateParameter(
"@reach", item.
Reach));
146 Command.Parameters.Add(CreateParameter(
"@delay", item.
Delay));
147 Command.Parameters.Add(CreateParameter(
"@offset", item.
Offset));
148 Command.Parameters.Add(CreateParameter(
"@jitter", item.
Jitter));
150 Log.SqlExecute(Command.CommandText, Command.Parameters);
151 var idObject = Command.ExecuteScalar();
152 item.
SetId(Convert.ToInt32(idObject));
156 Log.InsertError(TableName, e);
override void Update(PeerReading item)
OR/M mapper for tables with a large amount of rows.
OR/M mapper for table host.
readonly PeerDatabaseMapper peerMapper
const string DatabaseCacheError
const string DatabaseNoUpdate
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
PeerReadingDatabaseMapper(HostDatabaseMapper hostMapper, PeerDatabaseMapper peerMapper, LogBase log)
OR/M mapper for table peer.
override IEnumerator< PeerReading > GetEnumerator()
Read all data from table in a sequential manner.
readonly HostDatabaseMapper hostMapper
override void ReadContent()
override void Insert(PeerReading item)
OR/M mapper for table peerReading.
int Id
Gets the identifier.