MySQLConnector refactored; now it works with any SQL database if there is an appropriate DbConnection implementation

main
Inga 🏳‍🌈 15 years ago
parent 0a064c5f05
commit 56262d7896
  1. 2
      Builder/IISMainHandler/build.txt
  2. 2
      Common/Config.cs
  3. 46
      MySQLConnector/Connection.cs
  4. 8
      MySQLConnector/Extensions.cs
  5. 15
      MySQLConnector/IDBProvider.cs
  6. 2
      MySQLConnector/MySQLConnector.csproj
  7. 22
      MySQLConnector/MySQLProvider.cs
  8. 5
      MySQLConnector/Transaction.cs

@ -18,7 +18,7 @@ namespace FLocal.Common {
protected Config(NameValueCollection data) : base(data) { protected Config(NameValueCollection data) : base(data) {
this.InitTime = DateTime.Now.ToLongTimeString(); 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.dataDir = data["DataDir"];
this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString(); this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString();
} }

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Data.Common;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using FLocal.Core; using FLocal.Core;
using FLocal.Core.DB; using FLocal.Core.DB;
@ -10,27 +11,30 @@ namespace FLocal.MySQLConnector {
public class Connection : IDBConnection { public class Connection : IDBConnection {
private MySqlConnection connection; private IDBProvider provider;
private DbConnection connection;
private string connectionString; private string connectionString;
private HashSet<Transaction> transactions; private HashSet<Transaction> transactions;
public Connection(string connectionString) { public Connection(IDBProvider provider, string connectionString) {
this.connection = new MySqlConnection(connectionString); this.provider = provider;
this.connection.Open();
this.connectionString = connectionString; this.connectionString = connectionString;
this.connection = this.provider.createConnection(this.connectionString);
this.connection.Open();
this.transactions = new HashSet<Transaction>(); this.transactions = new HashSet<Transaction>();
} }
internal MySqlConnection createConnection() { internal DbConnection createConnection() {
MySqlConnection connection = new MySqlConnection(this.connectionString); DbConnection connection = this.provider.createConnection(this.connectionString);
connection.Open(); connection.Open();
return connection; return connection;
} }
public List<Dictionary<string, string>> LoadByIds(ITableSpec table, List<string> ids) { public List<Dictionary<string, string>> LoadByIds(ITableSpec table, List<string> ids) {
lock(this) { lock(this) {
using(MySqlCommand command = this.connection.CreateCommand()) { using(DbCommand command = this.connection.CreateCommand()) {
command.CommandType = System.Data.CommandType.Text; command.CommandType = System.Data.CommandType.Text;
ParamsHolder paramsHolder = new ParamsHolder(); 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.CommandText = "SELECT * FROM " + table.compile() + " WHERE " + table.getIdSpec().compile() + " IN (" + string.Join(", ", placeholder.ToArray()) + ")";
command.Prepare(); command.Prepare();
foreach(KeyValuePair<string, string> kvp in paramsHolder.data) { foreach(KeyValuePair<string, string> kvp in paramsHolder.data) {
command.Parameters.AddWithValue(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
} }
Dictionary<string, Dictionary<string, string>> rawResult = new Dictionary<string, Dictionary<string, string>>(); Dictionary<string, Dictionary<string, string>> rawResult = new Dictionary<string, Dictionary<string, string>>();
using(MySqlDataReader reader = command.ExecuteReader()) { using(DbDataReader reader = command.ExecuteReader()) {
while(reader.Read()) { while(reader.Read()) {
Dictionary<string, string> row = new Dictionary<string,string>(); Dictionary<string, string> row = new Dictionary<string,string>();
for(int i=0; i<reader.FieldCount; i++) { for(int i=0; i<reader.FieldCount; i++) {
@ -69,7 +73,7 @@ namespace FLocal.MySQLConnector {
public List<string> LoadIdsByConditions(ITableSpec table, FLocal.Core.DB.conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts) { public List<string> LoadIdsByConditions(ITableSpec table, FLocal.Core.DB.conditions.AbstractCondition conditions, Diapasone diapasone, JoinSpec[] joins, SortSpec[] sorts) {
lock(this) { lock(this) {
using(MySqlCommand command = this.connection.CreateCommand()) { using(DbCommand command = this.connection.CreateCommand()) {
command.CommandType = System.Data.CommandType.Text; command.CommandType = System.Data.CommandType.Text;
@ -86,7 +90,7 @@ namespace FLocal.MySQLConnector {
string queryMain = "FROM " + table.compile() + " " + queryJoins + " WHERE " + queryConditions; string queryMain = "FROM " + table.compile() + " " + queryJoins + " WHERE " + queryConditions;
foreach(KeyValuePair<string, string> kvp in paramsHolder.data) { foreach(KeyValuePair<string, string> kvp in paramsHolder.data) {
command.Parameters.AddWithValue(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
} }
command.CommandText = "SELECT COUNT(*) " + queryMain; command.CommandText = "SELECT COUNT(*) " + queryMain;
@ -104,7 +108,7 @@ namespace FLocal.MySQLConnector {
command.CommandText = "SELECT " + table.compile() + ".* " + queryMain + " " + querySorts + " " + queryLimits; command.CommandText = "SELECT " + table.compile() + ".* " + queryMain + " " + querySorts + " " + queryLimits;
List<string> result = new List<string>(); List<string> result = new List<string>();
using(MySqlDataReader reader = command.ExecuteReader()) { using(DbDataReader reader = command.ExecuteReader()) {
while(reader.Read()) { while(reader.Read()) {
result.Add(reader.GetString(0)); result.Add(reader.GetString(0));
} }
@ -131,7 +135,7 @@ namespace FLocal.MySQLConnector {
public void lockTable(FLocal.Core.DB.Transaction _transaction, ITableSpec table) { public void lockTable(FLocal.Core.DB.Transaction _transaction, ITableSpec table) {
Transaction transaction = (Transaction)_transaction; Transaction transaction = (Transaction)_transaction;
lock(transaction) { lock(transaction) {
using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { using(DbCommand command = transaction.sqlconnection.CreateCommand()) {
command.Transaction = transaction.sqltransaction; command.Transaction = transaction.sqltransaction;
command.CommandType = System.Data.CommandType.Text; command.CommandType = System.Data.CommandType.Text;
command.CommandText = "LOCK TABLES `" + MySqlHelper.EscapeString(table.name) + "`"; 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) { public void lockRow(FLocal.Core.DB.Transaction _transaction, ITableSpec table, string id) {
Transaction transaction = (Transaction)_transaction; Transaction transaction = (Transaction)_transaction;
lock(transaction) { lock(transaction) {
using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { using(DbCommand command = transaction.sqlconnection.CreateCommand()) {
command.Transaction = transaction.sqltransaction; command.Transaction = transaction.sqltransaction;
command.CommandType = System.Data.CommandType.Text; command.CommandType = System.Data.CommandType.Text;
command.CommandText = "SELECT * FROM `" + MySqlHelper.EscapeString(table.name) + "` where `" + MySqlHelper.EscapeString(table.idName) + "` = @id FOR UPDATE"; 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(); command.ExecuteNonQuery();
} }
} }
@ -156,7 +160,7 @@ namespace FLocal.MySQLConnector {
public void update(FLocal.Core.DB.Transaction _transaction, ITableSpec table, string id, Dictionary<string, string> data) { public void update(FLocal.Core.DB.Transaction _transaction, ITableSpec table, string id, Dictionary<string, string> data) {
Transaction transaction = (Transaction)_transaction; Transaction transaction = (Transaction)_transaction;
lock(transaction) { lock(transaction) {
using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { using(DbCommand command = transaction.sqlconnection.CreateCommand()) {
List<string> updates = new List<string>(); List<string> updates = new List<string>();
ParamsHolder paramsholder = new ParamsHolder(); ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> kvp in data) { foreach(KeyValuePair<string, string> kvp in data) {
@ -167,9 +171,9 @@ namespace FLocal.MySQLConnector {
command.Transaction = transaction.sqltransaction; command.Transaction = transaction.sqltransaction;
command.CommandType = System.Data.CommandType.Text; 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.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<string, string> kvp in paramsholder.data) { foreach(KeyValuePair<string, string> kvp in paramsholder.data) {
command.Parameters.AddWithValue(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
} }
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
@ -179,7 +183,7 @@ namespace FLocal.MySQLConnector {
public string insert(FLocal.Core.DB.Transaction _transaction, ITableSpec table, Dictionary<string, string> data) { public string insert(FLocal.Core.DB.Transaction _transaction, ITableSpec table, Dictionary<string, string> data) {
Transaction transaction = (Transaction)_transaction; Transaction transaction = (Transaction)_transaction;
lock(transaction) { lock(transaction) {
using(MySqlCommand command = transaction.sqlconnection.CreateCommand()) { using(DbCommand command = transaction.sqlconnection.CreateCommand()) {
List<string> updates = new List<string>(); List<string> updates = new List<string>();
ParamsHolder paramsholder = new ParamsHolder(); ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> kvp in data) { foreach(KeyValuePair<string, string> kvp in data) {
@ -191,10 +195,10 @@ namespace FLocal.MySQLConnector {
command.CommandType = System.Data.CommandType.Text; command.CommandType = System.Data.CommandType.Text;
command.CommandText = "INSERT INTO `" + MySqlHelper.EscapeString(table.name) + "` SET " + String.Join(", ", updates.ToArray()); command.CommandText = "INSERT INTO `" + MySqlHelper.EscapeString(table.name) + "` SET " + String.Join(", ", updates.ToArray());
foreach(KeyValuePair<string, string> kvp in paramsholder.data) { foreach(KeyValuePair<string, string> kvp in paramsholder.data) {
command.Parameters.AddWithValue(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
} }
command.ExecuteNonQuery(); command.ExecuteNonQuery();
return command.LastInsertedId.ToString(); return this.provider.LastInsertId(command).ToString();
} }
} }
} }

@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using FLocal.Core.DB; using FLocal.Core.DB;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using System.Data.Common;
namespace FLocal.MySQLConnector { namespace FLocal.MySQLConnector {
static class Extensions { static class Extensions {
@ -15,5 +16,12 @@ namespace FLocal.MySQLConnector {
public static string compile(this ColumnSpec column) { public static string compile(this ColumnSpec column) {
return column.table.compile() + ".`" + MySqlHelper.EscapeString(column.name) + "`"; 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);
}
} }
} }

@ -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);
}
}

@ -48,6 +48,8 @@
<Compile Include="ConditionCompiler.cs" /> <Compile Include="ConditionCompiler.cs" />
<Compile Include="Connection.cs" /> <Compile Include="Connection.cs" />
<Compile Include="Extensions.cs" /> <Compile Include="Extensions.cs" />
<Compile Include="IDBProvider.cs" />
<Compile Include="MySQLProvider.cs" />
<Compile Include="ParamsHolder.cs" /> <Compile Include="ParamsHolder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" /> <Compile Include="Transaction.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;
}
}
}

@ -4,12 +4,13 @@ using System.Linq;
using System.Text; using System.Text;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using FLocal.Core; using FLocal.Core;
using System.Data.Common;
namespace FLocal.MySQLConnector { namespace FLocal.MySQLConnector {
class Transaction : Core.DB.Transaction { class Transaction : Core.DB.Transaction {
internal MySqlConnection sqlconnection; internal DbConnection sqlconnection;
internal MySqlTransaction sqltransaction; internal DbTransaction sqltransaction;
public bool finalizedImpl { public bool finalizedImpl {
get; get;

Loading…
Cancel
Save