More abstraction in MySQL

main
Inga 🏳‍🌈 15 years ago
parent 56262d7896
commit 76b97c9973
  1. 2
      Builder/IISMainHandler/build.txt
  2. 2
      Common/Config.cs
  3. 20
      MySQLConnector/ConditionCompiler.cs
  4. 40
      MySQLConnector/Connection.cs
  5. 8
      MySQLConnector/Extensions.cs
  6. 4
      MySQLConnector/IDBTraits.cs
  7. 4
      MySQLConnector/MySQLConnector.csproj
  8. 8
      MySQLConnector/MySQLDBTraits.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(MySQLConnector.MySQLProvider.instance, data["ConnectionString"]); this.mainConnection = new MySQLConnector.Connection(data["ConnectionString"], MySQLConnector.MySQLDBTraits.instance);
this.dataDir = data["DataDir"]; this.dataDir = data["DataDir"];
this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString(); this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString();
} }

@ -9,21 +9,23 @@ namespace FLocal.MySQLConnector {
class ConditionCompiler { class ConditionCompiler {
private readonly ParamsHolder paramsholder; private readonly ParamsHolder paramsholder;
private readonly IDBTraits traits;
private ConditionCompiler() { private ConditionCompiler(IDBTraits traits) {
this.paramsholder = new ParamsHolder(); this.paramsholder = new ParamsHolder();
this.traits = traits;
} }
private string getName(ColumnOrValue cov) { private string getName(ColumnOrValue cov) {
if(cov.isColumn) { if(cov.isColumn) {
return cov.column.compile(); return cov.column.compile(this.traits);
} else { } else {
return "@" + this.paramsholder.Add(cov.value); return "@" + this.paramsholder.Add(cov.value);
} }
} }
private string CompileCondition(ComparisonCondition condition) { private string CompileCondition(ComparisonCondition condition) {
string left = condition.left.compile(); string left = condition.left.compile(this.traits);
string right = getName(condition.right); string right = getName(condition.right);
switch(condition.comparison) { switch(condition.comparison) {
case ComparisonType.EQUAL: case ComparisonType.EQUAL:
@ -44,11 +46,11 @@ namespace FLocal.MySQLConnector {
} }
private string CompileCondition(IsNullCondition condition) { private string CompileCondition(IsNullCondition condition) {
return condition.column.compile() + " IS NULL"; return condition.column.compile(this.traits) + " IS NULL";
} }
private string CompileCondition(NotIsNullCondition condition) { 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) { private string CompileCondition(MultiValueCondition condition) {
@ -58,9 +60,9 @@ namespace FLocal.MySQLConnector {
} }
if(condition.inclusive) { if(condition.inclusive) {
return condition.column.compile() + " IN (" + string.Join(", ", valueParams.ToArray()) + ")"; return condition.column.compile(this.traits) + " IN (" + string.Join(", ", valueParams.ToArray()) + ")";
} else { } 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<string, ParamsHolder> Compile(AbstractCondition condition) { public static KeyValuePair<string, ParamsHolder> Compile(AbstractCondition condition, IDBTraits traits) {
ConditionCompiler compiler = new ConditionCompiler(); ConditionCompiler compiler = new ConditionCompiler(traits);
string compiled = compiler.CompileCondition(condition); string compiled = compiler.CompileCondition(condition);
return new KeyValuePair<string,ParamsHolder>(compiled, compiler.paramsholder); return new KeyValuePair<string,ParamsHolder>(compiled, compiler.paramsholder);
} }

@ -11,23 +11,23 @@ namespace FLocal.MySQLConnector {
public class Connection : IDBConnection { public class Connection : IDBConnection {
private IDBProvider provider; private IDBTraits traits;
private DbConnection connection; private DbConnection connection;
private string connectionString; private string connectionString;
private HashSet<Transaction> transactions; private HashSet<Transaction> transactions;
public Connection(IDBProvider provider, string connectionString) { public Connection(string connectionString, IDBTraits traits) {
this.provider = provider; this.traits = traits;
this.connectionString = connectionString; this.connectionString = connectionString;
this.connection = this.provider.createConnection(this.connectionString); this.connection = this.traits.createConnection(this.connectionString);
this.connection.Open(); this.connection.Open();
this.transactions = new HashSet<Transaction>(); this.transactions = new HashSet<Transaction>();
} }
internal DbConnection createConnection() { internal DbConnection createConnection() {
DbConnection connection = this.provider.createConnection(this.connectionString); DbConnection connection = this.traits.createConnection(this.connectionString);
connection.Open(); connection.Open();
return connection; return connection;
} }
@ -43,7 +43,7 @@ namespace FLocal.MySQLConnector {
placeholder.Add("@" + paramsHolder.Add(id)); 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(); command.Prepare();
foreach(KeyValuePair<string, string> kvp in paramsHolder.data) { foreach(KeyValuePair<string, string> kvp in paramsHolder.data) {
command.AddParameter(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
@ -77,17 +77,25 @@ namespace FLocal.MySQLConnector {
command.CommandType = System.Data.CommandType.Text; command.CommandType = System.Data.CommandType.Text;
var conditionsCompiled = ConditionCompiler.Compile(conditions); var conditionsCompiled = ConditionCompiler.Compile(conditions, this.traits);
string queryConditions = conditionsCompiled.Key; string queryConditions = conditionsCompiled.Key;
ParamsHolder paramsHolder = conditionsCompiled.Value; ParamsHolder paramsHolder = conditionsCompiled.Value;
string queryJoins = ""; string queryJoins = "";
{
if(joins.Length > 0) {
throw new NotImplementedException();
}
}
string querySorts = ""; 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<string, string> kvp in paramsHolder.data) { foreach(KeyValuePair<string, string> kvp in paramsHolder.data) {
command.AddParameter(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
@ -105,7 +113,7 @@ namespace FLocal.MySQLConnector {
if(diapasone.count >= 0) { if(diapasone.count >= 0) {
queryLimits = "LIMIT " + diapasone.count + " OFFSET " + diapasone.start; 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<string> result = new List<string>(); List<string> result = new List<string>();
using(DbDataReader reader = command.ExecuteReader()) { using(DbDataReader reader = command.ExecuteReader()) {
@ -138,7 +146,7 @@ namespace FLocal.MySQLConnector {
using(DbCommand 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 " + table.compile(this.traits);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
} }
@ -150,7 +158,7 @@ namespace FLocal.MySQLConnector {
using(DbCommand 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 " + table.compile(this.traits) + " where " + table.getIdSpec().compile(this.traits) + " = @id FOR UPDATE";
command.AddParameter("id", id); command.AddParameter("id", id);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
@ -165,12 +173,12 @@ namespace FLocal.MySQLConnector {
ParamsHolder paramsholder = new ParamsHolder(); ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> kvp in data) { foreach(KeyValuePair<string, string> kvp in data) {
string paramname = paramsholder.Add(kvp.Value); 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.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 " + table.compile(traits) + " set " + String.Join(", ", updates.ToArray()) + " where " + table.getIdSpec().compile(this.traits) + " = @id";
command.AddParameter("id", id); command.AddParameter("id", id);
foreach(KeyValuePair<string, string> kvp in paramsholder.data) { foreach(KeyValuePair<string, string> kvp in paramsholder.data) {
command.AddParameter(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
@ -188,17 +196,17 @@ namespace FLocal.MySQLConnector {
ParamsHolder paramsholder = new ParamsHolder(); ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> kvp in data) { foreach(KeyValuePair<string, string> kvp in data) {
string paramname = paramsholder.Add(kvp.Value); 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.Transaction = transaction.sqltransaction;
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 " + table.compile(this.traits) + " SET " + String.Join(", ", updates.ToArray());
foreach(KeyValuePair<string, string> kvp in paramsholder.data) { foreach(KeyValuePair<string, string> kvp in paramsholder.data) {
command.AddParameter(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
} }
command.ExecuteNonQuery(); command.ExecuteNonQuery();
return this.provider.LastInsertId(command).ToString(); return this.traits.LastInsertId(command).ToString();
} }
} }
} }

@ -9,12 +9,12 @@ using System.Data.Common;
namespace FLocal.MySQLConnector { namespace FLocal.MySQLConnector {
static class Extensions { static class Extensions {
public static string compile(this ITableSpec table) { public static string compile(this ITableSpec table, IDBTraits traits) {
return "`" + MySqlHelper.EscapeString(table.name) + "`"; return traits.escapeIdentifier(table.name);
} }
public static string compile(this ColumnSpec column) { public static string compile(this ColumnSpec column, IDBTraits traits) {
return column.table.compile() + ".`" + MySqlHelper.EscapeString(column.name) + "`"; return column.table.compile(traits) + "." + traits.escapeIdentifier(column.name);
} }
public static void AddParameter(this DbCommand command, string name, string value) { public static void AddParameter(this DbCommand command, string name, string value) {

@ -5,11 +5,13 @@ using System.Text;
using System.Data.Common; using System.Data.Common;
namespace FLocal.MySQLConnector { namespace FLocal.MySQLConnector {
public interface IDBProvider { public interface IDBTraits {
DbConnection createConnection(string connectionString); DbConnection createConnection(string connectionString);
long LastInsertId(DbCommand command); long LastInsertId(DbCommand command);
string escapeIdentifier(string identifier);
} }
} }

@ -48,8 +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="IDBTraits.cs" />
<Compile Include="MySQLProvider.cs" /> <Compile Include="MySQLDBTraits.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" />

@ -6,9 +6,9 @@ using System.Data.Common;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
namespace FLocal.MySQLConnector { 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) { public DbConnection createConnection(string connectionString) {
return new MySqlConnection(connectionString); return new MySqlConnection(connectionString);
@ -18,5 +18,9 @@ namespace FLocal.MySQLConnector {
return ((MySqlCommand)command).LastInsertedId; return ((MySqlCommand)command).LastInsertedId;
} }
public string escapeIdentifier(string identifier) {
return "`" + MySqlHelper.EscapeString(identifier) + "`";
}
} }
} }
Loading…
Cancel
Save