From 4a1a504a2ffe9704575770f23033b0de7904ea2d Mon Sep 17 00:00:00 2001 From: Toby Wicks Date: Tue, 10 Apr 2012 22:23:52 +0930 Subject: [PATCH] Improved error message when KeyFieldAttribute omitted or KeyFieldname not overridden. Closes #49. An InvalidOperationException is thrown if the ObjectExists or ObjectByKey functions are called and KeyFieldName is unknown. --- Database/Database.vb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Database/Database.vb b/Database/Database.vb index 42a83f5..c362c65 100755 --- a/Database/Database.vb +++ b/Database/Database.vb @@ -552,13 +552,15 @@ Public Class Database Dim objSelect As SQL.SQLSelect = New SQL.SQLSelect Dim objSubset As SQL.SQLConditions + Dim keyFieldName As String = objCollection.KeyFieldName + EnsureKeyFieldNameIsSet(keyFieldName, objCollection) EnsureKeyDataTypeValid(objKey) With objSelect Dim objPrimaryTable As SQL.SQLSelectTable = .Tables.Add(objCollection.TableName) .Tables.Joins = objCollection.TableJoins(objPrimaryTable, .Tables) - .Where.Add(objCollection.KeyFieldName, SQL.ComparisonOperator.EqualTo, objKey) + .Where.Add(keyFieldName, SQL.ComparisonOperator.EqualTo, objKey) objSubset = objCollection.Subset If Not objSubset Is Nothing AndAlso Not objSubset.IsEmpty Then .Where.Add(objSubset) @@ -577,6 +579,17 @@ Public Class Database End Function + ''' + ''' Throwns an exception if the key field name is "". + ''' + Private Sub EnsureKeyFieldNameIsSet(ByVal keyFieldName As String, collection As IDatabaseObjects) + + If keyFieldName = String.Empty Then + Throw New InvalidOperationException("The KeyFieldAttribute has not been specified or the KeyFieldName function overridden for " & collection.GetType.FullName) + End If + + End Sub + ''' -------------------------------------------------------------------------------- ''' ''' ObjectByOrdinalFirst returns the first object in the collection respectively @@ -762,13 +775,15 @@ Public Class Database Dim objSelect As SQL.SQLSelect = New SQL.SQLSelect Dim objSubset As SQL.SQLConditions + Dim keyFieldName As String = objCollection.KeyFieldName + EnsureKeyFieldNameIsSet(keyFieldName, objCollection) EnsureKeyDataTypeValid(objKey) With objSelect .Tables.Add(objCollection.TableName) '.Fields.Add objCollection.DistinctFieldName - .Where.Add(objCollection.KeyFieldName, SQL.ComparisonOperator.EqualTo, objKey) + .Where.Add(keyFieldName, SQL.ComparisonOperator.EqualTo, objKey) objSubset = objCollection.Subset If Not objSubset Is Nothing AndAlso Not objSubset.IsEmpty Then .Where.Add(objSubset)