From 9b3cb8d297c5b67f33634a599e4806e5e284c92f Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 10 Jun 2010 12:23:30 +0000 Subject: [PATCH] More abstraction in MySQLConnector; PostgresDBTraits implemented; FLocal now uses postgres instead of mysql --- Builder/IISMainHandler/build.txt | 2 +- Builder/Program.cs | 2 ++ Common/Config.cs | 2 +- Common/dataobjects/Board.cs | 20 +++++++----- Common/dataobjects/Category.cs | 6 ++-- IISMainHandler/handlers/BoardsHandler.cs | 2 +- MySQLConnector/ConditionCompiler.cs | 2 +- MySQLConnector/Connection.cs | 24 +++++++-------- MySQLConnector/Extensions.cs | 1 - MySQLConnector/IDBTraits.cs | 5 ++- MySQLConnector/MySQLConnector.csproj | 5 +++ MySQLConnector/MySQLDBTraits.cs | 7 ++++- MySQLConnector/PostgresDBTraits.cs | 39 ++++++++++++++++++++++++ MySQLConnector/Transaction.cs | 1 - 14 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 MySQLConnector/PostgresDBTraits.cs diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index 780fea9..7730ef7 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -77 \ No newline at end of file +89 \ No newline at end of file diff --git a/Builder/Program.cs b/Builder/Program.cs index f6c2d55..adc9f1a 100644 --- a/Builder/Program.cs +++ b/Builder/Program.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; diff --git a/Common/Config.cs b/Common/Config.cs index d96d638..6af1351 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(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(); } diff --git a/Common/dataobjects/Board.cs b/Common/dataobjects/Board.cs index 69b1273..3aebe5f 100644 --- a/Common/dataobjects/Board.cs +++ b/Common/dataobjects/Board.cs @@ -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 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() { diff --git a/Common/dataobjects/Category.cs b/Common/dataobjects/Category.cs index 67adb7f..71d4877 100644 --- a/Common/dataobjects/Category.cs +++ b/Common/dataobjects/Category.cs @@ -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 data) { - this._name = data["name"]; + this._name = data["Name"]; } } diff --git a/IISMainHandler/handlers/BoardsHandler.cs b/IISMainHandler/handlers/BoardsHandler.cs index df8dc3c..38cc3e2 100644 --- a/IISMainHandler/handlers/BoardsHandler.cs +++ b/IISMainHandler/handlers/BoardsHandler.cs @@ -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), diff --git a/MySQLConnector/ConditionCompiler.cs b/MySQLConnector/ConditionCompiler.cs index d2bfd2f..475988f 100644 --- a/MySQLConnector/ConditionCompiler.cs +++ b/MySQLConnector/ConditionCompiler.cs @@ -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)); } } diff --git a/MySQLConnector/Connection.cs b/MySQLConnector/Connection.cs index f03b355..0c1ef0b 100644 --- a/MySQLConnector/Connection.cs +++ b/MySQLConnector/Connection.cs @@ -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 placeholder = new List(); 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 kvp in paramsHolder.data) { command.AddParameter(kvp.Key, kvp.Value); } @@ -54,7 +53,8 @@ namespace FLocal.MySQLConnector { while(reader.Read()) { Dictionary row = new Dictionary(); for(int i=0; i result = new List(); 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 updates = new List(); ParamsHolder paramsholder = new ParamsHolder(); foreach(KeyValuePair 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 kvp in paramsholder.data) { command.AddParameter(kvp.Key, kvp.Value); @@ -195,8 +194,7 @@ namespace FLocal.MySQLConnector { List updates = new List(); ParamsHolder paramsholder = new ParamsHolder(); foreach(KeyValuePair 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(); } } } diff --git a/MySQLConnector/Extensions.cs b/MySQLConnector/Extensions.cs index 679d807..4066738 100644 --- a/MySQLConnector/Extensions.cs +++ b/MySQLConnector/Extensions.cs @@ -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 { diff --git a/MySQLConnector/IDBTraits.cs b/MySQLConnector/IDBTraits.cs index bfe10ab..b9a6d1a 100644 --- a/MySQLConnector/IDBTraits.cs +++ b/MySQLConnector/IDBTraits.cs @@ -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); + } } diff --git a/MySQLConnector/MySQLConnector.csproj b/MySQLConnector/MySQLConnector.csproj index 2828c1f..dc22f9d 100644 --- a/MySQLConnector/MySQLConnector.csproj +++ b/MySQLConnector/MySQLConnector.csproj @@ -31,6 +31,10 @@ 4 + + False + C:\Program Files (x86)\PostgreSQL\Npgsql\bin\Npgsql.dll + 3.5 @@ -51,6 +55,7 @@ + diff --git a/MySQLConnector/MySQLDBTraits.cs b/MySQLConnector/MySQLDBTraits.cs index d4f9d3b..7cb10ef 100644 --- a/MySQLConnector/MySQLDBTraits.cs +++ b/MySQLConnector/MySQLDBTraits.cs @@ -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; + } + } } diff --git a/MySQLConnector/PostgresDBTraits.cs b/MySQLConnector/PostgresDBTraits.cs new file mode 100644 index 0000000..a11120d --- /dev/null +++ b/MySQLConnector/PostgresDBTraits.cs @@ -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; + } + + } +} diff --git a/MySQLConnector/Transaction.cs b/MySQLConnector/Transaction.cs index 11fae0a..5d2d224 100644 --- a/MySQLConnector/Transaction.cs +++ b/MySQLConnector/Transaction.cs @@ -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;