From: Richard B. <rb...@us...> - 2004-11-01 21:28:19
|
Update of /cvsroot/jcframework/dotnet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26918 Modified Files: CCacheEntry.vb CClassMap.vb CPersistenceBroker.vb CPersistentObject.vb IPersistentObject.vb Log Message: Fixed problem with cache and retrieving collections (was possible to change original cache entry after an object was loaded). Improved DumpCacheDetails output format as well. Also fixed potential problems with XML mappings of shared tables repeating the key fields from the parent. Index: CPersistenceBroker.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CPersistenceBroker.vb,v retrieving revision 1.75 retrieving revision 1.76 diff -u -d -r1.75 -r1.76 --- CPersistenceBroker.vb 1 Nov 2004 00:10:50 -0000 1.75 +++ CPersistenceBroker.vb 1 Nov 2004 21:28:02 -0000 1.76 @@ -1305,30 +1305,7 @@ ''' </history> '''----------------------------------------------------------------------------- Public Function DumpCacheDetails() As String - Dim x As DictionaryEntry - Dim ce As CCacheEntry - Dim i As Integer - Dim t As Type - Dim outString As String - Dim obj As IPersistableObject - Dim ck As CCacheKey - - outString = ">>>> START CACHE DUMP <<<<" & vbCrLf - i = 1 - For Each x In m_cache - ce = x.Value - obj = ce.PersistentObject - t = obj.GetObjectType - ck = x.Key - outString &= i.ToString & "> " & t.ToString & " Dirty:" & obj.IsDirty.ToString & _ - " Persistent:" & obj.Persistent.ToString & _ - " Loading:" & obj.IsLoading.ToString & _ - " AssocationsLoaded:" & obj.AssociationsLoaded.ToString & _ - vbCrLf & ck.ToString & vbCrLf - i += 1 - Next x - outString &= ">>>> END CACHE DUMP <<<<" - Return outString + Return m_cache.ToString End Function '''----------------------------------------------------------------------------- Index: IPersistentObject.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/IPersistentObject.vb,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- IPersistentObject.vb 20 Oct 2004 06:44:23 -0000 1.5 +++ IPersistentObject.vb 1 Nov 2004 21:28:03 -0000 1.6 @@ -31,7 +31,6 @@ Sub ResetOriginalDates() Function GetObjectByClassMap(ByVal classMap As CClassMap) As IPersistableObject Function GetSourceObject() As Object - End Interface Public Interface IPersistentObject Index: CClassMap.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CClassMap.vb,v retrieving revision 1.40 retrieving revision 1.41 diff -u -d -r1.40 -r1.41 --- CClassMap.vb 1 Nov 2004 04:08:23 -0000 1.40 +++ CClassMap.vb 1 Nov 2004 21:28:02 -0000 1.41 @@ -1163,12 +1163,18 @@ m_insertStatement.addSqlClause("(") isFirst = True For i = 1 To Me.getSize - If Not Me.getAttributeMap(i).ColumnMap.IsIdentity Then - If isFirst Then - m_insertStatement.addSqlClause(Me.getAttributeMap(i).ColumnMap.Name) - isFirst = False - Else - m_insertStatement.addSqlClause(", " & Me.getAttributeMap(i).ColumnMap.Name) + AttrMap = Me.getAttributeMap(i) + If Not AttrMap.ColumnMap.IsIdentity Then + 'Shared tables that are mapped with a reference field (ie repeat of the key) + 'should not have the extra key columns included. An attributemap on an attributemap + 'indicates that the attribute is a reference to the parent. + If Me.SharedTableValue Is Nothing OrElse AttrMap.AttributeMap Is Nothing Then + If isFirst Then + m_insertStatement.addSqlClause(AttrMap.ColumnMap.Name) + isFirst = False + Else + m_insertStatement.addSqlClause(", " & AttrMap.ColumnMap.Name) + End If End If End If Next i @@ -1187,11 +1193,13 @@ For i = 1 To Me.getSize AttrMap = Me.getAttributeMap(i) If Not AttrMap.ColumnMap.IsIdentity Then - If isFirst Then - m_insertStatement.addSqlClause(RelationalDatabase.getParamHolder(i)) - isFirst = False - Else - m_insertStatement.addSqlClause(", " & RelationalDatabase.getParamHolder(i)) + If Me.SharedTableValue Is Nothing OrElse AttrMap.AttributeMap Is Nothing Then + If isFirst Then + m_insertStatement.addSqlClause(RelationalDatabase.getParamHolder(i)) + isFirst = False + Else + m_insertStatement.addSqlClause(", " & RelationalDatabase.getParamHolder(i)) + End If End If End If Next i @@ -1211,7 +1219,9 @@ For i = 1 To Me.getSize AttrMap = Me.getAttributeMap(i) If Not AttrMap.ColumnMap.IsIdentity Then - m_insertStatement.addSqlParameter(i, obj.GetValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) + If Me.SharedTableValue Is Nothing OrElse AttrMap.AttributeMap Is Nothing Then + m_insertStatement.addSqlParameter(i, obj.GetValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) + End If End If Next i If Not Me.SharedTableValue Is Nothing Then @@ -1253,30 +1263,41 @@ 'add each field m_updateStatement.addSqlClause(RelationalDatabase.getClauseStringSet & " ") isFirst = True - For i = 1 To Me.getSize - AttrMap = Me.getAttributeMap(i) + i = 0 + For Each AttrMap In Me.AttributeMaps If Not AttrMap.ColumnMap.IsIdentity Then - If Not isFirst Then - m_updateStatement.addSqlClause(", " & Me.getAttributeMap(i).ColumnMap.Name & "=" & m_relationalDatabase.getParamHolder(i)) - Else - 'This is the first attribute - m_updateStatement.addSqlClause("" & Me.getAttributeMap(i).ColumnMap.Name & "=" & m_relationalDatabase.getParamHolder(i)) - isFirst = False + If Me.SharedTableValue Is Nothing OrElse AttrMap.AttributeMap Is Nothing Then + i += 1 + If Not isFirst Then + m_updateStatement.addSqlClause(", " & AttrMap.ColumnMap.Name & "=" & m_relationalDatabase.getParamHolder(i)) + Else + 'This is the first attribute + m_updateStatement.addSqlClause("" & AttrMap.ColumnMap.Name & "=" & m_relationalDatabase.getParamHolder(i)) + isFirst = False + End If End If End If - Next i + Next m_sqlUpdateStub = m_updateStatement.SqlString End If m_updateWhereParamPosition = Me.getSize + For Each AttrMap In Me.AttributeMaps + If AttrMap.ColumnMap.IsIdentity OrElse (Not Me.SharedTableValue Is Nothing AndAlso AttrMap.AttributeMap Is Nothing) Then + m_updateWhereParamPosition -= 1 + End If + Next 'Add Parameter values - For i = 1 To Me.getSize - AttrMap = Me.getAttributeMap(i) + i = 0 + For Each AttrMap In Me.AttributeMaps If Not AttrMap.ColumnMap.IsIdentity Then - m_updateStatement.addSqlParameter(i, obj.GetValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) + If Me.SharedTableValue Is Nothing OrElse AttrMap.AttributeMap Is Nothing Then + i += 1 + m_updateStatement.addSqlParameter(i, obj.GetValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) + End If End If - Next i - paramCount = Me.getSize + Next + paramCount = i 'Add WHERE clause m_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringWhere & " ") isFirst = True @@ -1289,13 +1310,11 @@ If Not obj.OriginalCacheKey Is Nothing Then For i = 1 To Me.getKeySize AttrMap = Me.getKeyAttributeMap(i) - 'keyValuesCol.Add(m_relationalDatabase.getValueFor(obj.OriginalCacheKey.GetKeyValue(i))) keyValuesCol.Add(obj.OriginalCacheKey.GetKeyValue(i)) Next Else For i = 1 To Me.getKeySize AttrMap = Me.getKeyAttributeMap(i) - 'keyValuesCol.Add(m_relationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name))) keyValuesCol.Add(obj.GetValueByAttribute(AttrMap.Name)) Next End If @@ -1304,12 +1323,8 @@ AttrMap = Me.getKeyAttributeMap(i) paramCount += 1 If Not isFirst Then - 'm_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(keyValuesCol.Item(i)))) - 'm_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & AttrMap.ColumnMap.getFullyQualifiedName & "=" & m_relationalDatabase.getParamHolder(paramCount)) m_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd) End If - 'm_updateStatement.addSqlClause("" & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(keyValuesCol.Item(i)))) - 'm_updateStatement.addSqlClause(AttrMap.ColumnMap.getFullyQualifiedName & "=" & m_relationalDatabase.getParamHolder(paramCount)) m_updateStatement.addSqlClause(" " & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo("_" & paramCount.ToString & "_")) isFirst = False m_updateStatement.addSqlParameter(paramCount, keyValuesCol.Item(i), AttrMap.ColumnMap) @@ -1326,8 +1341,6 @@ 'process timestamp attributes If Me.getAttributeMap(i).isTimeStamp Then paramCount += 1 - 'm_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & Me.getAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute("Original" & Me.getAttributeMap(i).Name)))) - 'm_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & Me.getAttributeMap(i).ColumnMap.getFullyQualifiedName & "=" & m_relationalDatabase.getParamHolder(paramCount)) m_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & Me.getAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo("_" & paramCount.ToString & "_")) m_updateStatement.addSqlParameter(paramCount, obj.GetValueByAttribute("Original" & Me.getAttributeMap(i).Name), Me.getAttributeMap(i).ColumnMap) End If Index: CCacheEntry.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CCacheEntry.vb,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- CCacheEntry.vb 15 Oct 2004 06:42:55 -0000 1.19 +++ CCacheEntry.vb 1 Nov 2004 21:28:02 -0000 1.20 @@ -126,6 +126,82 @@ Friend Sub resetExpiry(ByVal lifetime As Double) m_expiryTime = DateAdd(DateInterval.Minute, lifetime, Now) End Sub + + Public Overrides Function ToString() As String + Dim s As String + Dim i As Short + Dim indent As Integer + Dim formatString As String + + Dim cm As CClassMap + Dim am As CAttributeMap + Dim x As Object + Dim y As Object + Dim xstring As String + Dim ystring As String + + If m_object Is Nothing Then + Return "No persistent object" + End If + + s = m_object.GetObjectType.Name & ": " + indent = s.Length + If m_object.IsDirty Then + s &= "Dirty" + Else + s &= "Not Dirty" + End If + If m_object.Persistent Then + s &= ", Persistent" + Else + s &= ", NonPersistent" + End If + If m_object.IsLoading Then + s &= ", Loading" + Else + s &= ", Loaded" + End If + If m_object.AssociationsLoaded Then + s &= ", Assoc. Loaded" + Else + s &= ", No Assoc. Loaded" + End If + s &= vbCrLf + formatString = Space(indent) & "{0,-9}" & vbTab & "{1,-13}" & vbTab & "{2,-13}" & vbCrLf + s &= [String].Format(formatString, "Attribute", "Current Value", "Original Value") + s &= [String].Format(formatString, "---------", "-------------", "--------------") + i = 0 + cm = m_object.GetClassMap() + For i = 1 To cm.getKeySize + am = cm.getKeyAttributeMap(i) + x = m_object.GetValueByAttribute(am.Name) + Try + xstring = x.ToString + Catch ex As Exception + If x Is Nothing Then + xstring = "Nothing" + Else + xstring = "Unprintable" + End If + End Try + Try + If m_originalObject Is Nothing Then + ystring = "N/A" + Else + y = m_originalObject.GetValueByAttribute(am.Name) + ystring = y.ToString + End If + Catch ex As Exception + If y Is Nothing Then + ystring = "Nothing" + Else + ystring = "Unprintable" + End If + End Try + s &= [String].Format(formatString, am.Name, xstring, ystring) + Next + Return s + End Function End Class '''----------------------------------------------------------------------------- @@ -186,10 +262,10 @@ m_keyvalues = New Collection m_type = obj.GetObjectType m_hasLegitValues = True - cm = obj.getClassMap + cm = obj.GetClassMap For i = 1 To cm.getKeySize am = cm.getKeyAttributeMap(i) - x = obj.getValueByAttribute(am.Name) + x = obj.GetValueByAttribute(am.Name) 'Keys cannot have entries set to nothing If x Is Nothing Then m_hasLegitValues = False @@ -395,7 +471,7 @@ s = s & vbTab & i.ToString & ") " & obj.GetType.ToString & ": " & obj.ToString Catch x As Exception If obj Is Nothing Then - s = s & vbTab & i.ToString & ") --Null Reference--: " & "unprintable data" + s = s & vbTab & i.ToString & ") --Null Reference--: " & "Nothing" Else s = s & vbTab & i.ToString & ") " & obj.GetType.ToString & ": " & "unprintable data" End If @@ -477,7 +553,7 @@ Else ce.resetExpiry(m_expiryInterval) End If - If obj.getClassMap.RelationalDatabase.getConnection(Nothing).Started Then + If obj.GetClassMap.RelationalDatabase.getConnection(Nothing).Started Then ce.TransactionType = CCacheEntry.CacheTransaction.Saved ce.CopyObject() End If @@ -497,7 +573,7 @@ End If ce.PersistentObject = obj.Copy ce.OriginalObject = obj - If obj.getClassMap.RelationalDatabase.getConnection(Nothing).Started Then + If obj.GetClassMap.RelationalDatabase.getConnection(Nothing).Started Then ce.TransactionType = CCacheEntry.CacheTransaction.Added End If MyBase.Add(ckey, ce) @@ -529,7 +605,7 @@ Dim t As Type Dim interval As Double - cm = obj.getClassMap + cm = obj.GetClassMap 'Debug.WriteLine("Cache - Attempt to Find() <" & cm.Name & "> object in cache") @@ -554,7 +630,7 @@ found = True For i = 1 To cm.getFindSize attrmap = cm.FindAttributeMaps(i) - If obj.getValueByAttribute(attrmap.Name) <> ce.PersistentObject.getValueByAttribute(attrmap.Name) Then + If obj.GetValueByAttribute(attrmap.Name) <> ce.PersistentObject.GetValueByAttribute(attrmap.Name) Then found = False Exit For End If @@ -669,7 +745,7 @@ Exit Sub End If 'Debug.WriteLine("Cache - Delete object with keys:" & vbCrLf & ckey.ToString) - If obj.getClassMap.RelationalDatabase.getConnection(Nothing).Started Then + If obj.GetClassMap.RelationalDatabase.getConnection(Nothing).Started Then ce = MyBase.Item(ckey) If Not (ce Is Nothing) Then ce.TransactionType = CCacheEntry.CacheTransaction.Deleted @@ -724,7 +800,7 @@ ce = x.Value If (ce.TransactionType = CCacheEntry.CacheTransaction.Saved Or _ ce.TransactionType = CCacheEntry.CacheTransaction.Added) _ - AndAlso ce.PersistentObject.getClassMap.RelationalDatabase Is reldb Then + AndAlso ce.PersistentObject.GetClassMap.RelationalDatabase Is reldb Then ce.TransactionType = CCacheEntry.CacheTransaction.None End If Next @@ -732,7 +808,7 @@ 'Now look for cache entries to be deleted and remove them For Each x In Me ce = x.Value - If ce.TransactionType = CCacheEntry.CacheTransaction.Deleted AndAlso ce.PersistentObject.getClassMap.RelationalDatabase Is reldb Then + If ce.TransactionType = CCacheEntry.CacheTransaction.Deleted AndAlso ce.PersistentObject.GetClassMap.RelationalDatabase Is reldb Then al.Add(New CCacheKey(ce.PersistentObject)) End If Next @@ -750,11 +826,11 @@ For Each x In Me ce = x.Value 'Lets cancel deletes first - just means resetting transaction to None - If ce.TransactionType = CCacheEntry.CacheTransaction.Deleted AndAlso ce.PersistentObject.getClassMap.RelationalDatabase Is reldb Then + If ce.TransactionType = CCacheEntry.CacheTransaction.Deleted AndAlso ce.PersistentObject.GetClassMap.RelationalDatabase Is reldb Then ce.TransactionType = CCacheEntry.CacheTransaction.None End If 'Now look for cache entries to be saved and restore them - If ce.TransactionType = CCacheEntry.CacheTransaction.Saved AndAlso ce.PersistentObject.getClassMap.RelationalDatabase Is reldb Then + If ce.TransactionType = CCacheEntry.CacheTransaction.Saved AndAlso ce.PersistentObject.GetClassMap.RelationalDatabase Is reldb Then ce.RestoreObject() ce.TransactionType = CCacheEntry.CacheTransaction.None End If @@ -763,7 +839,7 @@ 'Now look for cache entries to be removed and get rid of them For Each x In Me ce = x.Value - If ce.TransactionType = CCacheEntry.CacheTransaction.Added AndAlso ce.PersistentObject.getClassMap.RelationalDatabase Is reldb Then + If ce.TransactionType = CCacheEntry.CacheTransaction.Added AndAlso ce.PersistentObject.GetClassMap.RelationalDatabase Is reldb Then al.Add(New CCacheKey(ce.PersistentObject)) End If Next @@ -777,6 +853,24 @@ Return m_objectsLoading End Get Set(ByVal Value As Boolean) + If m_objectsLoading And Value = False Then + 'we are turning off the objects loading flag. + 'When this flag is on, references to the original object are returned + 'so that collections can be populated properly, etc. Which means that + 'at this point in time it is quite likely that some collections have + 'direct references to the original object of a cache entry and could easily + 'change that value (which should not happen). + ' For this reason we need to now go through each cache entry and copy the + 'current original_object over the top of itself. Collections will still + 'be referencing a proper and valid object, but changing it will not then + 'affect the contents of the cache. + Debug.WriteLine("Cache: ObjectsLoading turned off so now resetting m_originalobject for all cache entries") + Dim ce As CCacheEntry + For Each de As DictionaryEntry In Me + ce = de.Value + ce.OriginalObject = ce.OriginalObject.Copy + Next de + End If m_objectsLoading = Value End Set End Property @@ -790,4 +884,28 @@ Return ce.PersistentObject.Copy End If End Function + + Public Overrides Function ToString() As String + Dim x As DictionaryEntry + Dim ce As CCacheEntry + Dim i As Integer + Dim t As Type + Dim outString As String + Dim pObj As IPersistableObject + Dim ck As CCacheKey + + outString = ">>>> START CACHE DUMP <<<<" & vbCrLf + i = 1 + For Each x In Me + ce = x.Value + pObj = ce.PersistentObject + ck = x.Key + outString &= i.ToString & ">" & ce.ToString & vbCrLf & _ + i.ToString & " (Cache Key)>" & ck.ToString & vbCrLf + i += 1 + Next x + outString &= ">>>> END CACHE DUMP <<<<" + Return outString + + End Function End Class \ No newline at end of file Index: CPersistentObject.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CPersistentObject.vb,v retrieving revision 1.48 retrieving revision 1.49 diff -u -d -r1.48 -r1.49 --- CPersistentObject.vb 27 Oct 2004 07:38:48 -0000 1.48 +++ CPersistentObject.vb 1 Nov 2004 21:28:03 -0000 1.49 @@ -1819,4 +1819,5 @@ Public Function GetSourceObject() As Object Implements IPersistableObject.GetSourceObject Return Me End Function + End Class |