23 using System.Collections.Generic;
24 using System.Diagnostics.CodeAnalysis;
30 namespace Ntp.Analyzer.Data.Sql
42 private const string SelectSql =
44 "id, tallyChar, remote, refid, stratus, type, lastPoll, poll, reach, delay, [offset], jitter, hostId " +
45 "FROM associationEntry";
47 private const string InsertSql =
48 "INSERT INTO associationEntry ( tallyChar, hostId, remote, refid, stratus, type, lastPoll, poll, reach, delay, [offset], jitter ) " +
49 "VALUES ( @tallyChar, @hostId, @remote, @refid, @stratus, @type, @lastPoll, @poll, @reach, @delay, @offset, @jitter );{0};";
51 private const string DeleteUniqueSql =
52 "DELETE FROM associationEntry WHERE remote = @remote AND hostId = @hostId";
54 private const string DeleteHostSql =
55 "DELETE FROM associationEntry WHERE hostId = @hostId";
57 protected override bool UseCache =>
false;
59 protected override string TableName =>
"associationEntry";
61 protected override string CreateSql =>
"CREATE TABLE associationEntry ( " +
62 " id {0} PRIMARY KEY, " +
63 " tallyChar CHAR(1) NOT NULL, " +
64 " remote VARCHAR(45) NOT NULL, " +
65 " refid VARCHAR(45) NOT NULL, " +
66 " stratus INT NOT NULL, " +
67 " type CHAR(1) NOT NULL, " +
68 " lastPoll INT NOT NULL, " +
69 " poll 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 " hostId INT NOT NULL, " +
75 " UNIQUE KEY (hostId, remote), " +
76 " FOREIGN KEY (hostId) REFERENCES host(id) " +
79 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
80 public void Delete(
string ip,
int hostId)
87 Command.CommandText = PrepareSql(DeleteUniqueSql);
88 Command.Parameters.Add(CreateParameter(
"@remote", ip));
89 Command.Parameters.Add(CreateParameter(
"@hostId", hostId));
91 Log.SqlExecute(Command.CommandText, Command.Parameters);
92 Command.ExecuteNonQuery();
96 Log.DeleteError(TableName, e);
105 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
113 Command.CommandText = PrepareSql(DeleteHostSql);
114 Command.Parameters.Add(CreateParameter(
"@hostId", hostId));
116 Log.SqlExecute(Command.CommandText, Command.Parameters);
117 Command.ExecuteNonQuery();
121 Log.DeleteError(TableName, e);
134 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
144 Command.CommandText = PrepareSql(SelectSql);
145 Log.SqlExecute(Command.CommandText);
146 Reader = Command.ExecuteReader();
150 Log.ReadError(TableName, e);
157 while (Reader.Read())
159 int id = Convert.ToInt32(Reader[
"id"]);
160 string tallyChar = Convert.ToString(Reader[
"tallyChar"]);
161 string remote = Convert.ToString(Reader[
"remote"]);
162 string refid = Convert.ToString(Reader[
"refid"]);
163 int stratus = Convert.ToInt32(Reader[
"stratus"]);
164 char type = Convert.ToChar(Reader[
"type"]);
165 int lastPoll = Convert.ToInt32(Reader[
"lastPoll"]);
166 int poll = Convert.ToInt32(Reader[
"poll"]);
167 int reach = Convert.ToInt32(Reader[
"reach"]);
168 double delay = Convert.ToDouble(Reader[
"delay"]);
169 double offset = Convert.ToDouble(Reader[
"offset"]);
170 double jitter = Convert.ToDouble(Reader[
"jitter"]);
171 int hostId = Convert.ToInt32(Reader[
"hostId"]);
173 if (tallyChar ==
string.Empty)
177 id, hostId, tallyChar[0], remote, refid, stratus, type,
178 lastPoll, poll, reach, delay, offset, jitter);
187 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
195 Command.CommandText = PrepareInsertSql(InsertSql);
196 Command.Parameters.Add(CreateParameter(
"@tallyChar", item.
TallyChar));
197 Command.Parameters.Add(CreateParameter(
"@remote", item.
Remote));
198 Command.Parameters.Add(CreateParameter(
"@refid", item.
Refid));
199 Command.Parameters.Add(CreateParameter(
"@stratus", item.
Stratus));
200 Command.Parameters.Add(CreateParameter(
"@type", item.
PeerType));
201 Command.Parameters.Add(CreateParameter(
"@lastPoll", item.
LastPoll));
202 Command.Parameters.Add(CreateParameter(
"@poll", item.
Poll));
203 Command.Parameters.Add(CreateParameter(
"@reach", item.
Reach));
204 Command.Parameters.Add(CreateParameter(
"@delay", item.
Delay));
205 Command.Parameters.Add(CreateParameter(
"@offset", item.
Offset));
206 Command.Parameters.Add(CreateParameter(
"@jitter", item.
Jitter));
207 Command.Parameters.Add(CreateParameter(
"@hostId", item.
HostId));
209 Log.SqlExecute(Command.CommandText, Command.Parameters);
210 var idObject = Command.ExecuteScalar();
211 item.
SetId(Convert.ToInt32(idObject));
215 Log.InsertError(TableName, e);
void Delete(string ip, int hostId)
string Refid
Gets the reference ID (0.0.0.0 if this is unknown)
double Jitter
Gets the dispersion of the peer in milliseconds.
int Poll
Gets the the polling interval in seconds.
int Stratus
Gets the stratum of the remote peer.
char PeerType
Gets the type of the peer (local, unicast, multicast or broadcast)
override void Insert(AssociationEntry item)
const string DatabaseCacheError
double Offset
Gets the offset of the peer in milliseconds.
const string DatabaseNoUpdate
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
override void Update(AssociationEntry item)
string Remote
Gets the address of the remote peer.
char TallyChar
Gets the tally char.
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
int HostId
Gets the host ID of this association entry.
AssociationEntryMapper(LogBase log)
double Delay
Gets the current estimated delay.
override IEnumerator< AssociationEntry > GetEnumerator()
Read all data from table in a sequential manner.
override void ReadContent()
int LastPoll
Gets when the last packet was received, the polling interval, in seconds.
int Reach
Gets the reachability register in octal.
OR/M mapper for table associationEntry.