23 using System.Diagnostics.CodeAnalysis;
29 namespace Ntp.Analyzer.Data.Sql
39 this.timeServerMapper = timeServerMapper;
42 private const string SelectSql =
43 "SELECT id, name, ip, orgId FROM peer;";
45 private const string InsertSql =
46 "INSERT INTO peer( name, ip, orgId ) VALUES( @name, @ip, @orgId );{0};";
48 private const string UpdateSql =
49 "UPDATE peer SET name = @name, ip = @ip, orgId = @orgId WHERE id = @id";
53 protected override bool UseCache =>
true;
55 protected override string TableName =>
"peer";
57 protected override string CreateSql =>
"CREATE TABLE peer ( " +
58 " id {0} PRIMARY KEY, " +
59 " name VARCHAR(255) NOT NULL, " +
60 " ip VARCHAR(45) NOT NULL, " +
64 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
72 Command.CommandText = PrepareInsertSql(InsertSql);
73 Command.Parameters.Add(CreateParameter(
"@name", item.
Name));
74 Command.Parameters.Add(CreateParameter(
"@ip", item.
Ip));
75 Command.Parameters.Add(CreateParameter(
"@orgId", item.
OrgId));
77 Log.SqlExecute(Command.CommandText, Command.Parameters);
78 var idObject = Command.ExecuteScalar();
79 item.
SetId(Convert.ToInt32(idObject));
83 Log.InsertError(TableName, e);
92 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
98 Command.CommandText = PrepareSql(SelectSql);
99 Log.SqlExecute(Command.CommandText);
100 Reader = Command.ExecuteReader();
102 while (Reader.Read())
104 int id = Convert.ToInt32(Reader[
"id"]);
105 string name = Reader[
"name"].ToString();
106 string ip = Reader[
"ip"].ToString();
107 var orgId = Reader[
"orgId"] != DBNull.Value
108 ? Convert.ToInt32(Reader[
"orgId"])
111 var server = orgId.HasValue
112 ? timeServerMapper[orgId.Value]
115 var peer =
new Peer(
id, name, ip, server);
121 Log.ReadError(TableName, e);
129 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
137 Command.CommandText = PrepareSql(UpdateSql);
138 Command.Parameters.Add(CreateParameter(
"@name", item.
Name));
139 Command.Parameters.Add(CreateParameter(
"@ip", item.
Ip));
140 Command.Parameters.Add(CreateParameter(
"@orgId", item.
OrgId));
141 Command.Parameters.Add(CreateParameter(
"@id", item.
Id));
143 Log.SqlExecute(Command.CommandText, Command.Parameters);
144 Command.ExecuteNonQuery();
148 Log.UpdateError(TableName, e);
OR/M mapper for table timeServer.
override void Insert(Peer item)
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
readonly TimeServerDatabaseMapper timeServerMapper
OR/M mapper for table peer.
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
int Id
Gets the identifier.
override void ReadContent()
int OrgId
Gets the server identifier as specified on support.ntp.org.
override void Update(Peer item)
PeerDatabaseMapper(TimeServerDatabaseMapper timeServerMapper, LogBase log)