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

main
Inga 🏳‍🌈 15 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); revNumber = int.Parse(document.GetElementsByTagName("entry")[0].Attributes["revision"].Value);
} }
Console.WriteLine("Version number: 1." + revNumber + "." + buildNumber + ".0");
using(TempFile tempFile = new TempFile()) { using(TempFile tempFile = new TempFile()) {
string wxsData; string wxsData;

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

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

@ -8,8 +8,8 @@ namespace FLocal.Common.dataobjects {
private class TableSpec : FLocal.Core.DB.ITableSpec { private class TableSpec : FLocal.Core.DB.ITableSpec {
public static readonly TableSpec instance = new TableSpec(); public static readonly TableSpec instance = new TableSpec();
public string name { get { return "categories"; } } public string name { get { return "Categories"; } }
public string idName { get { return "id"; } } public string idName { get { return "Id"; } }
} }
protected override FLocal.Core.DB.ITableSpec table { get { return TableSpec.instance; } } 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) { 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) { override protected XDocument getData(WebContext context) {
Board board1 = Board.LoadById(1); Board board1 = Board.LoadById(1);
Board board2 = Board.LoadById(2); Board board2 = Board.LoadById(2);
Board board3 = Board.LoadById(3); Board board3 = Board.LoadById(4);
return new XDocument( return new XDocument(
new XElement("root", new XElement("root",
new XElement("title", Config.instance.AppInfo), new XElement("title", Config.instance.AppInfo),

@ -20,7 +20,7 @@ namespace FLocal.MySQLConnector {
if(cov.isColumn) { if(cov.isColumn) {
return cov.column.compile(this.traits); return cov.column.compile(this.traits);
} else { } 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.Linq;
using System.Text; using System.Text;
using System.Data.Common; using System.Data.Common;
using MySql.Data.MySqlClient;
using FLocal.Core; using FLocal.Core;
using FLocal.Core.DB; using FLocal.Core.DB;
@ -40,11 +39,11 @@ namespace FLocal.MySQLConnector {
ParamsHolder paramsHolder = new ParamsHolder(); ParamsHolder paramsHolder = new ParamsHolder();
List<string> placeholder = new List<string>(); List<string> placeholder = new List<string>();
foreach(string id in ids) { 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.CommandText = "SELECT * FROM " + table.compile(this.traits) + " WHERE " + table.getIdSpec().compile(this.traits) + " = " + 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);
} }
@ -54,7 +53,8 @@ namespace FLocal.MySQLConnector {
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++) {
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); rawResult.Add(row[table.idName], row);
} }
@ -118,7 +118,7 @@ namespace FLocal.MySQLConnector {
List<string> result = new List<string>(); List<string> result = new List<string>();
using(DbDataReader reader = command.ExecuteReader()) { using(DbDataReader reader = command.ExecuteReader()) {
while(reader.Read()) { while(reader.Read()) {
result.Add(reader.GetString(0)); result.Add(reader.GetValue(0).ToString());
} }
} }
return result; return result;
@ -158,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 " + 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.AddParameter("id", id);
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
@ -172,13 +172,12 @@ namespace FLocal.MySQLConnector {
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) {
string paramname = paramsholder.Add(kvp.Value); updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = " + this.traits.markParam(paramsholder.Add(kvp.Value)));
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 " + 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); 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);
@ -195,8 +194,7 @@ namespace FLocal.MySQLConnector {
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) {
string paramname = paramsholder.Add(kvp.Value); updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = " + this.traits.markParam(paramsholder.Add(kvp.Value)));
updates.Add(this.traits.escapeIdentifier(kvp.Key) + " = @" + paramname);
} }
command.Transaction = transaction.sqltransaction; command.Transaction = transaction.sqltransaction;
@ -206,7 +204,7 @@ namespace FLocal.MySQLConnector {
command.AddParameter(kvp.Key, kvp.Value); command.AddParameter(kvp.Key, kvp.Value);
} }
command.ExecuteNonQuery(); 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.Linq;
using System.Text; using System.Text;
using FLocal.Core.DB; using FLocal.Core.DB;
using MySql.Data.MySqlClient;
using System.Data.Common; using System.Data.Common;
namespace FLocal.MySQLConnector { namespace FLocal.MySQLConnector {

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

@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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" />
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -51,6 +55,7 @@
<Compile Include="IDBTraits.cs" /> <Compile Include="IDBTraits.cs" />
<Compile Include="MySQLDBTraits.cs" /> <Compile Include="MySQLDBTraits.cs" />
<Compile Include="ParamsHolder.cs" /> <Compile Include="ParamsHolder.cs" />
<Compile Include="PostgresDBTraits.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" /> <Compile Include="Transaction.cs" />
</ItemGroup> </ItemGroup>

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

Loading…
Cancel
Save