NTP Analyzer  0.8.2
Analyze the operation of time servers
HostDatabaseMapper.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 HostDatabaseMapper : SqlDatabaseMapper<Host>
35  {
37  : base(log)
38  {
39  }
40 
41  private const string SelectSql =
42  "SELECT id, name, ip, orgId FROM host;";
43 
44  private const string InsertSql =
45  "INSERT INTO host( id, name, ip, orgId ) VALUES( @id, @name, @ip, @orgId );";
46 
47  private const string UpdateSql =
48  "UPDATE host SET name = @name, ip = @ip, orgId = @orgId WHERE id = @id";
49 
50  protected override bool UseCache => true;
51 
52  protected override string TableName => "host";
53 
54  protected override string CreateSql => "CREATE TABLE host ( " +
55  " id INT NOT NULL PRIMARY KEY, " +
56  " name VARCHAR(255) NOT NULL, " +
57  " ip VARCHAR(45) NOT NULL, " +
58  " orgId INT " +
59  "){1};";
60 
61  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
62  protected override void Insert(Host item)
63  {
64  lock (MapperLocker)
65  {
66  try
67  {
68  Open();
69  Command.CommandText = PrepareInsertSql(InsertSql);
70  Command.Parameters.Add(CreateParameter("@id", item.Id));
71  Command.Parameters.Add(CreateParameter("@name", item.Name));
72  Command.Parameters.Add(CreateParameter("@ip", item.Ip));
73  Command.Parameters.Add(CreateParameter("@orgId", item.OrgId));
74  Command.Prepare();
75  Log.SqlExecute(Command.CommandText, Command.Parameters);
76  Command.ExecuteNonQuery();
77  }
78  catch (Exception e)
79  {
80  Log.InsertError(TableName, e);
81  }
82  finally
83  {
84  Close();
85  }
86  }
87  }
88 
89  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
90  protected override void ReadContent()
91  {
92  try
93  {
94  Open();
95  Command.CommandText = PrepareSql(SelectSql);
96  Log.SqlExecute(Command.CommandText);
97  Reader = Command.ExecuteReader();
98 
99  while (Reader.Read())
100  {
101  int id = Convert.ToInt32(Reader["id"]);
102  string name = Reader["name"].ToString();
103  string ip = Reader["ip"].ToString();
104  var orgId = Reader["orgId"] != DBNull.Value
105  ? Convert.ToInt32(Reader["orgId"])
106  : (int?) null;
107  var host = new Host(id, name, ip, orgId, false);
108  AddItem(host);
109  }
110  }
111  catch (Exception e)
112  {
113  Log.ReadError(TableName, e);
114  }
115  finally
116  {
117  Close();
118  }
119  }
120 
121  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
122  protected override void Update(Host item)
123  {
124  lock (MapperLocker)
125  {
126  try
127  {
128  Open();
129  Command.CommandText = PrepareSql(UpdateSql);
130  Command.Parameters.Add(CreateParameter("@name", item.Name));
131  Command.Parameters.Add(CreateParameter("@ip", item.Ip));
132  Command.Parameters.Add(CreateParameter("@orgId", item.OrgId));
133  Command.Parameters.Add(CreateParameter("@id", item.Id));
134  Command.Prepare();
135  Log.SqlExecute(Command.CommandText, Command.Parameters);
136  Command.ExecuteScalar();
137  }
138  catch (Exception e)
139  {
140  Log.UpdateError(TableName, e);
141  }
142  finally
143  {
144  Close();
145  }
146  }
147  }
148  }
149 }
var e
Definition: bootstrap.min.js:6
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.