23 using System.Diagnostics.CodeAnalysis;
29 namespace Ntp.Analyzer.Data.Sql
42 this.hostMapper = hostMapper;
43 this.peerMapper = peerMapper;
46 private const string SelectSql =
47 "SELECT id, lastActive, zone, hide, hostId, peerId FROM peerActivity";
49 private const string InsertSql =
50 "INSERT INTO peerActivity( lastActive, zone, hide, hostId, peerId ) " +
51 "VALUES( @lastActive, @zone, @hide, @hostId, @peerId );{0};";
53 private const string UpdateSql =
54 "UPDATE peerActivity SET lastActive = @lastActive, zone = @zone WHERE id = @id";
59 protected override bool UseCache =>
true;
61 protected override string TableName =>
"peerActivity";
63 protected override string CreateSql =>
"CREATE TABLE peerActivity ( " +
64 " id {0} PRIMARY KEY, " +
65 " lastActive TIMESTAMP NOT NULL, " +
66 " zone INT NOT NULL, " +
67 " hide BIT(1) NOT NULL, " +
68 " hostId INT NOT NULL, " +
69 " peerId INT NOT NULL, " +
70 " UNIQUE KEY (hostId, peerId), " +
71 " FOREIGN KEY (hostId) REFERENCES host(id), " +
72 " FOREIGN KEY (peerId) REFERENCES peer(id) " +
75 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
83 Command.CommandText = PrepareInsertSql(InsertSql);
84 Command.Parameters.Add(CreateParameter(
"@lastActive", item.
LastActive));
85 Command.Parameters.Add(CreateParameter(
"@zone", item.
UtcOffset));
86 Command.Parameters.Add(CreateParameter(
"@hide", item.
Hide));
87 Command.Parameters.Add(CreateParameter(
"@hostId", item.
Host.
Id));
88 Command.Parameters.Add(CreateParameter(
"@peerId", item.
Peer.
Id));
90 Log.SqlExecute(Command.CommandText, Command.Parameters);
91 var idObject = Command.ExecuteScalar();
92 item.
SetId(Convert.ToInt32(idObject));
96 Log.InsertError(TableName, e);
105 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
111 Command.CommandText = PrepareSql(SelectSql);
112 Log.SqlExecute(Command.CommandText);
113 Reader = Command.ExecuteReader();
115 while (Reader.Read())
117 int id = Convert.ToInt32(Reader[
"id"]);
118 var time = Convert.ToDateTime(Reader[
"lastActive"]);
119 int zone = Convert.ToInt32(Reader[
"zone"]);
120 bool hide = Convert.ToBoolean(Reader[
"hide"]);
121 int hostId = Convert.ToInt32(Reader[
"hostId"]);
122 var host = hostMapper[hostId];
123 int peerId = Convert.ToInt32(Reader[
"peerId"]);
124 var peer = peerMapper[peerId];
125 var item =
new PeerActivity(
id, peer, host, time, zone, hide);
131 Log.UpdateError(TableName, e);
139 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
147 Command.CommandText = PrepareSql(UpdateSql);
148 Command.Parameters.Add(CreateParameter(
"@lastActive", item.
LastActive));
149 Command.Parameters.Add(CreateParameter(
"@zone", item.
UtcOffset));
150 Command.Parameters.Add(CreateParameter(
"@id", item.
Id));
152 Log.SqlExecute(Command.CommandText, Command.Parameters);
153 Command.ExecuteNonQuery();
157 Log.UpdateError(TableName, e);
OR/M mapper for table host.
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
override void Update(PeerActivity item)
OR/M mapper for table peer.
readonly PeerDatabaseMapper peerMapper
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
PeerActivityDatabaseMapper(HostDatabaseMapper hostMapper, PeerDatabaseMapper peerMapper, LogBase log)
OR/M mapper for table peerActivity.
readonly HostDatabaseMapper hostMapper
int Id
Gets the identifier.
override void ReadContent()
override void Insert(PeerActivity item)