From: Richard B. <rb...@us...> - 2005-03-03 00:38:43
|
Update of /cvsroot/jcframework/dotnet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21979 Modified Files: CClassMap.vb CInjectedObject.vb CPersistenceBroker.vb CPersistentObject.vb CXMLConfigLoader.vb IPersistentObject.vb Log Message: Bug fix (1155465) for saving without first retrieving an object. Also added state property to IPersistableObject to indicate persistent, nonpersistent or unknown state. Index: CPersistenceBroker.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CPersistenceBroker.vb,v retrieving revision 1.97 retrieving revision 1.98 diff -u -d -r1.97 -r1.98 --- CPersistenceBroker.vb 1 Mar 2005 01:28:19 -0000 1.97 +++ CPersistenceBroker.vb 3 Mar 2005 00:38:23 -0000 1.98 @@ -183,6 +183,11 @@ conn.AutoCommit = False Try obj.Persistent = retrievePrivateObject(obj, conn, useFind, useCache) + If obj.Persistent Then + obj.State = PersistenceState.Persistent + Else + obj.State = PersistenceState.NotPersistent + End If Catch ex As Exception x = New RetrieveException(ex.Message, ex) Finally @@ -674,7 +679,7 @@ End If End If End If - End If + End If End If Next Next @@ -937,6 +942,7 @@ Dim tmpdate As Date Dim statement As CSqlStatement Dim rs As CResultset + Dim usedInsert As Boolean clMap = obj.GetClassMap cm = clMap @@ -948,20 +954,47 @@ If obj.Persistent Then statement = cm.getUpdateSqlFor(obj) conn.processStatement(statement) + usedInsert = False Try PCSQLHits.Increment() PCUpdates.Increment() Catch End Try Else - statement = cm.getInsertSqlFor(obj) - conn.processStatement(statement) - Try - PCSQLHits.Increment() - PCInserts.Increment() - Catch - End Try - If cm.getIdentitySize > 0 Then + If obj.State = PersistenceState.Unknown Then + 'We haven't checked to see if this object exists. + 'We will try to insert by default, and if that doesn't work then + 'we'll attempt to update + Try + statement = cm.getInsertSqlFor(obj) + conn.processStatement(statement) + usedInsert = True + PCSQLHits.Increment() + PCUpdates.Increment() + Catch ex As Exception + Debug.WriteLine("Couldn't insert object in unknown state, trying to update...") + Try + statement = cm.getUpdateSqlFor(obj) + conn.processStatement(statement) + usedInsert = False + PCSQLHits.Increment() + PCUpdates.Increment() + Catch ex1 As Exception + Debug.WriteLine("Couldn't update object in unknown state, throwing initial exception") + Throw ex + End Try + End Try + Else + statement = cm.getInsertSqlFor(obj) + conn.processStatement(statement) + usedInsert = True + Try + PCSQLHits.Increment() + PCInserts.Increment() + Catch + End Try + End If + If usedInsert AndAlso cm.getIdentitySize > 0 Then If CInt(cm.RelationalDatabase.getValueFor(obj.GetValueByAttribute(cm.getIdentityAttributeMap(1).Name))) = 0 Then obj.SetAttributeValue(cm.getIdentityAttributeMap(1).Name, cm.RelationalDatabase.getIdentityValue(conn)) End If @@ -972,6 +1005,7 @@ 'If we don't do this the object won't get saved correctly on subsequent calls. obj.ResetOriginalDates() obj.Persistent = True + obj.State = PersistenceState.Persistent obj.IsDirty = False obj.IsQueued = False 'Update cache key in-case primary key fields have changed value @@ -1145,7 +1179,7 @@ If GetType(IValidation).IsInstanceOfType(obj.GetSourceObject) Then If Not CType(obj.GetSourceObject, IValidation).IsValidToDelete Then Debug.WriteLine("Not valid to delete object") - Return 'Do not delete if object + Return 'Do not delete if object End If End If @@ -1182,6 +1216,7 @@ End If obj.Persistent = False + obj.State = PersistenceState.NotPersistent If m_useCache Then m_cache.Remove(obj) 'remove from the cache Try @@ -1241,33 +1276,6 @@ End If Next i - 'statement = clMap.getDeleteSqlFor(obj) - 'Try - ' PCSQLHits.Increment() - 'Catch - 'End Try - 'conn.processStatement(statement) - - 'If deleteSuperClass Then - ' cm = cm.SuperClass - ' If Not cm Is Nothing Then - ' 'delete super class and its associations - ' Value = obj.GetObjectByClassMap(cm) - ' If retrieveObject(Value, False, True) Then - ' 'If Value.Retrieve() Then - ' deletePrivateObject(Value, conn, True) - ' End If - ' End If - 'End If - - 'obj.Persistent = False - 'If m_useCache Then - ' m_cache.Remove(obj) 'remove from the cache - ' Try - ' PCCacheSize.RawValue = m_cache.Count - ' Catch - ' End Try - 'End If colCriteriaParameters = Nothing End Sub @@ -2467,7 +2475,6 @@ Next 'Not any of the childrens, so check classMap - obj = classMap.CreateObjectInstance 'Retrieve superclass details first @@ -2475,41 +2482,17 @@ cm2 = classMap While Not cm1 Is Nothing classMap.populateObject(cm1, obj, dataRow, joins.GetTableAlias(cm1), True, classMap) - 'classMap.populateObject(cm1, obj, dataRow, joins.GetTableAlias(cm1), True, cm1) cm2 = cm1 cm1 = cm1.SuperClass End While 'set persistent to false because the parent will allways be persistent obj.Persistent = False + 'Now retrieve the class details classMap.populateObject(classMap, obj, dataRow, joins.GetTableAlias(classMap)) - 'Dim tmpobj As CPersistentObject - 'tmpobj = m_cache.Item(obj) - 'If Not tmpobj Is Nothing Then - ' Return tmpobj - 'End If - - 'cm1 = classMap.SuperClass - - 'Dim tmpobj2 As CPersistentObject - 'While Not cm1 Is Nothing - ' tmpobj = obj.GetBaseCopy - ' tmpobj2 = m_cache.Item(tmpobj) - ' If tmpobj2 Is Nothing Then - ' cm1 = cm1.SuperClass - ' Else - ' Return tmpobj2 - ' End If - 'End While - - ''retrieve associations for the class and its super classes - 'obj.IsLoading = True - 'retrieveAssociations(obj, conn, classMap, True) - 'obj.IsLoading = False If obj.Persistent Then - 'obj.AssociationsLoaded = True obj.IsDirty = False obj.OriginalCacheKey = New CCacheKey(obj) End If @@ -2562,7 +2545,7 @@ Dim qObj As Object Dim col As IList 'Dim stack As New Stack - Dim queue As New Queue + Dim queue As New queue Dim i, k As Integer Dim tmpObj As Object Dim aObj As CAssociationObject Index: IPersistentObject.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/IPersistentObject.vb,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- IPersistentObject.vb 1 Mar 2005 01:28:19 -0000 1.9 +++ IPersistentObject.vb 3 Mar 2005 00:38:24 -0000 1.10 @@ -14,6 +14,7 @@ ReadOnly Property IsReadOnly() As Boolean ReadOnly Property IsModifyOnly() As Boolean Property OriginalCacheKey() As CCacheKey + Property State() As PersistenceState Function GetClassMap() As CClassMap Function GetFieldLengthByName(ByVal x As String) As Integer @@ -67,4 +68,10 @@ Public Interface IValidation Function IsValid() As Boolean Function IsValidToDelete() As Boolean -End Interface \ No newline at end of file +End Interface + +Public Enum PersistenceState As Integer + Unknown + NotPersistent + Persistent +End Enum \ No newline at end of file Index: CXMLConfigLoader.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CXMLConfigLoader.vb,v retrieving revision 1.32 retrieving revision 1.33 diff -u -d -r1.32 -r1.33 --- CXMLConfigLoader.vb 10 Feb 2005 04:22:46 -0000 1.32 +++ CXMLConfigLoader.vb 3 Mar 2005 00:38:24 -0000 1.33 @@ -7,7 +7,7 @@ Imports System.IO '''----------------------------------------------------------------------------- -''' Project : AToMSFramework +''' Project : Atoms.Framework ''' Class : CXMLConfigLoader ''' '''----------------------------------------------------------------------------- @@ -168,7 +168,7 @@ Try dbMap.Add(reldb.Name, reldb) Catch ex As Exception - Throw New AToMSFramework.XMLMappingException("Could not add database " & reldb.Name & vbCrLf & ex.Message, ex) + Throw New XMLMappingException("Could not add database " & reldb.Name & vbCrLf & ex.Message, ex) End Try ElseIf node.Name = "class" Then clm = getClassMap(elem) @@ -180,7 +180,7 @@ Try clMap.Add(keyStr, clm) Catch ex As Exception - Throw New AToMSFramework.XMLMappingException("Could not add classmap " & clm.Name & vbCrLf & ex.Message, ex) + Throw New XMLMappingException("Could not add classmap " & clm.Name & vbCrLf & ex.Message, ex) End Try ElseIf node.Name = "association" Then processAssociation(elem) @@ -223,12 +223,14 @@ Dim pAsm As [Assembly] Dim types(), t, reldbType As Type + Dim rdb As Type + rdb = GetType(CRelationalDatabase) If ((Not attrPMName Is Nothing) And (Not attrProviderName Is Nothing)) Then Try pAsm = [Assembly].LoadFrom(attrProviderName.Value) types = pAsm.GetTypes() For Each t In types - If t.IsSubclassOf(GetType(CRelationalDatabase)) Then + If t.IsSubclassOf(rdb) Then reldbType = t Exit For End If Index: CClassMap.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CClassMap.vb,v retrieving revision 1.51 retrieving revision 1.52 diff -u -d -r1.51 -r1.52 --- CClassMap.vb 28 Feb 2005 23:07:43 -0000 1.51 +++ CClassMap.vb 3 Mar 2005 00:38:23 -0000 1.52 @@ -1400,6 +1400,7 @@ Dim rw As DataRow Dim skipAttribute As Boolean + obj.State = PersistenceState.NotPersistent rw = rs.ResultSet.Tables(0).Rows(0) For i = 1 To cm.getSize AttrMap = cm.getAttributeMap(i) @@ -1428,8 +1429,9 @@ Catch ex As Exception End Try obj.SetAttributeValue(AttrMap.Name, val) - If Not IsDBNull(val) And Not val Is Nothing Then + If Not val Is Nothing AndAlso Not IsDBNull(val) Then obj.Persistent = True + obj.State = PersistenceState.Persistent End If End If Next i @@ -1495,6 +1497,7 @@ End If Dim i As Short Dim AttrMap As CAttributeMap + obj.State = PersistenceState.NotPersistent For i = 1 To ClassMap.getSize AttrMap = ClassMap.getAttributeMap(i) 'Attempt to load column via alias first, then table qualified name then column name @@ -1522,8 +1525,9 @@ End Try obj.SetAttributeValue(AttrMap.Name, tmpObj) - If Not IsDBNull(tmpObj) And Not tmpObj Is Nothing Then + If Not tmpObj Is Nothing AndAlso Not IsDBNull(tmpObj) Then obj.Persistent = True + obj.State = PersistenceState.Persistent End If End If Next i @@ -1569,6 +1573,8 @@ ClassMap = Me Dim i As Short Dim AttrMap As CAttributeMap + + obj.State = PersistenceState.NotPersistent Do For i = 1 To ClassMap.getSize AttrMap = ClassMap.getAttributeMap(i) @@ -1585,8 +1591,9 @@ Catch ex As Exception End Try obj.SetAttributeValue(AttrMap.Name, tmpObj) - If Not IsDBNull(tmpObj) And Not tmpObj Is Nothing Then + If Not tmpObj Is Nothing AndAlso Not IsDBNull(tmpObj) Then obj.Persistent = True + obj.State = PersistenceState.Persistent End If End If Next i @@ -1603,6 +1610,8 @@ End If Dim i As Short Dim AttrMap As CAttributeMap + + obj.State = PersistenceState.NotPersistent Do For i = 1 To ClassMap.getKeySize AttrMap = ClassMap.getKeyAttributeMap(i) @@ -1618,8 +1627,9 @@ Catch ex As Exception End Try obj.SetAttributeValue(AttrMap.Name, tmpObj) - If Not IsDBNull(tmpObj) And Not tmpObj Is Nothing Then + If Not tmpObj Is Nothing AndAlso Not IsDBNull(tmpObj) Then obj.Persistent = True + obj.State = PersistenceState.Persistent End If Next i ClassMap = ClassMap.SuperClass Index: CInjectedObject.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CInjectedObject.vb,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- CInjectedObject.vb 28 Feb 2005 23:07:44 -0000 1.14 +++ CInjectedObject.vb 3 Mar 2005 00:38:23 -0000 1.15 @@ -19,6 +19,7 @@ Private m_guid As Guid Private m_classmap As CClassMap Private m_persistent As Boolean + Private m_state As PersistenceState = PersistenceState.Unknown Private m_retrievedCacheKey As CCacheKey Private m_loading As Boolean Private m_queued As Boolean @@ -436,6 +437,15 @@ End Set End Property + Public Property State() As PersistenceState Implements IPersistableObject.State + Get + Return m_state + End Get + Set(ByVal Value As PersistenceState) + m_state = Value + End Set + End Property + Public Function getCollectionByAttribute(ByVal pName As String) As IList Implements IPersistableObject.getCollectionByAttribute Dim dotPos As Integer dotPos = pName.IndexOf(".") @@ -591,18 +601,17 @@ 'set persistent before populating the object since the dirty flag checks its validity If Me.Persistent Then obj.Persistent = True + obj.State = Me.State End If 'set the associated objects For i = 1 To classMap.getStraightAssociationMapSize udamap = classMap.getStraightAssociationMap(i) - 'If udamap.SaveAutomatic Then If udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_ONE Then obj.SetAttributeValue(udamap.FromClassTarget, Me.getObjectByAttribute(udamap.FromClassTarget)) ElseIf udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Or udamap.Cardinality = CUDAMap.CardinalityEnum.MANY_TO_MANY Then obj.SetAttributeValue(udamap.FromClassTarget, Me.getCollectionByAttribute(udamap.FromClassTarget)) End If - 'End If Next i 'set object's attributes @@ -616,13 +625,11 @@ 'set the associated objects For i = 1 To cm.getStraightAssociationMapSize udamap = cm.getStraightAssociationMap(i) - 'If udamap.SaveAutomatic Then If udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_ONE Then obj.SetAttributeValue(udamap.FromClassTarget, Me.getObjectByAttribute(udamap.FromClassTarget)) ElseIf udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Or udamap.Cardinality = CUDAMap.CardinalityEnum.MANY_TO_MANY Then obj.SetAttributeValue(udamap.FromClassTarget, Me.getCollectionByAttribute(udamap.FromClassTarget)) End If - 'End If Next i 'set superclass's attributes Index: CPersistentObject.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CPersistentObject.vb,v retrieving revision 1.57 retrieving revision 1.58 diff -u -d -r1.57 -r1.58 --- CPersistentObject.vb 1 Mar 2005 01:28:19 -0000 1.57 +++ CPersistentObject.vb 3 Mar 2005 00:38:24 -0000 1.58 @@ -45,6 +45,7 @@ Private m_checkingAssociations As Boolean Private m_isNew As Boolean Private m_editing As Boolean + <NonSerialized()> Private m_state As PersistenceState = PersistenceState.Unknown <NonSerialized()> Private m_classmap As CClassMap Public Event MarkedAsDirty As EventHandler Implements IPersistentObject.MarkedAsDirty @@ -125,6 +126,15 @@ End Set End Property + <Browsable(False)> Public Property State() As PersistenceState Implements IPersistableObject.State + Get + Return m_state + End Get + Set(ByVal Value As PersistenceState) + m_state = Value + End Set + End Property + '''----------------------------------------------------------------------------- ''' <summary> ''' Indicates if the all atributes are populated or only those marked as proxy attributes @@ -1627,7 +1637,7 @@ ''' </summary> ''' <param name="classMap">The class map to get</param> ''' <returns>CPersistentObject The object.</returns> - ''' <remarks>The method creates an object bases on the class map. And populates its attributes + ''' <remarks>The method creates an object based on the class map. And populates its attributes ''' from the child class object. ''' processed. This is used to save the base class object and its assotiations.</remarks> ''' <history> @@ -1648,18 +1658,17 @@ 'set persistent before populating the object since the dirty flag checks its validity If Me.Persistent Then obj.Persistent = True + obj.State = Me.State End If 'set the associated objects For i = 1 To classMap.getStraightAssociationMapSize udamap = classMap.getStraightAssociationMap(i) - 'If udamap.SaveAutomatic Then If udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_ONE Then obj.setAttributeValue(udamap.FromClassTarget, Me.getObjectByAttribute(udamap.FromClassTarget)) ElseIf udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Or udamap.Cardinality = CUDAMap.CardinalityEnum.MANY_TO_MANY Then obj.setAttributeValue(udamap.FromClassTarget, Me.getCollectionByAttribute(udamap.FromClassTarget)) End If - 'End If Next i 'set object's attributes @@ -1673,13 +1682,11 @@ 'set the associated objects For i = 1 To cm.getStraightAssociationMapSize udamap = cm.getStraightAssociationMap(i) - 'If udamap.SaveAutomatic Then If udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_ONE Then obj.setAttributeValue(udamap.FromClassTarget, Me.getObjectByAttribute(udamap.FromClassTarget)) ElseIf udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Or udamap.Cardinality = CUDAMap.CardinalityEnum.ONE_TO_MANY Then obj.setAttributeValue(udamap.FromClassTarget, Me.getCollectionByAttribute(udamap.FromClassTarget)) End If - 'End If Next i 'set superclass's attributes @@ -1698,38 +1705,6 @@ Return obj End Function - '<EditorBrowsable(EditorBrowsableState.Advanced)> _ - 'Public Overridable Function GetBaseCopy() As CPersistentObject - ' 'Use reflection to copy all of the fields from Obj to me (by value) - ' If Me.getClassMap.SuperClass Is Nothing Then - ' Return Nothing - ' End If - ' Dim obj As CPersistentObject = Me.getClassMap.SuperClass.CreateObjectInstance - ' Dim f, fields() As FieldInfo - ' Dim value As Object - ' Dim t As Type - ' Try - ' t = obj.GetType - ' While Not t Is Nothing - ' fields = t.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public) - ' 'Note that this will copy event handlers as well - ' For Each f In fields - ' value = f.GetValue(Me) - ' f.SetValue(obj, value) - ' Next - ' If t.IsSubclassOf(GetType(CPersistentObject)) Then - ' t = t.BaseType - ' Else - ' t = Nothing - ' End If - ' End While - ' Return obj - ' Catch ex As Exception - ' Debug.WriteLine(ex.Message) - ' End Try - ' Return Nothing - 'End Function - #End Region #Region "IEditableObject" @@ -1868,5 +1843,4 @@ Public Function GetSourceObject() As Object Implements IPersistableObject.GetSourceObject Return Me End Function - End Class |