23 using System.Diagnostics.CodeAnalysis;
25 using MySql.Data.MySqlClient;
27 namespace Ntp.Data.Provider
35 private const string CreateDatabaseSql =
36 "CREATE DATABASE IF NOT EXISTS {0};";
38 private const string CheckTableSql =
39 "SELECT table_name " +
40 "FROM information_schema.tables " +
41 "WHERE table_schema = '{0}' " +
42 "AND table_name = '{1}';";
46 return new MySqlCommand();
51 return new MySqlConnection(BuildConnectionString());
54 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
57 var connection = CreateGenericConnection();
59 var command = Instance.CreateCommand();
60 command.Connection = connection;
61 command.CommandText =
string.Format(CreateDatabaseSql, Config.Name);
68 return new MySqlConnection(BuildConnectionString(
false));
73 return new MySqlParameter(name, value);
76 public override string DateAddMinutes(
string dateColumn,
string minuteColumn)
78 return $
"{dateColumn} - interval {minuteColumn} minute";
83 return PrepareSql(
string.Format(CheckTableSql, Config.Name, table));
88 return PrepareSql(
string.Format(sql,
"INT NOT NULL AUTO_INCREMENT",
" ENGINE=INNODB"));
93 return PrepareSql(
string.Format(sql,
"SELECT LAST_INSERT_ID()"));
98 return sql.Replace(
"[",
string.Empty).Replace(
"]",
string.Empty);
103 if (Config.ConnectionString != null)
104 return Config.ConnectionString;
106 var
b =
new StringBuilder();
107 b.Append($
"Server={Config.Host};");
109 if (Config.Port != null)
110 b.Append($
"Port={Config.Port};");
113 b.Append($
"Database={Config.Name};");
115 b.Append($
"Uid={Config.User};");
116 b.Append($
"Pwd={Config.Pass};");
118 if (Config.EnableSsl)
119 b.Append(
@"SSL Mode=Required;");
121 if (Config.CertificateFile != null)
122 b.Append($
"CertificateFile={Config.CertificateFile};");
124 if (Config.CertificatePassword != null)
125 b.Append($
"CertificatePassword={Config.CertificatePassword};");
127 if (Config.ConnectionTimeout.HasValue)
128 b.Append($
"Connection Timeout={Config.ConnectionTimeout.Value};");
override string PrepareCheckTableSql(string table)
override string PrepareInsertSql(string sql)
override IDbConnection CreateConnection()
override string PrepareCreateTableSql(string sql)
override IDbConnection CreateGenericConnection()
override void CreateDatabase()
static string BuildConnectionString(bool includeName=true)
override string PrepareSql(string sql)
override IDbCommand CreateCommand()
override string DateAddMinutes(string dateColumn, string minuteColumn)
override IDbDataParameter CreateParameter(string name, object value)