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) {
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();
}

@ -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<string, ParamsHolder> Compile(AbstractCondition condition) {
ConditionCompiler compiler = new ConditionCompiler();
public static KeyValuePair<string, ParamsHolder> Compile(AbstractCondition condition, IDBTraits traits) {
ConditionCompiler compiler = new ConditionCompiler(traits);
string compiled = compiler.CompileCondition(condition);
return new KeyValuePair<string,ParamsHolder>(compiled, compiler.paramsholder);
}

@ -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<Transaction> 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<Transaction>();
}
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<string, string> 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<string, string> 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<string> result = new List<string>();
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<string, string> 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<string, string> kvp in paramsholder.data) {
command.AddParameter(kvp.Key, kvp.Value);
@ -188,17 +196,17 @@ namespace FLocal.MySQLConnector {
ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> 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<string, string> kvp in paramsholder.data) {
command.AddParameter(kvp.Key, kvp.Value);
}
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 {
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) {

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

@ -48,8 +48,8 @@
<Compile Include="ConditionCompiler.cs" />
<Compile Include="Connection.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="IDBProvider.cs" />
<Compile Include="MySQLProvider.cs" />
<Compile Include="IDBTraits.cs" />
<Compile Include="MySQLDBTraits.cs" />
<Compile Include="ParamsHolder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.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) + "`";
}
}
}
Loading…
Cancel
Save