Skip to content

Commit

Permalink
Improved error message when KeyFieldAttribute omitted or KeyFieldname…
Browse files Browse the repository at this point in the history
… not overridden. Closes #49.

An InvalidOperationException is thrown if the ObjectExists or ObjectByKey functions are called and KeyFieldName is unknown.
  • Loading branch information
hisystems committed Apr 10, 2012
1 parent 831dfa1 commit 4a1a504
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Database/Database.vb
Expand Up @@ -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)
Expand All @@ -577,6 +579,17 @@ Public Class Database

End Function

''' <summary>
''' Throwns an exception if the key field name is "".
''' </summary>
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

''' --------------------------------------------------------------------------------
''' <summary>
''' ObjectByOrdinalFirst returns the first object in the collection respectively
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4a1a504

Please sign in to comment.