Many changes to postal codes - possibly quite breaking changes

This commit is contained in:
Rick Falkvinge
2012-11-17 20:58:11 +00:00
parent d218e8e6d1
commit a234337412
6 changed files with 55 additions and 20 deletions
+8 -6
View File
@@ -7,15 +7,16 @@ namespace Activizr.Basic.Types
[Serializable]
public class BasicPostalCode
{
public BasicPostalCode(string postalCode, string cityName, string countryCode)
public BasicPostalCode(int postalCodeId, string postalCode, int cityId, int countryId)
{
this.PostalCodeId = postalCodeId;
this.PostalCode = postalCode.Replace(" ", "");
this.CityName = cityName;
this.CountryCode = countryCode;
this.CityId = cityId;
this.CountryId = countryId;
}
public BasicPostalCode(BasicPostalCode original)
: this(original.PostalCode, original.CityName, original.CountryCode)
: this(original.PostalCodeId, original.PostalCode, original.CityId, original.CountryId)
{
}
@@ -25,8 +26,9 @@ namespace Activizr.Basic.Types
// this should never be called from code
}
public int PostalCodeId { get; private set; }
public string PostalCode { get; private set; } // any whitespace removed
public string CityName { get; protected set; }
public string CountryCode { get; protected set; }
public int CityId { get; protected set; }
public int CountryId { get; protected set; }
}
}
+8 -10
View File
@@ -14,18 +14,16 @@ namespace Activizr.Database
#region Field reading code
private const string postalCodeFieldSequence =
" PostalCodes.PostalCode,Cities.CityName,Countries.Code AS CountryCode " +
" FROM PostalCodes " +
" JOIN Cities ON (PostalCodes.CityId=Cities.CityId) " +
" JOIN Countries ON (PostalCodes.CountryId=Countries.CountryId) ";
" PostalCodeId,PostalCode,CityId,CountryId ";
private BasicPostalCode ReadPostalCodeFromDataReader(DbDataReader reader)
{
string postalCode = reader.GetString(0);
string cityName = reader.GetString(1);
string countryCode = reader.GetString(2);
int postalCodeId = reader.GetInt32(0);
string postalCode = reader.GetString(1);
int cityId = reader.GetInt32(2);
int countryId = reader.GetInt32(3);
return new BasicPostalCode(postalCode, cityName, countryCode);
return new BasicPostalCode(postalCodeId, postalCode, cityId, countryId);
}
#endregion
@@ -34,7 +32,7 @@ namespace Activizr.Database
#region Record reading - SELECT statements
public BasicPostalCode[] GetPostalCodesForCountry (string countryCode)
public BasicPostalCode[] GetPostalCodesForCountry (int countryId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
@@ -42,7 +40,7 @@ namespace Activizr.Database
DbCommand command =
GetDbCommand(
"SELECT" + postalCodeFieldSequence + "WHERE Countries.Code='" + countryCode.Replace("'", "''") + "';", connection);
"SELECT" + postalCodeFieldSequence + "WHERE CountryId=" + countryId.ToString() + ";", connection);
using (DbDataReader reader = command.ExecuteReader())
{
+5
View File
@@ -19,6 +19,11 @@ namespace Activizr.Logic.Structure
// this instance is NOT initalized, and intended to be used only in serialization.
}
public static City FromIdentity (int cityId)
{
return FromBasic(PirateDb.GetDatabaseForReading().GetCity(cityId));
}
public Geography Geography
{
+8 -3
View File
@@ -16,7 +16,7 @@ namespace Activizr.Logic.Structure
}
[Obsolete ("Do not call this constructor directly. It is intended only for serialization.", true)]
public PostalCode(): base (string.Empty, string.Empty, string.Empty)
public PostalCode(): base (0, string.Empty, 0, 0)
{
// this ctor does NOT initialize the instance. It is provided to allow for serialization.
}
@@ -28,12 +28,17 @@ namespace Activizr.Logic.Structure
public Country Country
{
get { return Country.FromCode(this.CountryCode); }
get { return Country.FromIdentity (this.CountryId); }
}
public City City
{
get { return City.FromName(this.CityName, this.CountryCode); }
get { return City.FromIdentity(this.CityId); }
}
public string CityName
{
get { return this.City.Name; }
}
}
+6 -1
View File
@@ -24,7 +24,12 @@ namespace Activizr.Logic.Structure
public static PostalCodes ForCountry (string countryCode)
{
return FromArray(PirateDb.GetDatabaseForReading().GetPostalCodesForCountry (countryCode));
return ForCountry(Country.FromCode(countryCode).Identity);
}
public static PostalCodes ForCountry (int countryId)
{
return FromArray(PirateDb.GetDatabaseForReading().GetPostalCodesForCountry(countryId));
}
}
}
+20
View File
@@ -305,6 +305,7 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page
Dictionary<int, int> geographyIdTranslation = new Dictionary<int, int>();
Dictionary<int, int> cityIdTranslation = new Dictionary<int, int>();
Dictionary<string, int> countryIdTranslation = new Dictionary<string, int>();
Dictionary<int, bool> cityIdsUsedLookup = new Dictionary<int, bool>();
// Initialize the root geography (which becomes #1 if everything works)
@@ -360,11 +361,30 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page
int countryRootGeographyId = PirateDb.GetDatabaseForWriting().CreateGeography(geography.Name, rootGeographyId);
geographyIdTranslation[geography.GeographyId] = countryRootGeographyId;
PirateDb.GetDatabaseForWriting().SetCountryGeographyId(countryIdTranslation[countryCode],
countryRootGeographyId);
InitDatabaseThreadCreateGeographyChildren(geography.Children, countryRootGeographyId, ref geographyIdTranslation);
_initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry/3);
// Get the postal codes and cities
Activizr.Site.Automation.City[] cities = geoDataFetcher.GetCitiesForCountry(countryCode);
_initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry/2);
Activizr.Site.Automation.PostalCode[] postalCodes = geoDataFetcher.GetPostalCodesForCountry(countryCode);
// Find which cities are actually used
foreach (Activizr.Site.Automation.PostalCode postalCode in postalCodes)
{
// cityIdsUsedLookup[postalCode.CityName] =
}
_initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry*2/3);
countryCount++;
}