More abstraction in MySQLConnector; PostgresDBTraits implemented; FLocal now uses postgres instead of mysql

main
Inga 🏳‍🌈 14 years ago
parent 76b97c9973
commit 9b3cb8d297
  1. 2
      Builder/IISMainHandler/build.txt
  2. 2
      Builder/Program.cs
  3. 2
      Common/Config.cs
  4. 20
      Common/dataobjects/Board.cs
  5. 6
      Common/dataobjects/Category.cs
  6. 2
      IISMainHandler/handlers/BoardsHandler.cs
  7. 2
      MySQLConnector/ConditionCompiler.cs
  8. 24
      MySQLConnector/Connection.cs
  9. 1
      MySQLConnector/Extensions.cs
  10. 5
      MySQLConnector/IDBTraits.cs
  11. 5
      MySQLConnector/MySQLConnector.csproj
  12. 7
      MySQLConnector/MySQLDBTraits.cs
  13. 39
      MySQLConnector/PostgresDBTraits.cs
  14. 1
      MySQLConnector/Transaction.cs

@ -75,6 +75,8 @@ namespace Builder {
revNumber = int.Parse(document.GetElementsByTagName("entry")[0].Attributes["revision"].Value);
}
Console.WriteLine("Version number: 1." + revNumber + "." + buildNumber + ".0");
using(TempFile tempFile = new TempFile()) {
string wxsData;

@ -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"], MySQLConnector.MySQLDBTraits.instance);
this.mainConnection = new MySQLConnector.Connection(data["ConnectionString"], MySQLConnector.PostgresDBTraits.instance);
this.dataDir = data["DataDir"];
this.DirSeparator = System.IO.Path.DirectorySeparatorChar.ToString();
}

