diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index a76c74d..aa92725 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -75 \ No newline at end of file +76 \ No newline at end of file diff --git a/Common/Config.cs b/Common/Config.cs index b5776d4..0770f1b 100644 --- a/Common/Config.cs +++ b/Common/Config.cs @@ -18,7 +18,7 @@ namespace FLocal.Common { protected Config(NameValueCollection data) : base(data) { this.InitTime = DateTime.Now.ToLongTimeString(); - this.mainConnection = new MySQLConnector.Connection(data["ConnectionString"]); + this.mainConnection = new MySQLConnector.Connection(MySQLConnector.MySQLProvider.instance, data["ConnectionString"]); this.dataDir = data["DataDir"]; this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString(); } diff --git a/MySQLConnector/Connection.cs b/MySQLConnector/Connection.cs index 70e2090..5255d0d 100644 --- a/MySQLConnector/Connection.cs +++ b/MySQLConnector/Connection.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Data.Common; using MySql.Data.MySqlClient; using FLocal.Core; using FLocal.Core.DB; @@ -10,27 +11,30 @@ namespace FLocal.MySQLConnector { public class Connection : IDBConnection { - private MySqlConnection connection; + private IDBProvider provider; + + private DbConnection connection; private string connectionString; private HashSet transactions; - public Connection(string connectionString) { - this.connection = new MySqlConnection(connectionString); - this.connection.Open(); + public Connection(IDBProvider provider, string connectionString) { + this.provider = provider; this.connectionString = connectionString; + this.connection = this.provider.createConnection(this.connectionString); + this.connection.Open(); this.transactions = new HashSet(); } - internal MySqlConnection createConnection() { - MySqlConnection connection = new MySqlConnection(this.connectionString); + internal DbConnection createConnection() { + DbConnection connection = this.provider.createConnection(this.connectionString); connection.Open(); return connection; } public List> LoadByIds(ITableSpec table, List ids) { lock(this) { - using(MySqlCommand command = this.connection.CreateCommand()) { + using(DbCommand command = this.connection.CreateCommand()) { command.CommandType = System.Data.CommandType.Text; ParamsHolder paramsHolder = new ParamsHolder(); @@ -42,11 +46,11 @@ namespace FLocal.MySQLConnector { command.CommandText = "SELECT * FROM " + table.compile() + " WHERE " + table.getIdSpec().compile() + " IN (" + string.Join(", ", placeholder.ToArray()) + ")"; command.Prepare(); foreach(KeyValuePair kvp in paramsHolder.data) { - command.Parameters.AddWithValue(kvp.Key, kvp.Value); + command.AddParameter(kvp.Key, kvp.Value); } Dictionary> rawResult = new Dictionary>(); - using(MySqlDataReader reader = command.ExecuteReader()) { + using(DbDataReader reader = command.ExecuteReader()) { while(reader.Read()) { Dictionary row = new Dictionary(); for(int i=0; i LoadIdsByConditions(ITableSpec table, FLocal.Core.DB.conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts) { lock(this) { - using(MySqlCommand command = this.connection.CreateCommand()) { + using(DbCommand command = this.connection.CreateCommand()) { command.CommandType = System.Data.CommandType.Text; @@ -86,7 +90,7 @@ namespace FLocal.MySQLConnector { string queryMain = "FROM " + table.compile() + " " + queryJoins + " WHERE " + queryConditions; foreach(KeyValuePair kvp in paramsHolder.data) { - command.Parameters.AddWithValue(kvp.Key, kvp.Value); + command.AddParameter(kvp.Key, kvp.Value); } command.CommandText = "SELECT COUNT(*) " + queryMain; @@ -104,7 +108,7 @@ namespace FLocal.MySQLConnector { command.CommandText = "SELECT " + table.compile() + ".* " + queryMain + " " + querySorts + " " + queryLimits; List result = new List(); - using(MySqlDataReader reader = command.ExecuteReader()) { + using(DbDataReader reader = command.ExecuteReader()) { while(reader.Read()) { result.Add(reader.GetString(0)); } @@ -131,7 +135,7 @@ namespace FLocal.MySQLConnector { public void lockTable(FLocal.Core.DB.Transaction _transaction, ITableSpec table) { Transaction transaction = (Transaction)_transaction; lock(transaction) { - using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { + using(DbCommand command = transaction.sqlconnection.CreateCommand()) { command.Transaction = transaction.sqltransaction; command.CommandType = System.Data.CommandType.Text; command.CommandText = "LOCK TABLES `" + MySqlHelper.EscapeString(table.name) + "`"; @@ -143,11 +147,11 @@ namespace FLocal.MySQLConnector { public void lockRow(FLocal.Core.DB.Transaction _transaction, ITableSpec table, string id) { Transaction transaction = (Transaction)_transaction; lock(transaction) { - using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { + using(DbCommand command = transaction.sqlconnection.CreateCommand()) { command.Transaction = transaction.sqltransaction; command.CommandType = System.Data.CommandType.Text; command.CommandText = "SELECT * FROM `" + MySqlHelper.EscapeString(table.name) + "` where `" + MySqlHelper.EscapeString(table.idName) + "` = @id FOR UPDATE"; - command.Parameters.Add(new MySqlParameter("id", id)); + command.AddParameter("id", id); command.ExecuteNonQuery(); } } @@ -156,7 +160,7 @@ namespace FLocal.MySQLConnector { public void update(FLocal.Core.DB.Transaction _transaction, ITableSpec table, string id, Dictionary data) { Transaction transaction = (Transaction)_transaction; lock(transaction) { - using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { + using(DbCommand command = transaction.sqlconnection.CreateCommand()) { List updates = new List(); ParamsHolder paramsholder = new ParamsHolder(); foreach(KeyValuePair kvp in data) { @@ -167,9 +171,9 @@ namespace FLocal.MySQLConnector { command.Transaction = transaction.sqltransaction; command.CommandType = System.Data.CommandType.Text; command.CommandText = "UPDATE `" + MySqlHelper.EscapeString(table.name) + "` set " + String.Join(", ", updates.ToArray()) + " where `" + MySqlHelper.EscapeString(table.idName) + "` = @id"; - command.Parameters.AddWithValue("id", id); + command.AddParameter("id", id); foreach(KeyValuePair kvp in paramsholder.data) { - command.Parameters.AddWithValue(kvp.Key, kvp.Value); + command.AddParameter(kvp.Key, kvp.Value); } command.ExecuteNonQuery(); } @@ -179,7 +183,7 @@ namespace FLocal.MySQLConnector { public string insert(FLocal.Core.DB.Transaction _transaction, ITableSpec table, Dictionary data) { Transaction transaction = (Transaction)_transaction; lock(transaction) { - using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { + using(DbCommand command = transaction.sqlconnection.CreateCommand()) { List updates = new List(); ParamsHolder paramsholder = new ParamsHolder(); foreach(KeyValuePair kvp in data) { @@ -191,10 +195,10 @@ namespace FLocal.MySQLConnector { command.CommandType = System.Data.CommandType.Text; command.CommandText = "INSERT INTO `" + MySqlHelper.EscapeString(table.name) + "` SET " + String.Join(", ", updates.ToArray()); foreach(KeyValuePair kvp in paramsholder.data) { - command.Parameters.AddWithValue(kvp.Key, kvp.Value); + command.AddParameter(kvp.Key, kvp.Value); } command.ExecuteNonQuery(); - return command.LastInsertedId.ToString(); + return this.provider.LastInsertId(command).ToString(); } } } diff --git a/MySQLConnector/Extensions.cs b/MySQLConnector/Extensions.cs index 0ef884d..3549eba 100644 --- a/MySQLConnector/Extensions.cs +++ b/MySQLConnector/Extensions.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using FLocal.Core.DB; using MySql.Data.MySqlClient; +using System.Data.Common; namespace FLocal.MySQLConnector { static class Extensions { @@ -15,5 +16,12 @@ namespace FLocal.MySQLConnector { public static string compile(this ColumnSpec column) { return column.table.compile() + ".`" + MySqlHelper.EscapeString(column.name) + "`"; } + + public static void AddParameter(this DbCommand command, string name, string value) { + DbParameter parameter = command.CreateParameter(); + parameter.ParameterName = name; + parameter.Value = value; + command.Parameters.Add(parameter); + } } } diff --git a/MySQLConnector/IDBProvider.cs b/MySQLConnector/IDBProvider.cs new file mode 100644 index 0000000..ee772d7 --- /dev/null +++ b/MySQLConnector/IDBProvider.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Data.Common; + +namespace FLocal.MySQLConnector { + public interface IDBProvider { + + DbConnection createConnection(string connectionString); + + long LastInsertId(DbCommand command); + + } +} diff --git a/MySQLConnector/MySQLConnector.csproj b/MySQLConnector/MySQLConnector.csproj index e61334d..7d86fba 100644 --- a/MySQLConnector/MySQLConnector.csproj +++ b/MySQLConnector/MySQLConnector.csproj @@ -48,6 +48,8 @@ + + diff --git a/MySQLConnector/MySQLProvider.cs b/MySQLConnector/MySQLProvider.cs new file mode 100644 index 0000000..4133c0b --- /dev/null +++ b/MySQLConnector/MySQLProvider.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Data.Common; +using MySql.Data.MySqlClient; + +namespace FLocal.MySQLConnector { + public class MySQLProvider : IDBProvider { + + public static readonly MySQLProvider instance = new MySQLProvider(); + + public DbConnection createConnection(string connectionString) { + return new MySqlConnection(connectionString); + } + + public long LastInsertId(DbCommand command) { + return ((MySqlCommand)command).LastInsertedId; + } + + } +} diff --git a/MySQLConnector/Transaction.cs b/MySQLConnector/Transaction.cs index 6d4f933..11fae0a 100644 --- a/MySQLConnector/Transaction.cs +++ b/MySQLConnector/Transaction.cs @@ -4,12 +4,13 @@ using System.Linq; using System.Text; using MySql.Data.MySqlClient; using FLocal.Core; +using System.Data.Common; namespace FLocal.MySQLConnector { class Transaction : Core.DB.Transaction { - internal MySqlConnection sqlconnection; - internal MySqlTransaction sqltransaction; + internal DbConnection sqlconnection; + internal DbTransaction sqltransaction; public bool finalizedImpl { get;