using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using Swarmops.Basic.Enums; using Swarmops.Basic.Types; namespace Swarmops.Database { public partial class SwarmDb { #region Field reading code private const string geographyFieldSequence = " GeographyId, ParentGeographyId, Name " + // 0-2 " FROM Geographies "; private const string geographyDesignationFieldSequence = " GeographyId, CountryId, Designation, GeographyLevelId " + // 0-3 " FROM GeographyDesignations "; private static BasicGeography ReadGeographyFromDataReader (DbDataReader reader) { int geographyId = reader.GetInt32 (0); int parentGeographyId = reader.GetInt32 (1); string name = reader.GetString (2); return new BasicGeography (geographyId, parentGeographyId, name); } private static BasicGeographyDesignation ReadGeographyDesignationFromDataReader (DbDataReader reader) { int geographyId = reader.GetInt32 (0); int countryId = reader.GetInt32 (1); string designation = reader.GetString (2); GeographyLevel level = (GeographyLevel) reader.GetInt32 (3); return new BasicGeographyDesignation (geographyId, countryId, designation, level); } #endregion public BasicGeography[] GetAllGeographies() { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT " + geographyFieldSequence + " WHERE ParentGeographyId >= 0 ORDER BY \"Name\"", connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add (ReadGeographyFromDataReader (reader)); } return result.ToArray(); } } } public BasicGeography[] GetGeographies (int[] ids) { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT " + geographyFieldSequence + " WHERE ParentGeographyId >= 0 AND GeographyId in (" + JoinIds (ids) + ") ORDER BY \"Name\"", connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add (ReadGeographyFromDataReader (reader)); } return result.ToArray(); } } } public BasicGeography[] GetGeographyChildren (int parentGeographyId) { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT " + geographyFieldSequence + " WHERE ParentGeographyId=" + parentGeographyId + " ORDER BY \"Name\"", connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add (ReadGeographyFromDataReader (reader)); } return result.ToArray(); } } } public Dictionary> GetHashedGeographies() { // This generates a Dictionary >. // // Keys are integers corresponding to NodeIds. At each key n, // the value is an List starting with the node n followed by // its children. // // (Later reflection:) O(n) complexity, instead of recursion. Nice! Dictionary> result = new Dictionary>(); BasicGeography[] nodes = GetAllGeographies(); // Add the nodes. foreach (BasicGeography node in nodes) { List newList = new List(); newList.Add (node); result[node.GeographyId] = newList; } // Add the children. foreach (BasicGeography node in nodes) { if (node.ParentGeographyId != 0) { result[node.ParentGeographyId].Add (node); } } return result; } public BasicGeography[] GetGeographyLine (int leafGeographyId) { List result = new List(); Dictionary> nodes = GetHashedGeographies(); BasicGeography currentNode = nodes[leafGeographyId][0]; // This iterates until the zero-parentid root node is found while (currentNode.GeographyId != 0) { result.Add (currentNode); if (currentNode.ParentGeographyId != 0) { currentNode = nodes[currentNode.ParentGeographyId][0]; } else { currentNode = new BasicGeography (0, 0, string.Empty); } } result.Reverse(); return result.ToArray(); } public BasicGeography[] GetGeographyTree() { return GetGeographyTree (1); } public BasicGeography[] GetGeographyTree (int startGeographyId) { Dictionary> nodes = GetHashedGeographies(); return GetGeographyTree (nodes, startGeographyId, 0); } public Dictionary GetGeographyHashtable (int startGeographyId) { BasicGeography[] nodes = GetGeographyTree (startGeographyId); Dictionary result = new Dictionary(); foreach (BasicGeography node in nodes) { result[node.GeographyId] = node; } return result; } private BasicGeography[] GetGeographyTree (Dictionary> geographies, int startNodeId, int generation) { List result = new List(); List thisList = geographies[startNodeId]; foreach (BasicGeography node in thisList) { if (node.GeographyId != startNodeId) { result.Add (new BasicGeography (node.GeographyId, node.ParentGeographyId, node.Name, generation + 1)); // Add recursively BasicGeography[] children = GetGeographyTree (geographies, node.GeographyId, generation + 1); if (children.Length > 0) { foreach (BasicGeography child in children) { result.Add (child); } } } else if (generation == 0) { // The top parent is special and should be added; the others shouldn't result.Add (new BasicGeography (node.GeographyId, node.ParentGeographyId, node.Name, generation)); } } return result.ToArray(); } public BasicGeography GetGeography (int geographyId) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT " + geographyFieldSequence + " WHERE GeographyId=" + geographyId, connection); using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) { return ReadGeographyFromDataReader (reader); } throw new ArgumentException ("No such GeographyId: " + geographyId); } } } public BasicGeography GetGeographyByName (string geographyName) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT " + geographyFieldSequence + " WHERE Name ='" + geographyName.Replace ("'", "''") + "'", connection); using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) { return ReadGeographyFromDataReader (reader); } throw new ArgumentException ("No such Geography: " + geographyName); } } } public void SetGeographyName (int geographyId, string name) { throw new NotImplementedException ("Renaming geographies is not migrated to MySQL"); /* using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand("SetGeographyName", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName(command, "geographyId", geographyId); AddParameterWithName(command, "name", name); command.ExecuteNonQuery(); }*/ } public Dictionary GetGeographyVoterCounts() { throw new NotImplementedException ("Not Migrated to MySQL"); /* Dictionary result = new Dictionary(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand( "select GeographyId, SUM(VoterCount) AS VoterCount from VotingDistricts GROUP BY GeographyId", connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result[reader.GetInt32(0)] = reader.GetInt32(1); } return result; } }*/ } public void CreateGeographyOfficialDesignation (int geographyId, GeographyLevel level, int countryId, string designation) { throw new NotImplementedException ("Not Migrated to MySQL"); /* using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand("CreateGeographyOfficialDesignation", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName(command, "geographyId", geographyId); AddParameterWithName(command, "countryId", countryId); AddParameterWithName(command, "geographyLevelId", (int)level); AddParameterWithName(command, "designation", designation); command.ExecuteNonQuery(); }*/ } public int GetGeographyIdFromOfficialDesignation (int countryId, GeographyLevel level, string designation) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "select GeographyId FROM GeographyDesignations WHERE CountryId=" + countryId + " AND GeographyLevelId= " + ((int) level) + " AND Designation='" + designation.Replace ("'", "''") + "'", connection); using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) { return reader.GetInt32 (0); } throw new ArgumentException ("No such designation: " + designation); } } } public GeographyLevel[] GetGeographyLevelsAtGeographyId (int geographyId) { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "select GeographyLevelId FROM GeographyDesignations WHERE GeographyId=" + geographyId, connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add ((GeographyLevel) reader.GetInt32 (0)); } } } return result.ToArray(); } public BasicGeographyDesignation[] GetGeographyDesignationsForGeographyId (int geographyId) { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT " + geographyDesignationFieldSequence + " WHERE GeographyId=" + geographyId, connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add (ReadGeographyDesignationFromDataReader (reader)); } } } return result.ToArray(); } public int[] GetGeographyIdsFromLevel (int countryId, GeographyLevel level) { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "select GeographyId FROM GeographyDesignations WHERE GeographyLevelId=" + ((int) level) + " AND CountryId=" + countryId, connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add (reader.GetInt32 (0)); } } } return result.ToArray(); } public int CreateGeography (string name, int parentGeographyId) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ("CreateGeography", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "name", name); AddParameterWithName (command, "parentGeographyId", parentGeographyId); return Convert.ToInt32 (command.ExecuteScalar()); } } } }