@ -9,8 +9,8 @@ namespace FLocal.Common.dataobjects {
private class TableSpec : FLocal.Core.DB.ITableSpec {
public static readonly TableSpec instance = new TableSpec();
public string name { get { return "boards"; } }
public string idName { get { return "id"; } }
public string name { get { return "Boards"; } }
public string idName { get { return "Id"; } }
}
protected override FLocal.Core.DB.ITableSpec table { get { return TableSpec.instance; } }
@ -31,11 +31,11 @@ namespace FLocal.Common.dataobjects {
}
}
private int _lastPostId;
private int? _lastPostId;
public int lastPostId {
get {
this.LoadIfNotLoaded();
return this._lastPostId;
return this._lastPostId.Value;
}
}
@ -53,10 +53,14 @@ namespace FLocal.Common.dataobjects {
}
protected override void doFromHash(Dictionary<string, string> data) {
this._name = data["name"];
this._description = data["comment"];
this._lastPostId = int.Parse(data["lastPostId"]);
this._categoryId = int.Parse(data["categoryId"]);
this._name = data["Name"];
this._description = data["Comment"];
if(data["LastPostId"] != "") {
this._lastPostId = int.Parse(data["LastPostId"]);
} else {
this._lastPostId = null;
}
this._categoryId = int.Parse(data["CategoryId"]);
}
public XElement exportToXml() {

@ -8,8 +8,8 @@ namespace FLocal.Common.dataobjects {
private class TableSpec : FLocal.Core.DB.ITableSpec {
public static readonly TableSpec instance = new TableSpec();
public string name { get { return "categories"; } }
public string idName { get { return "id"; } }
public string name { get { return "Categories"; } }
public string idName { get { return "Id"; } }
}
protected override FLocal.Core.DB.ITableSpec table { get { return TableSpec.instance; } }
@ -23,7 +23,7 @@ namespace FLocal.Common.dataobjects {
}
protected override void doFromHash(Dictionary<string, string> data) {
this._name = data["name"];
this._name = data["Name"];
}
}

@ -20,7 +20,7 @@ namespace FLocal.IISHandler.handlers {
override protected XDocument getData(WebContext context) {
Board board1 = Board.LoadById(1);
Board board2 = Board.LoadById(2);
Board board3 = Board.LoadById(3);
Board board3 = Board.LoadById(4);
return new XDocument(
new XElement("root",
new XElement("title", Config.instance.AppInfo),

@ -20,7 +20,7 @@ namespace FLocal.MySQLConnector {
if(cov.isColumn) {
return cov.column.compile(this.traits);
} else {
return "@" + this.paramsholder.Add(cov.value);
return this.traits.markParam(this.paramsholder.Add(cov.value));
}
}

@ -3,7 +3,6 @@ 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;
@ -40,11 +39,11 @@ namespace FLocal.MySQLConnector {
ParamsHolder paramsHolder = new ParamsHolder();
List<string> placeholder = new List<string>();
foreach(string id in ids) {
placeholder.Add("@" + paramsHolder.Add(id));
placeholder.Add(this.traits.markParam(paramsHolder.Add(id)));
}
command.CommandText = "SELECT * FROM " + table.compile(this.traits) + " WHERE " + table.getIdSpec().compile(this.traits) + " IN (" + string.Join(", ", placeholder.ToArray()) + ")";
command.Prepare();
command.CommandText = "SELECT * FROM " + table.compile(this.traits) + " WHERE " + table.getIdSpec().compile(this.traits) + " = " + string.Join(", ", placeholder.ToArray()) + "";
//command.Prepare();
foreach(KeyValuePair<string, string> kvp in paramsHolder.data) {
command.AddParameter(kvp.Key, kvp.Value);
}
@ -54,7 +53,8 @@ namespace FLocal.MySQLConnector {
while(reader.Read()) {
Dictionary<string, string> row = new Dictionary<string,string>();
for(int i=0; i<reader.FieldCount; i++) {
row.Add(reader.GetName(i), reader.GetString(i));
// throw new CriticalException("Name: " + reader.GetName(i));
row.Add(reader.GetName(i), reader.GetValue(i).ToString());
}
rawResult.Add(row[table.idName], row);
}
@ -118,7 +118,7 @@ namespace FLocal.MySQLConnector {
List<string> result = new List<string>();
using(DbDataReader reader = command.ExecuteReader()) {
while(reader.Read()) {
result.Add(reader.GetString(0));
result.Add(reader.GetValue(0).ToString());
}
}
return result;
@ -158,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 " + table.compile(this.traits) + " where " + table.getIdSpec().compile(this.traits) + " = @id FOR UPDATE";
command.CommandText = "SELECT * FROM " + table.compile(this.traits) + " where " + table.getIdSpec().compile(this.traits) + " = " + this.traits.markParam("id") + " FOR UPDATE";
command.AddParameter("id", id);
command.ExecuteNonQuery();
}
@ -172,13 +172,12 @@ namespace FLocal.MySQLConnector {
List<string> updates = new List<string>();
ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> kvp in data) {
string paramname = paramsholder.Add(kvp.Value);
updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = @" + paramname);
updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = " + this.traits.markParam(paramsholder.Add(kvp.Value)));
}
command.Transaction = transaction.sqltransaction;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "UPDATE " + table.compile(traits) + " set " + String.Join(", ", updates.ToArray()) + " where " + table.getIdSpec().compile(this.traits) + " = @id";
command.CommandText = "UPDATE " + table.compile(traits) + " set " + String.Join(", ", updates.ToArray()) + " where " + table.getIdSpec().compile(this.traits) + " = " + this.traits.markParam("id");
command.AddParameter("id", id);
foreach(KeyValuePair<string, string> kvp in paramsholder.data) {
command.AddParameter(kvp.Key, kvp.Value);
@ -195,8 +194,7 @@ namespace FLocal.MySQLConnector {
List<string> updates = new List<string>();
ParamsHolder paramsholder = new ParamsHolder();
foreach(KeyValuePair<string, string> kvp in data) {
string paramname = paramsholder.Add(kvp.Value);
updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = @" + paramname);
updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = " + this.traits.markParam(paramsholder.Add(kvp.Value)));
}
command.Transaction = transaction.sqltransaction;
@ -206,7 +204,7 @@ namespace FLocal.MySQLConnector {
command.AddParameter(kvp.Key, kvp.Value);
}
command.ExecuteNonQuery();
return this.traits.LastInsertId(command).ToString();
return this.traits.LastInsertId(command, table).ToString();
}
}
}

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLocal.Core.DB;
using MySql.Data.MySqlClient;
using System.Data.Common;
namespace FLocal.MySQLConnector {

@ -3,15 +3,18 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using FLocal.Core.DB;
namespace FLocal.MySQLConnector {
public interface IDBTraits {
DbConnection createConnection(string connectionString);
long LastInsertId(DbCommand command);
long LastInsertId(DbCommand command, ITableSpec table);
string escapeIdentifier(string identifier);
string markParam(string param);
}
}

@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Npgsql, Version=2.0.8.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\PostgreSQL\Npgsql\bin\Npgsql.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -51,6 +55,7 @@
<Compile Include="IDBTraits.cs" />
<Compile Include="MySQLDBTraits.cs" />
<Compile Include="ParamsHolder.cs" />
<Compile Include="PostgresDBTraits.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" />
</ItemGroup>

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Data.Common;
using MySql.Data.MySqlClient;
using FLocal.Core.DB;
namespace FLocal.MySQLConnector {
public class MySQLDBTraits : IDBTraits {
@ -14,7 +15,7 @@ namespace FLocal.MySQLConnector {
return new MySqlConnection(connectionString);
}
public long LastInsertId(DbCommand command) {
public long LastInsertId(DbCommand command, ITableSpec table) {
return ((MySqlCommand)command).LastInsertedId;
}
@ -22,5 +23,9 @@ namespace FLocal.MySQLConnector {
return "`" + MySqlHelper.EscapeString(identifier) + "`";
}
public string markParam(string param) {
return "@" + param;
}
}
}

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Npgsql;
using FLocal.Core.DB;
namespace FLocal.MySQLConnector {
public class PostgresDBTraits : IDBTraits {
public static readonly PostgresDBTraits instance = new PostgresDBTraits();
public DbConnection createConnection(string connectionString) {
return new Npgsql.NpgsqlConnection(connectionString);
}
public long LastInsertId(DbCommand command, ITableSpec table) {
string sequenceName = table.name + "_" + table.idName + "_seq";
using(DbCommand newCommand = command.Connection.CreateCommand()) {
if(command.Transaction != null) {
newCommand.Transaction = command.Transaction;
}
newCommand.CommandType = System.Data.CommandType.Text;
newCommand.CommandText = "SELECT CURRVAL(" + this.escapeIdentifier(sequenceName) + ")";
return (long)newCommand.ExecuteScalar();
}
}
public string escapeIdentifier(string identifier) {
return "\"" + identifier.Replace("\"", "") + "\"";
}
public string markParam(string param) {
return ":" + param;
}
}
}

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using FLocal.Core;
using System.Data.Common;

Loading…
Cancel
Save