From 5ae4e23763eea1fd6059b0bb19280ae37fac14aa Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 22 Dec 2014 03:33:59 +0100 Subject: [PATCH] Added basic GeographyUpdate structures --- Basic/Basic.csproj | 1 + Basic/Types/Structure/BasicGeographyUpdate.cs | 54 ++++++ Database/Database-GeographyUpdates.cs | 181 ++++++++++++++++++ Database/Database.csproj | 3 +- 4 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 Basic/Types/Structure/BasicGeographyUpdate.cs create mode 100644 Database/Database-GeographyUpdates.cs diff --git a/Basic/Basic.csproj b/Basic/Basic.csproj index 78dd231f..96e78554 100644 --- a/Basic/Basic.csproj +++ b/Basic/Basic.csproj @@ -72,6 +72,7 @@ + diff --git a/Basic/Types/Structure/BasicGeographyUpdate.cs b/Basic/Types/Structure/BasicGeographyUpdate.cs new file mode 100644 index 00000000..9ed3f05f --- /dev/null +++ b/Basic/Types/Structure/BasicGeographyUpdate.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Linq; +using System.Reflection.Emit; +using System.Text; + +namespace Swarmops.Basic.Types.Structure +{ + [Serializable] + public class BasicGeographyUpdate + { + [Obsolete ("Do not call this ctor directly. It is intended for serialization.", true)] + public BasicGeographyUpdate() + { + + } + + public BasicGeographyUpdate (int geographyUpdateId, string updateType, string updateSource, string guid, + string countryCode, string changeDataXml, DateTime createdDateTime, DateTime effectiveDateTime, + bool processed) + { + this.GeographyUpdateId = geographyUpdateId; + this.UpdateType = updateType; + this.UpdateSource = updateSource; + this.Guid = guid; + this.CountryCode = countryCode; + this.ChangeDataXml = changeDataXml; + this.CreatedDateTime = createdDateTime; + this.EffectiveDateTime = effectiveDateTime; + this.Processed = processed; + } + + public BasicGeographyUpdate (BasicGeographyUpdate original) + : this ( + original.GeographyUpdateId, original.UpdateType, original.UpdateSource, original.Guid, + original.CountryCode, original.ChangeDataXml, original.CreatedDateTime, original.EffectiveDateTime, + original.Processed) + { + + } + + + public int GeographyUpdateId { get; private set; } + public string UpdateType { get; private set; } + public string UpdateSource { get; private set; } + public string Guid { get; private set; } + public string CountryCode { get; private set; } + public string ChangeDataXml { get; private set; } + public DateTime CreatedDateTime { get; private set; } + public DateTime EffectiveDateTime { get; private set; } + public bool Processed { get; protected set; } + } +} diff --git a/Database/Database-GeographyUpdates.cs b/Database/Database-GeographyUpdates.cs new file mode 100644 index 00000000..1bd90430 --- /dev/null +++ b/Database/Database-GeographyUpdates.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using Swarmops.Basic.Enums; +using Swarmops.Basic.Types; +using Swarmops.Basic.Types.Structure; + +namespace Swarmops.Database +{ + public partial class SwarmDb + { + #region Field reading code + + private const string geographyUpdateFieldSequence = + " GeographyUpdates.GeographyUpdateId, GeographyUpdates.GeographyUpdateTypeId, " + // 0-1 + "GeographyUpdates.GeographyUpdateSourceId, GeographyUpdates.Guid, GeographyUpdates.CountryCode, " + // 2-4 + "GeographyUpdates.ChangeDataXml, GeographyUpdates.CreatedDateTime, " + // 5-6 + "GeographyUpdates.EffectiveDateTime,GeographyUpdates.Processed, " + // 7-8 + "GeographyUpdateTypes.Name AS GeographyUpdateType, " + // 9 + "GeographyUpdateSources.Name AS GeographyUpdateSource " + // 10 + "FROM GeographyUpdates " + + "JOIN GeographyUpdateTypes ON (GeographyUpdateTypes.GeographyUpdateTypeId=GeographyUpdates.GeographyUpdateTypeId) " + + "JOIN GeographyUpdateSources ON (GeographyUpdateSources.GeographyUpdateSourceId=GeographyUpdates.GeographyUpdateSourceId) "; + + private static BasicGeographyUpdate ReadGeographyUpdateFromDataReader (DbDataReader reader) + { + int geographyUpdateId = reader.GetInt32 (0); + // Fields# 1 and 2 are indexes into other tables, contents of which joined in #9 and #10 instead + string guid = reader.GetString (3); + string countryCode = reader.GetString (4); + string changeDataXml = reader.GetString (5); + DateTime createdDateTime = reader.GetDateTime (6); + DateTime effectiveDateTime = reader.GetDateTime (7); + bool processed = reader.GetBoolean (8); + string updateType = reader.GetString (9); + string updateSource = reader.GetString (10); + + return new BasicGeographyUpdate (geographyUpdateId, updateType, updateSource, guid, countryCode, + changeDataXml, createdDateTime, effectiveDateTime, processed); + } + + #endregion + + public BasicGeographyUpdate[] GetAllGeographyUpdates() + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand ( + "SELECT " + geographyUpdateFieldSequence + " ORDER BY \"CreatedDateTime\"", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add (ReadGeographyUpdateFromDataReader (reader)); + } + + return result.ToArray(); + } + } + } + + + public BasicGeographyUpdate[] GetGeographyUpdatesSince(DateTime since) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT " + geographyUpdateFieldSequence + " WHERE CreatedDateTime >= @sinceDateTime", connection); + AddParameterWithName(command, "@sinceDateTime", since); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadGeographyUpdateFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + public BasicGeographyUpdate[] GetUnprocessedGeographyUpdates() + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT " + geographyUpdateFieldSequence + " WHERE EffectiveDateTime >= @dateTimeNow AND Processed=0", connection); + AddParameterWithName(command, "@dateTimeNow", DateTime.UtcNow); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadGeographyUpdateFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + public bool ExistGeographyUpdateWithGuid(string guid) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT Guid FROM GeographyUpdates WHERE Guid = @testGuid", connection); + AddParameterWithName(command, "@testGuid", guid); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return true; + } + + return false; + } + } + } + + + public int CreateGeographyUpdate (string updateType, string updateSource, string guid, string countryCode, string changeDataXml, DateTime createdDateTime, DateTime effectiveDateTime) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand ("CreateGeographyUpdate", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "geographyUpdateType", updateType); + AddParameterWithName(command, "geographyUpdateSource", updateSource); + AddParameterWithName(command, "guid", guid); + AddParameterWithName(command, "countryCode", countryCode); + AddParameterWithName(command, "changeDataXml", changeDataXml); + AddParameterWithName(command, "createdDateTime", createdDateTime); + AddParameterWithName(command, "effectiveDateTime", effectiveDateTime); + + return Convert.ToInt32 (command.ExecuteScalar()); + } + } + + public void SetGeographyUpdateProcessed (int geographyUpdateId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetGeographyUpdateProcessed", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "geographyUpdateId", geographyUpdateId); + command.ExecuteNonQuery(); + } + } + } +} \ No newline at end of file diff --git a/Database/Database.csproj b/Database/Database.csproj index 6f7bdccd..577ba67d 100644 --- a/Database/Database.csproj +++ b/Database/Database.csproj @@ -58,6 +58,7 @@ + @@ -142,4 +143,4 @@ - + \ No newline at end of file