2016-08-06 16:19:35 +02:00
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>
|
|
|
|
|
//
|
2016-04-28 19:29:22 +02:00
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2016-08-06 16:19:35 +02:00
|
|
|
|
//
|
2016-04-28 19:29:22 +02:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2016-08-06 16:19:35 +02:00
|
|
|
|
//
|
2016-04-28 19:29:22 +02:00
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
2016-08-11 19:32:27 +02:00
|
|
|
|
using System.Data;
|
2016-04-28 19:29:22 +02:00
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
#if MYSQL
|
2016-04-28 19:29:22 +02:00
|
|
|
|
using System.Data.Common;
|
2016-05-22 23:40:47 +02:00
|
|
|
|
using System.Text;
|
2016-04-28 19:29:22 +02:00
|
|
|
|
using MySql.Data.MySqlClient;
|
|
|
|
|
|
|
|
|
|
namespace Ntp.Data.Provider
|
|
|
|
|
{
|
|
|
|
|
public sealed class MySqlFactory : SqlDatabaseFactory
|
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
internal MySqlFactory()
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
private const string CreateDatabaseSql =
|
2016-04-28 19:29:22 +02:00
|
|
|
|
"CREATE DATABASE IF NOT EXISTS {0};";
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
private const string CheckTableSql =
|
2016-04-28 19:29:22 +02:00
|
|
|
|
"SELECT table_name " +
|
|
|
|
|
"FROM information_schema.tables " +
|
|
|
|
|
"WHERE table_schema = '{0}' " +
|
|
|
|
|
"AND table_name = '{1}';";
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
public override void CreateDatabase()
|
|
|
|
|
{
|
2016-08-11 19:32:27 +02:00
|
|
|
|
IDbConnection connection = CreateGenericConnection();
|
2016-05-22 23:40:47 +02:00
|
|
|
|
connection.Open();
|
2016-08-11 19:32:27 +02:00
|
|
|
|
IDbCommand command = Instance.CreateCommand();
|
2016-05-22 23:40:47 +02:00
|
|
|
|
command.Connection = connection;
|
2016-08-06 16:19:35 +02:00
|
|
|
|
command.CommandText = string.Format(CreateDatabaseSql, Config.Name);
|
2016-05-22 23:40:47 +02:00
|
|
|
|
command.Prepare();
|
|
|
|
|
connection.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string PrepareSql(string sql)
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-08-06 16:19:35 +02:00
|
|
|
|
return sql.Replace("[", string.Empty).Replace("]", string.Empty);
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
public override string PrepareCheckTableSql(string table)
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-08-06 16:19:35 +02:00
|
|
|
|
return PrepareSql(string.Format(CheckTableSql, Config.Name, table));
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
public override string PrepareCreateTableSql(string sql)
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-08-06 16:19:35 +02:00
|
|
|
|
return PrepareSql(string.Format(sql, "INT NOT NULL AUTO_INCREMENT", " ENGINE=INNODB"));
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
public override string PrepareInsertSql(string sql)
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-08-06 16:19:35 +02:00
|
|
|
|
return PrepareSql(string.Format(sql, "SELECT LAST_INSERT_ID()"));
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 19:32:27 +02:00
|
|
|
|
public override IDbConnection CreateGenericConnection()
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
return new MySqlConnection(BuildConnectionString(false));
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 19:32:27 +02:00
|
|
|
|
public override IDbConnection CreateConnection()
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
return new MySqlConnection(BuildConnectionString());
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
private string BuildConnectionString(bool includeName = true)
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
var b = new StringBuilder();
|
|
|
|
|
b.AppendFormat("Server={0};", Config.Host);
|
2016-04-28 19:29:22 +02:00
|
|
|
|
|
|
|
|
|
if (Config.Port != null)
|
2016-05-22 23:40:47 +02:00
|
|
|
|
b.AppendFormat("Port={0};", Config.Port);
|
2016-04-28 19:29:22 +02:00
|
|
|
|
|
|
|
|
|
if (includeName)
|
2016-05-22 23:40:47 +02:00
|
|
|
|
b.AppendFormat("Database={0};", Config.Name);
|
2016-04-28 19:29:22 +02:00
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
b.AppendFormat("Uid={0};", Config.User);
|
|
|
|
|
b.AppendFormat("Pwd={0};", Config.Pass);
|
2016-04-28 19:29:22 +02:00
|
|
|
|
|
|
|
|
|
// TODO: Inlcude SSL options
|
|
|
|
|
//SSL Mode=Required;CertificateFile=C:\folder\client.pfx;CertificatePassword=pass;
|
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
return b.ToString();
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 19:32:27 +02:00
|
|
|
|
public override IDbCommand CreateCommand()
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
return new MySqlCommand();
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 19:32:27 +02:00
|
|
|
|
public override IDbDataParameter CreateParameter()
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
return new MySqlParameter();
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 19:32:27 +02:00
|
|
|
|
public override IDbDataParameter CreateParameter(string name, object value)
|
2016-04-28 19:29:22 +02:00
|
|
|
|
{
|
2016-05-22 23:40:47 +02:00
|
|
|
|
return new MySqlParameter(name, value);
|
2016-04-28 19:29:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-22 23:40:47 +02:00
|
|
|
|
}
|
2016-08-06 16:19:35 +02:00
|
|
|
|
|
2016-05-22 23:40:47 +02:00
|
|
|
|
#endif
|