23 using System.Diagnostics.CodeAnalysis;
31 namespace Ntp.Analyzer.Data.Sql
43 private const string SelectSql =
44 "SELECT id, stratum, countryCode, hostName, ip, ip6, " +
45 "useDns+0 as dns, poolMember+0 as pool, location, displayLocation, organization, " +
46 "coordinates, synchronization, serviceArea, accessPolicy, " +
47 "accessDetails, notification+0 as noti, autoKeyUrl, symmetricKeyType, " +
48 "symmetricKeyURL, serverContact, providerPage, providerUrl, " +
52 private const string InsertSql =
53 "INSERT INTO timeServer( id, stratum, countryCode, hostName, ip, ip6, " +
54 "useDns, poolMember, location, displayLocation, organization, " +
55 "coordinates, synchronization, serviceArea, accessPolicy, " +
56 "accessDetails, notification, autoKeyUrl, symmetricKeyType, " +
57 "symmetricKeyURL, serverContact, providerPage, providerUrl, " +
59 "VALUES ( @id, @stratum, @countryCode, @hostName, @ip, @ip6, " +
60 "@useDns, @poolMember, @location, @displayLocation, @organization, " +
61 "@coordinates, @synchronization, @serviceArea, @accessPolicy, " +
62 "@accessDetails, @notification, @autoKeyUrl, @symmetricKeyType, " +
63 "@symmetricKeyURL, @serverContact, @providerPage, @providerUrl, " +
66 protected override bool UseCache =>
true;
68 protected override string TableName =>
"timeServer";
70 protected override string CreateSql =>
"CREATE TABLE timeServer ( " +
71 " id INT NOT NULL PRIMARY KEY, " +
72 " stratum INT NOT NULL, " +
73 " countryCode CHAR(7) NOT NULL, " +
74 " hostName VARCHAR(50) NOT NULL, " +
75 " ip VARCHAR(15) NULL, " +
76 " ip6 VARCHAR(30) NULL, " +
77 " useDns BIT(1) NULL, " +
78 " poolMember BIT(1) NULL, " +
79 " location VARCHAR(100) NULL, " +
80 " displayLocation VARCHAR(60) NOT NULL, " +
81 " organization VARCHAR(50) NULL, " +
82 " coordinates VARCHAR(50) NULL, " +
83 " synchronization VARCHAR(255) NULL, " +
84 " serviceArea VARCHAR(100) NULL, " +
85 " accessPolicy VARCHAR(255) NULL, " +
86 " accessDetails VARCHAR(512) NULL, " +
87 " notification BIT(1) NULL, " +
88 " autoKeyUrl VARCHAR(255) NULL, " +
89 " symmetricKeyType VARCHAR(100) NULL, " +
90 " symmetricKeyURL VARCHAR(100) NULL, " +
91 " serverContact VARCHAR(255) NULL, " +
92 " updated TIMESTAMP NOT NULL, " +
93 " providerPage VARCHAR(100) NULL, " +
94 " providerUrl VARCHAR(255) NULL " +
113 server = adapter.Import(
id);
119 Log.TimeServerFetchError(e);
126 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
129 string countryCode = item.
Country?.PadRight(7).Substring(0, 7).TrimEnd();
130 string hostName = item.
Name?.PadRight(50).Substring(0, 50).TrimEnd();
131 string ip = item.
Address?.ToString().PadRight(15).Substring(0, 15).TrimEnd();
132 string ip6 = item.
V6Address?.PadRight(30).Substring(0, 30).TrimEnd();
133 string location = item.
Location?.PadRight(100).Substring(0, 100).TrimEnd();
134 string displayLocation = item.
DisplayLocation?.PadRight(60).Substring(0, 60).TrimEnd();
135 string organization = item.
Organization?.PadRight(50).Substring(0, 50).TrimEnd();
136 string coordinates = item.
Geo?.PadRight(50).Substring(0, 50).TrimEnd();
137 string synchronization = item.
Server?.PadRight(255).Substring(0, 255).TrimEnd();
138 string serviceArea = item.
ServiceArea?.PadRight(100).Substring(0, 100).TrimEnd();
139 string accessPolicy = item.
AccessPolicy?.PadRight(255).Substring(0, 255).TrimEnd();
140 string accessDetails = item.
AccessDetails?.PadRight(512).Substring(0, 512).TrimEnd();
141 string autoKeyUrl = item.
AutoKey?.PadRight(255).Substring(0, 255).TrimEnd();
142 string symmetricKeyType = item.
SymKey?.PadRight(100).Substring(0, 100).TrimEnd();
143 string symmetricKeyUrl = item.
SymUrl?.PadRight(100).Substring(0, 100).TrimEnd();
144 string serverContact = item.
Contact?.PadRight(255).Substring(0, 255).TrimEnd();
145 string providerPage = item.
ProviderPage?.PadRight(100).Substring(0, 100).TrimEnd();
146 string providerUrl = item.
ProviderUrl?.PadRight(255).Substring(0, 255).TrimEnd();
153 Command.CommandText = PrepareInsertSql(InsertSql);
154 Command.Parameters.Add(CreateParameter(
"@id", item.
Id));
155 Command.Parameters.Add(CreateParameter(
"@stratum", item.
Stratum));
156 Command.Parameters.Add(CreateParameter(
"@countryCode", countryCode));
157 Command.Parameters.Add(CreateParameter(
"@hostName", hostName));
158 Command.Parameters.Add(CreateParameter(
"@ip", ip));
159 Command.Parameters.Add(CreateParameter(
"@ip6", ip6));
160 Command.Parameters.Add(CreateParameter(
"@useDns", item.
ShouldUseDns));
161 Command.Parameters.Add(CreateParameter(
"@poolMember", item.
IsPoolMember));
162 Command.Parameters.Add(CreateParameter(
"@location", location));
163 Command.Parameters.Add(CreateParameter(
"@displayLocation", displayLocation));
164 Command.Parameters.Add(CreateParameter(
"@organization", organization));
165 Command.Parameters.Add(CreateParameter(
"@coordinates", coordinates));
166 Command.Parameters.Add(CreateParameter(
"@synchronization", synchronization));
167 Command.Parameters.Add(CreateParameter(
"@serviceArea", serviceArea));
168 Command.Parameters.Add(CreateParameter(
"@accessPolicy", accessPolicy));
169 Command.Parameters.Add(CreateParameter(
"@accessDetails", accessDetails));
170 Command.Parameters.Add(CreateParameter(
"@notification", item.
ShouldNotify));
171 Command.Parameters.Add(CreateParameter(
"@autoKeyUrl", autoKeyUrl));
172 Command.Parameters.Add(CreateParameter(
"@symmetricKeyType", symmetricKeyType));
173 Command.Parameters.Add(CreateParameter(
"@symmetricKeyURL", symmetricKeyUrl));
174 Command.Parameters.Add(CreateParameter(
"@serverContact", serverContact));
175 Command.Parameters.Add(CreateParameter(
"@providerPage", providerPage));
176 Command.Parameters.Add(CreateParameter(
"@providerUrl", providerUrl));
177 Command.Parameters.Add(CreateParameter(
"@updated", item.
Updated));
179 Log.SqlExecute(Command.CommandText, Command.Parameters);
180 Command.ExecuteNonQuery();
185 Log.InsertError(TableName, e);
194 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
200 Command.CommandText = PrepareSql(SelectSql);
201 Log.SqlExecute(Command.CommandText);
202 Reader = Command.ExecuteReader();
204 while (Reader.Read())
206 int id = Convert.ToInt32(Reader[
"id"]);
207 int stratum = Convert.ToInt32(Reader[
"stratum"]);
208 string countryCode = Reader[
"countryCode"].ToString();
209 string hostName = Reader[
"hostName"].ToString();
210 string ip4 = Reader[
"ip"] != DBNull.Value ? Reader[
"ip"].ToString() : null;
211 string ip6 = Reader[
"ip6"] != DBNull.Value ? Reader[
"ip6"].ToString() : null;
212 bool? useDns = Reader[
"dns"] != DBNull.Value ? Convert.ToBoolean(Reader[
"dns"]) : (
bool?) null;
213 bool? poolMember = Reader[
"pool"] != DBNull.Value ? Convert.ToBoolean(Reader[
"pool"]) : (
bool?) null;
214 string location = Reader[
"location"].ToString();
215 string displayLocation = Reader[
"displayLocation"].ToString();
216 string organization = Reader[
"organization"] != DBNull.Value
217 ? Reader[
"organization"].ToString()
219 string coordinates = Reader[
"coordinates"] != DBNull.Value
220 ? Reader[
"organization"].ToString()
222 string synchronization = Reader[
"synchronization"].ToString();
223 string serviceArea = Reader[
"serviceArea"].ToString();
224 string accessPolicy = Reader[
"accessPolicy"].ToString();
225 string accessDetails = Reader[
"accessDetails"] != DBNull.Value
226 ? Reader[
"accessDetails"].ToString()
228 bool? notification = Reader[
"noti"] != DBNull.Value
229 ? Convert.ToBoolean(Reader[
"noti"])
231 string autoKeyUrl = Reader[
"autoKeyUrl"] != DBNull.Value ? Reader[
"autoKeyUrl"].ToString() : null;
232 string symmetricKeyType = Reader[
"symmetricKeyType"] != DBNull.Value
233 ? Reader[
"symmetricKeyType"].ToString()
235 string symmetricKeyUrl = Reader[
"symmetricKeyURL"] != DBNull.Value
236 ? Reader[
"symmetricKeyURL"].ToString()
238 string serverContact = Reader[
"serverContact"] != DBNull.Value
239 ? Reader[
"serverContact"].ToString()
241 string providerPage = Reader[
"providerPage"] != DBNull.Value
242 ? Reader[
"providerPage"].ToString()
244 string providerUrl = Reader[
"providerUrl"] != DBNull.Value
245 ? Reader[
"providerUrl"].ToString()
247 DateTime updated = Convert.ToDateTime(Reader[
"updated"]);
249 IPAddress address = null;
251 IPAddress.TryParse(ip4, out address);
254 id, stratum, countryCode, hostName, address, ip6, useDns, poolMember, location,
255 displayLocation, organization, coordinates, synchronization, serviceArea, accessDetails,
256 accessPolicy, notification, autoKeyUrl, symmetricKeyType, symmetricKeyUrl, serverContact,
257 providerPage, providerUrl, updated);
264 Log.ReadError(TableName, e);
OR/M mapper for table timeServer.
abstract bool ShouldUseDns
abstract string ProviderPage
override void Update(TimeServer item)
abstract string Organization
override TimeServer FetchExternal(int id)
Fetch time server from external source.
abstract string AccessDetails
const string DatabaseNoUpdate
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
abstract string AccessPolicy
abstract bool IsPoolMember
static TimeServerWebAdapter Create(LogBase log)
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
TimeServerDatabaseMapper(LogBase log)
abstract string V6Address
override void ReadContent()
override void Insert(TimeServer item)
abstract IPAddress Address
abstract DateTime Updated
abstract string DisplayLocation
int Id
Gets the identifier.
abstract bool ShouldNotify
abstract string ProviderUrl
abstract string ServiceArea