From 76b97c997309acf2b15e65685761aecec081664d Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 10 Jun 2010 09:25:55 +0000 Subject: [PATCH] More abstraction in MySQL --- Builder/IISMainHandler/build.txt | 2 +- Common/Config.cs | 2 +- MySQLConnector/ConditionCompiler.cs | 20 +++++----- MySQLConnector/Connection.cs | 40 +++++++++++-------- MySQLConnector/Extensions.cs | 8 ++-- .../{IDBProvider.cs => IDBTraits.cs} | 4 +- MySQLConnector/MySQLConnector.csproj | 4 +- .../{MySQLProvider.cs => MySQLDBTraits.cs} | 8 +++- 8 files changed, 52 insertions(+), 36 deletions(-) rename MySQLConnector/{IDBProvider.cs => IDBTraits.cs} (73%) rename MySQLConnector/{MySQLProvider.cs => MySQLDBTraits.cs} (61%) diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index aa92725..780fea9 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -76 \ No newline at end of file +77 \ No newline at end of file diff --git a/Common/Config.cs b/Common/Config.cs index 0770f1b..d96d638 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(MySQLConnector.MySQLProvider.instance, data["ConnectionString"]); + this.mainConnection = new MySQLConnector.Connection(data["ConnectionString"], MySQLConnector.MySQLDBTraits.instance); this.dataDir = data["DataDir"]; this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString(); } diff --git a/MySQLConnector/ConditionCompiler.cs b/MySQLConnector/ConditionCompiler.cs index 4f2c93c..d2bfd2f 100644 --- a/MySQLConnector/ConditionCompiler.cs +++ b/MySQLConnector/ConditionCompiler.cs @@ -9,21 +9,23 @@ namespace FLocal.MySQLConnector { class ConditionCompiler { private readonly ParamsHolder paramsholder; + private readonly IDBTraits traits; - private ConditionCompiler() { + private ConditionCompiler(IDBTraits traits) { this.paramsholder = new ParamsHolder(); + this.traits = traits; } private string getName(ColumnOrValue cov) { if(cov.isColumn) { - return cov.column.compile(); + return cov.column.compile(this.traits); } else { return "@" + this.paramsholder.Add(cov.value); } } private string CompileCondition(ComparisonCondition condition) { - string left = condition.left.compile(); + string left = condition.left.compile(this.traits); string right = getName(condition.right); switch(condition.comparison) { case ComparisonType.EQUAL: @@ -44,11 +46,11 @@ namespace FLocal.MySQLConnector { } private string CompileCondition(IsNullCondition condition) { - return condition.column.compile() + " IS NULL"; + return condition.column.compile(this.traits) + " IS NULL"; } private string CompileCondition(NotIsNullCondition condition) { - return condition.column.compile() + " NOT IS NULL"; + return condition.column.compile(this.traits) + " NOT IS NULL"; } private string CompileCondition(MultiValueCondition condition) { @@ -58,9 +60,9 @@ namespace FLocal.MySQLConnector { } if(condition.inclusive) { - return condition.column.compile() + " IN (" + string.Join(", ", valueParams.ToArray()) + ")"; + return condition.column.compile(this.traits) + " IN (" + string.Join(", ", valueParams.ToArray()) + ")"; } else { - return condition.column.compile() + " IN (" + string.Join(", ", valueParams.ToArray()) + ")"; + return condition.column.compile(this.traits) + " IN (" + string.Join(", ", valueParams.ToArray()) + ")"; } } @@ -104,8 +106,8 @@ namespace FLocal.MySQLConnector { } } - public static KeyValuePair Compile(AbstractCondition condition) { - ConditionCompiler compiler = new ConditionCompiler(); + public static KeyValuePair Compile(AbstractCondition condition, IDBTraits traits) { + ConditionCompiler compiler = new ConditionCompiler(traits); string compiled = compiler.CompileCondition(condition); return new KeyValuePair(compiled, compiler.paramsholder); } diff --git a/MySQLConnector/Connection.cs b/MySQLConnector/Connection.cs index 5255d0d..f03b355 100644 --- a/MySQLConnector/Connection.cs +++ b/MySQLConnector/Connection.cs @@ -11,23 +11,23 @@ namespace FLocal.MySQLConnector { public class Connection : IDBConnection { - private IDBProvider provider; + private IDBTraits traits; private DbConnection connection; private string connectionString; private HashSet transactions; - public Connection(IDBProvider provider, string connectionString) { - this.provider = provider; + public Connection(string connectionString, IDBTraits traits) { + this.traits = traits; this.connectionString = connectionString; - this.connection = this.provider.createConnection(this.connectionString); + this.connection = this.traits.createConnection(this.connectionString); this.connection.Open(); this.transactions = new HashSet(); } internal DbConnection createConnection() { - DbConnection connection = this.provider.createConnection(this.connectionString); + DbConnection connection = this.traits.createConnection(this.connectionString); connection.Open(); return connection; } @@ -43,7 +43,7 @@ namespace FLocal.MySQLConnector { placeholder.Add("@" + paramsHolder.Add(id)); } - command.CommandText = "SELECT * FROM " + table.compile() + " WHERE " + table.getIdSpec().compile() + " IN (" + string.Join(", ", placeholder.ToArray()) + ")"; + command.CommandText = "SELECT * FROM " + table.compile(this.traits) + " WHERE " + table.getIdSpec().compile(this.traits) + " IN (" + string.Join(", ", placeholder.ToArray()) + ")"; command.Prepare(); foreach(KeyValuePair kvp in paramsHolder.data) { command.AddParameter(kvp.Key, kvp.Value); @@ -77,17 +77,25 @@ namespace FLocal.MySQLConnector { command.CommandType = System.Data.CommandType.Text; - var conditionsCompiled = ConditionCompiler.Compile(conditions); + var conditionsCompiled = ConditionCompiler.Compile(conditions, this.traits); string queryConditions = conditionsCompiled.Key; ParamsHolder paramsHolder = conditionsCompiled.Value; string queryJoins = ""; + { + if(joins.Length > 0) { + throw new NotImplementedException(); + } + } string querySorts = ""; { + if(sorts.Length > 0) { + throw new NotImplementedException(); + } } - string queryMain = "FROM " + table.compile() + " " + queryJoins + " WHERE " + queryConditions; + string queryMain = "FROM " + table.compile(this.traits) + " " + queryJoins + " WHERE " + queryConditions; foreach(KeyValuePair kvp in paramsHolder.data) { command.AddParameter(kvp.Key, kvp.Value); @@ -105,7 +113,7 @@ namespace FLocal.MySQLConnector { if(diapasone.count >= 0) { queryLimits = "LIMIT " + diapasone.count + " OFFSET " + diapasone.start; } - command.CommandText = "SELECT " + table.compile() + ".* " + queryMain + " " + querySorts + " " + queryLimits; + command.CommandText = "SELECT " + table.compile(this.traits) + ".* " + queryMain + " " + querySorts + " " + queryLimits; List result = new List(); using(DbDataReader reader = command.ExecuteReader()) { @@ -138,7 +146,7 @@ namespace FLocal.MySQLConnector { using(DbCommand command = transaction.sqlconnection.CreateCommand()) { command.Transaction = transaction.sqltransaction; command.CommandType = System.Data.CommandType.Text; - command.CommandText = "LOCK TABLES `" + MySqlHelper.EscapeString(table.name) + "`"; + command.CommandText = "LOCK TABLES " + table.compile(this.traits); command.ExecuteNonQuery(); } } @@ -150,7 +158,7 @@ namespace FLocal.MySQLConnector { 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.CommandText = "SELECT * FROM " + table.compile(this.traits) + " where " + table.getIdSpec().compile(this.traits) + " = @id FOR UPDATE"; command.AddParameter("id", id); command.ExecuteNonQuery(); } @@ -165,12 +173,12 @@ namespace FLocal.MySQLConnector { ParamsHolder paramsholder = new ParamsHolder(); foreach(KeyValuePair kvp in data) { string paramname = paramsholder.Add(kvp.Value); - updates.Add("`" + MySqlHelper.EscapeString(kvp.Key) + "` = @" + paramname); + updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = @" + paramname); } 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.CommandText = "UPDATE " + table.compile(traits) + " set " + String.Join(", ", updates.ToArray()) + " where " + table.getIdSpec().compile(this.traits) + " = @id"; command.AddParameter("id", id); foreach(KeyValuePair kvp in paramsholder.data) { command.AddParameter(kvp.Key, kvp.Value); @@ -188,17 +196,17 @@ namespace FLocal.MySQLConnector { ParamsHolder paramsholder = new ParamsHolder(); foreach(KeyValuePair kvp in data) { string paramname = paramsholder.Add(kvp.Value); - updates.Add("`" + MySqlHelper.EscapeString(kvp.Key) + "` = @" + paramname); + updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = @" + paramname); } command.Transaction = transaction.sqltransaction; command.CommandType = System.Data.CommandType.Text; - command.CommandText = "INSERT INTO `" + MySqlHelper.EscapeString(table.name) + "` SET " + String.Join(", ", updates.ToArray()); + command.CommandText = "INSERT INTO " + table.compile(this.traits) + " SET " + String.Join(", ", updates.ToArray()); foreach(KeyValuePair kvp in paramsholder.data) { command.AddParameter(kvp.Key, kvp.Value); } command.ExecuteNonQuery(); - return this.provider.LastInsertId(command).ToString(); + return this.traits.LastInsertId(command).ToString(); } } } diff --git a/MySQLConnector/Extensions.cs b/MySQLConnector/Extensions.cs index 3549eba..679d807 100644 --- a/MySQLConnector/Extensions.cs +++ b/MySQLConnector/Extensions.cs @@ -9,12 +9,12 @@ using System.Data.Common; namespace FLocal.MySQLConnector { static class Extensions { - public static string compile(this ITableSpec table) { - return "`" + MySqlHelper.EscapeString(table.name) + "`"; + public static string compile(this ITableSpec table, IDBTraits traits) { + return traits.escapeIdentifier(table.name); } - public static string compile(this ColumnSpec column) { - return column.table.compile() + ".`" + MySqlHelper.EscapeString(column.name) + "`"; + public static string compile(this ColumnSpec column, IDBTraits traits) { + return column.table.compile(traits) + "." + traits.escapeIdentifier(column.name); } public static void AddParameter(this DbCommand command, string name, string value) { diff --git a/MySQLConnector/IDBProvider.cs b/MySQLConnector/IDBTraits.cs similarity index 73% rename from MySQLConnector/IDBProvider.cs rename to MySQLConnector/IDBTraits.cs index ee772d7..bfe10ab 100644 --- a/MySQLConnector/IDBProvider.cs +++ b/MySQLConnector/IDBTraits.cs @@ -5,11 +5,13 @@ using System.Text; using System.Data.Common; namespace FLocal.MySQLConnector { - public interface IDBProvider { + public interface IDBTraits { DbConnection createConnection(string connectionString); long LastInsertId(DbCommand command); + string escapeIdentifier(string identifier); + } } diff --git a/MySQLConnector/MySQLConnector.csproj b/MySQLConnector/MySQLConnector.csproj index 7d86fba..2828c1f 100644 --- a/MySQLConnector/MySQLConnector.csproj +++ b/MySQLConnector/MySQLConnector.csproj @@ -48,8 +48,8 @@ - - + + diff --git a/MySQLConnector/MySQLProvider.cs b/MySQLConnector/MySQLDBTraits.cs similarity index 61% rename from MySQLConnector/MySQLProvider.cs rename to MySQLConnector/MySQLDBTraits.cs index 4133c0b..d4f9d3b 100644 --- a/MySQLConnector/MySQLProvider.cs +++ b/MySQLConnector/MySQLDBTraits.cs @@ -6,9 +6,9 @@ using System.Data.Common; using MySql.Data.MySqlClient; namespace FLocal.MySQLConnector { - public class MySQLProvider : IDBProvider { + public class MySQLDBTraits : IDBTraits { - public static readonly MySQLProvider instance = new MySQLProvider(); + public static readonly MySQLDBTraits instance = new MySQLDBTraits(); public DbConnection createConnection(string connectionString) { return new MySqlConnection(connectionString); @@ -18,5 +18,9 @@ namespace FLocal.MySQLConnector { return ((MySqlCommand)command).LastInsertedId; } + public string escapeIdentifier(string identifier) { + return "`" + MySqlHelper.EscapeString(identifier) + "`"; + } + } }