NTP Analyzer  0.8.2
Analyze the operation of time servers
MySqlFactory.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.Data;
23 using System.Diagnostics.CodeAnalysis;
24 using System.Text;
25 using MySql.Data.MySqlClient;
26 
27 namespace Ntp.Data.Provider
28 {
29  public sealed class MySqlFactory : SqlDatabaseFactory
30  {
31  internal MySqlFactory()
32  {
33  }
34 
35  private const string CreateDatabaseSql =
36  "CREATE DATABASE IF NOT EXISTS {0};";
37 
38  private const string CheckTableSql =
39  "SELECT table_name " +
40  "FROM information_schema.tables " +
41  "WHERE table_schema = '{0}' " +
42  "AND table_name = '{1}';";
43 
44  public override IDbCommand CreateCommand()
45  {
46  return new MySqlCommand();
47  }
48 
49  public override IDbConnection CreateConnection()
50  {
51  return new MySqlConnection(BuildConnectionString());
52  }
53 
54  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
55  public override void CreateDatabase()
56  {
57  var connection = CreateGenericConnection();
58  connection.Open();
59  var command = Instance.CreateCommand();
60  command.Connection = connection;
61  command.CommandText = string.Format(CreateDatabaseSql, Config.Name);
62  command.Prepare();
63  connection.Close();
64  }
65 
66  public override IDbConnection CreateGenericConnection()
67  {
68  return new MySqlConnection(BuildConnectionString(false));
69  }
70 
71  public override IDbDataParameter CreateParameter(string name, object value)
72  {
73  return new MySqlParameter(name, value);
74  }
75 
76  public override string DateAddMinutes(string dateColumn, string minuteColumn)
77  {
78  return $"{dateColumn} - interval {minuteColumn} minute";
79  }
80 
81  public override string PrepareCheckTableSql(string table)
82  {
83  return PrepareSql(string.Format(CheckTableSql, Config.Name, table));
84  }
85 
86  public override string PrepareCreateTableSql(string sql)
87  {
88  return PrepareSql(string.Format(sql, "INT NOT NULL AUTO_INCREMENT", " ENGINE=INNODB"));
89  }
90 
91  public override string PrepareInsertSql(string sql)
92  {
93  return PrepareSql(string.Format(sql, "SELECT LAST_INSERT_ID()"));
94  }
95 
96  public override string PrepareSql(string sql)
97  {
98  return sql.Replace("[", string.Empty).Replace("]", string.Empty);
99  }
100 
101  private static string BuildConnectionString(bool includeName = true)
102  {
103  if (Config.ConnectionString != null)
104  return Config.ConnectionString;
105 
106  var b = new StringBuilder();
107  b.Append($"Server={Config.Host};");
108 
109  if (Config.Port != null)
110  b.Append($"Port={Config.Port};");
111 
112  if (includeName)
113  b.Append($"Database={Config.Name};");
114 
115  b.Append($"Uid={Config.User};");
116  b.Append($"Pwd={Config.Pass};");
117 
118  if (Config.EnableSsl)
119  b.Append(@"SSL Mode=Required;");
120 
121  if (Config.CertificateFile != null)
122  b.Append($"CertificateFile={Config.CertificateFile};");
123 
124  if (Config.CertificatePassword != null)
125  b.Append($"CertificatePassword={Config.CertificatePassword};");
126 
127  if (Config.ConnectionTimeout.HasValue)
128  b.Append($"Connection Timeout={Config.ConnectionTimeout.Value};");
129 
130  return b.ToString();
131  }
132  }
133 }
override string PrepareCheckTableSql(string table)
Definition: MySqlFactory.cs:81
override string PrepareInsertSql(string sql)
Definition: MySqlFactory.cs:91
override IDbConnection CreateConnection()
Definition: MySqlFactory.cs:49
override string PrepareCreateTableSql(string sql)
Definition: MySqlFactory.cs:86
override IDbConnection CreateGenericConnection()
Definition: MySqlFactory.cs:66
override void CreateDatabase()
Definition: MySqlFactory.cs:55
static string BuildConnectionString(bool includeName=true)
override string PrepareSql(string sql)
Definition: MySqlFactory.cs:96
override IDbCommand CreateCommand()
Definition: MySqlFactory.cs:44
override string DateAddMinutes(string dateColumn, string minuteColumn)
Definition: MySqlFactory.cs:76
override IDbDataParameter CreateParameter(string name, object value)
Definition: MySqlFactory.cs:71
var b
Definition: bootstrap.min.js:6