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, drift " +
48 private const string InsertSql =
49 "INSERT INTO driftReading ( time, zone, hostId, drift ) " +
50 "VALUES ( @time, @zone, @hostId, @drift );{0};";
54 protected override bool UseCache =>
false;
56 protected override string TableName =>
"driftReading";
58 protected override string CreateSql =>
"CREATE TABLE driftReading ( " +
59 " id {0} PRIMARY KEY, " +
60 " time TIMESTAMP NOT NULL, " +
61 " zone INT NOT NULL, " +
62 " hostId INT NOT NULL, " +
63 " drift DOUBLE PRECISION NOT NULL, " +
64 " FOREIGN KEY (hostId) REFERENCES host(id) " +
71 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
77 string sql = PrepareSql(SelectSql);
82 Command.CommandText = sql;
83 Log.SqlExecute(Command.CommandText);
84 Reader = Command.ExecuteReader();
88 Log.ReadError(TableName, e);
97 int id = Convert.ToInt32(Reader[
"id"]);
98 var time = Convert.ToDateTime(Reader[
"time"]);
99 int zone = Convert.ToInt32(Reader[
"zone"]);
100 int hostId = Convert.ToInt32(Reader[
"hostId"]);
101 var host = hostMapper[hostId];
102 double drift = Convert.ToDouble(Reader[
"drift"]);
103 yield
return new DriftReading(
id, time, zone, host, drift);
110 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
118 Command.CommandText = PrepareInsertSql(InsertSql);
119 Command.Parameters.Add(CreateParameter(
"@time", item.
Time));
120 Command.Parameters.Add(CreateParameter(
"@zone", item.
UtcOffset));
121 Command.Parameters.Add(CreateParameter(
"@hostId", item.
Host.
Id));
122 Command.Parameters.Add(CreateParameter(
"@drift", item.
Value));
124 Log.SqlExecute(Command.CommandText, Command.Parameters);
125 var idObject = Command.ExecuteScalar();
126 item.
SetId(Convert.ToInt32(idObject));
130 Log.InsertError(TableName, e);
override IEnumerator< DriftReading > GetEnumerator()
Read all data from table in a sequential manner.
readonly HostDatabaseMapper hostMapper
DriftReadingDatabaseMapper(HostDatabaseMapper hostMapper, LogBase log)
override void ReadContent()
OR/M mapper for tables with a large amount of rows.
OR/M mapper for table host.
OR/M mapper for table driftReading.
override void Update(DriftReading item)
const string DatabaseCacheError
const string DatabaseNoUpdate
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
override void Insert(DriftReading item)
Value from NTP drift file.
double Value
Gets the drift value.
int Id
Gets the identifier.