NTP Analyzer  0.8.2
Analyze the operation of time servers
PeerDatabaseMapper.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Diagnostics.CodeAnalysis;
24 using Ntp.Analyzer.Data.Log;
25 using Ntp.Analyzer.Objects;
26 using Ntp.Common.Log;
27 using Ntp.Data.Log;
28 
29 namespace Ntp.Analyzer.Data.Sql
30 {
34  public sealed class PeerDatabaseMapper : SqlDatabaseMapper<Peer>
35  {
36  internal PeerDatabaseMapper(TimeServerDatabaseMapper timeServerMapper, LogBase log)
37  : base(log)
38  {
39  this.timeServerMapper = timeServerMapper;
40  }
41 
42  private const string SelectSql =
43  "SELECT id, name, ip, orgId FROM peer;";
44 
45  private const string InsertSql =
46  "INSERT INTO peer( name, ip, orgId ) VALUES( @name, @ip, @orgId );{0};";
47 
48  private const string UpdateSql =
49  "UPDATE peer SET name = @name, ip = @ip, orgId = @orgId WHERE id = @id";
50 
52 
53  protected override bool UseCache => true;
54 
55  protected override string TableName => "peer";
56 
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, " +
61  " orgId INT " +
62  "){1};";
63 
64  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
65  protected override void Insert(Peer item)
66  {
67  lock (MapperLocker)
68  {
69  try
70  {
71  Open();
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));
76  Command.Prepare();
77  Log.SqlExecute(Command.CommandText, Command.Parameters);
78  var idObject = Command.ExecuteScalar();
79  item.SetId(Convert.ToInt32(idObject));
80  }
81  catch (Exception e)
82  {
83  Log.InsertError(TableName, e);
84  }
85  finally
86  {
87  Close();
88  }
89  }
90  }
91 
92  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
93  protected override void ReadContent()
94  {
95  try
96  {
97  Open();
98  Command.CommandText = PrepareSql(SelectSql);
99  Log.SqlExecute(Command.CommandText);
100  Reader = Command.ExecuteReader();
101 
102  while (Reader.Read())
103  {
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"])
109  : (int?) null;
110 
111  var server = orgId.HasValue
112  ? timeServerMapper[orgId.Value]
113  : null;
114 
115  var peer = new Peer(id, name, ip, server);
116  AddItem(peer);
117  }
118  }
119  catch (Exception e)
120  {
121  Log.ReadError(TableName, e);
122  }
123  finally
124  {
125  Close();
126  }
127  }
128 
129  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
130  protected override void Update(Peer item)
131  {
132  lock (MapperLocker)
133  {
134  try
135  {
136  Open();
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));
142  Command.Prepare();
143  Log.SqlExecute(Command.CommandText, Command.Parameters);
144  Command.ExecuteNonQuery();
145  }
146  catch (Exception e)
147  {
148  Log.UpdateError(TableName, e);
149  }
150  finally
151  {
152  Close();
153  }
154  }
155  }
156  }
157 }
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
readonly TimeServerDatabaseMapper timeServerMapper
var e
Definition: bootstrap.min.js:6
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
int OrgId
Gets the server identifier as specified on support.ntp.org.
Definition: Peer.cs:48
PeerDatabaseMapper(TimeServerDatabaseMapper timeServerMapper, LogBase log)