diff --git a/ActiveRecord/Entity/InstanceGettersSetters/Getters.vb b/ActiveRecord/Entity/InstanceGettersSetters/Getters.vb index 860f742..bc46311 100644 --- a/ActiveRecord/Entity/InstanceGettersSetters/Getters.vb +++ b/ActiveRecord/Entity/InstanceGettersSetters/Getters.vb @@ -18,7 +18,7 @@ Namespace ActiveRecord Optional memberNames As List(Of String) = Nothing, Optional keysByCode As Boolean = False, Optional asInitialData As Boolean = False, - Optional ByRef classMembersInfo As Dictionary(Of String, Databasic.MemberInfo) = Nothing + Optional classMembersInfo As Dictionary(Of String, Databasic.MemberInfo) = Nothing ) As Dictionary(Of String, Object) Dim result As New Dictionary(Of String, Object) If asInitialData Then @@ -96,7 +96,7 @@ Namespace ActiveRecord ''' Dictionary with values, which are different from initial set up. Public Function GetTouched( Optional keysByCode As Boolean = False, - Optional ByRef classMembersInfo As Dictionary(Of String, Databasic.MemberInfo) = Nothing + Optional classMembersInfo As Dictionary(Of String, Databasic.MemberInfo) = Nothing ) As Dictionary(Of String, Object) Dim touched As New Dictionary(Of String, Object) Dim initialValue As Object diff --git a/ActiveRecord/Entity/Manipulations/Save.vb b/ActiveRecord/Entity/Manipulations/Save.vb index ade6a26..9606051 100644 --- a/ActiveRecord/Entity/Manipulations/Save.vb +++ b/ActiveRecord/Entity/Manipulations/Save.vb @@ -5,14 +5,14 @@ Namespace ActiveRecord Partial Public MustInherit Class Entity Inherits DynamicObject - Public Function Save(insertNew As Boolean) As Integer - Dim connection As Connection = Connection.Get( - Tools.GetConnectionIndexByClassAttr(Me.GetType(), True) - ) - Return connection.GetProviderResource().Save(insertNew, Me, connection) - End Function + Public Function Save(Optional insertNew As Boolean = False) As Integer + Dim connection As Connection = Connection.Get( + Tools.GetConnectionIndexByClassAttr(Me.GetType(), True) + ) + Return connection.GetProviderResource().Save(insertNew, Me, connection) + End Function - Public Function Save(insertNew As Boolean, connection As Connection) As Integer + Public Function Save(insertNew As Boolean, connection As Connection) As Integer Return connection.GetProviderResource().Save(insertNew, Me, connection) End Function diff --git a/Connection/BeginTransaction.vb b/Connection/BeginTransaction.vb index 7049d7c..1c6ea56 100644 --- a/Connection/BeginTransaction.vb +++ b/Connection/BeginTransaction.vb @@ -2,46 +2,45 @@ Partial Public MustInherit Class Connection - ''' - ''' Create and begin transaction on first config connection. - ''' - ''' Transaction name. - ''' Transaction isolation level. - ''' New transaction. - Public Function BeginTransaction(Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Transaction - Return Me.BeginTransaction( - Tools.GetConnectionIndexByClassAttr(Tools.GetEntryClassType()), - transactionName, isolationLevel - ) - End Function + ''' + ''' Create and begin transaction on first config connection. + ''' + ''' Transaction name. + ''' Transaction isolation level. + ''' New transaction. + Public Function BeginTransaction(transactionName As String, isolationLevel As IsolationLevel) As Transaction + Return Me.createAndBeginTransaction( + transactionName, isolationLevel + ) + End Function - ''' - ''' Create and begin transaction on specified connection config index. - ''' - ''' Config connection index. - ''' Transaction name. - ''' Transaction isolation level. - ''' New transaction. - Public Function BeginTransaction(connectionIndex As Int32, Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Transaction - Return Databasic.Connection.Get(connectionIndex).createAndBeginTransaction( - transactionName, isolationLevel - ) - End Function + ''' + ''' Create and begin transaction on specified connection config index. + ''' + ''' Config connection index. + ''' Transaction name. + ''' Transaction isolation level. + ''' New transaction. + Public Shared Function BeginTransaction(connectionIndex As Int32, Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Transaction + Return Databasic.Connection.Get(connectionIndex).createAndBeginTransaction( + transactionName, isolationLevel + ) + End Function - ''' - ''' Create and begin transaction on specified connection config name. - ''' - ''' Config connection name. - ''' Transaction name. - ''' Transaction isolation level. - ''' New transaction. - Public Function BeginTransaction(connectionName As String, Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Transaction - Return Databasic.Connection.Get(connectionName).createAndBeginTransaction( - transactionName, isolationLevel - ) - End Function + ''' + ''' Create and begin transaction on specified connection config name. + ''' + ''' Config connection name. + ''' Transaction name. + ''' Transaction isolation level. + ''' New transaction. + Public Shared Function BeginTransaction(connectionName As String, Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = IsolationLevel.Unspecified) As Transaction + Return Databasic.Connection.Get(connectionName).createAndBeginTransaction( + transactionName, isolationLevel + ) + End Function - Protected Overridable Function createAndBeginTransaction(Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = -1) As Transaction + Protected Overridable Function createAndBeginTransaction(Optional transactionName As String = "", Optional isolationLevel As IsolationLevel = -1) As Transaction Return Nothing End Function diff --git a/MetaDescriptor.vb b/MetaDescriptor.vb index 3206ffe..31fc2b9 100644 --- a/MetaDescriptor.vb +++ b/MetaDescriptor.vb @@ -229,19 +229,21 @@ Public Class MetaDescriptor End If formatProvider = If(TypeOf formatAttr Is FormatAttribute, formatAttr.FormatProvider, Nothing) trimChars = If(TypeOf trimAttr Is TrimAttribute, trimAttr.Chars, New Char() {}) - ' complete code column names and db column names collections - result.ColumnsByCodeNames.Add(codeColumnName, New Databasic.MemberInfo With { - .Name = dbColumnName, - .MemberInfo = reflMemberInfo, .Type = item.Value.Type, - .FormatProvider = formatProvider, .TrimChars = trimChars - }) - result.ColumnsByDatabaseNames.Add(dbColumnName, New Databasic.MemberInfo With { - .Name = codeColumnName, - .MemberInfo = reflMemberInfo, .Type = item.Value.Type, - .FormatProvider = formatProvider, .TrimChars = trimChars - }) - ' if there is any key info at class element, add it into keys info collections - If TypeOf primaryKeyAttr Is PrimaryKeyAttribute Then + ' complete code column names and db column names collections + result.ColumnsByCodeNames.Add(codeColumnName, New Databasic.MemberInfo With { + .Name = dbColumnName, + .MemberInfo = reflMemberInfo, .Type = item.Value.Type, + .FormatProvider = formatProvider, .TrimChars = trimChars, + .MemberInfoType = item.Value.MemberInfoType, .Value = Nothing + }) + result.ColumnsByDatabaseNames.Add(dbColumnName, New Databasic.MemberInfo With { + .Name = codeColumnName, + .MemberInfo = reflMemberInfo, .Type = item.Value.Type, + .FormatProvider = formatProvider, .TrimChars = trimChars, + .MemberInfoType = item.Value.MemberInfoType, .Value = Nothing + }) + ' if there is any key info at class element, add it into keys info collections + If TypeOf primaryKeyAttr Is PrimaryKeyAttribute Then If Not String.IsNullOrEmpty(primaryKeyAttr.KeyName) Then keyName = primaryKeyAttr.KeyName If result.PrimaryColumns.ContainsKey(keyName) Then result.PrimaryColumns(keyName).Add(codeColumnName, dbColumnName) diff --git a/Misc/Enums.vb b/Misc/Enums.vb index 4f04362..d869054 100644 --- a/Misc/Enums.vb +++ b/Misc/Enums.vb @@ -32,6 +32,7 @@ Public Enum DuplicateKeyBehaviour End Enum Public Enum MemberInfoType - Prop - Field + None + Prop + Field End Enum diff --git a/ProviderResource/Manipulations/Insert.vb b/ProviderResource/Manipulations/Insert.vb index 180acc3..bc3f5d0 100644 --- a/ProviderResource/Manipulations/Insert.vb +++ b/ProviderResource/Manipulations/Insert.vb @@ -30,19 +30,21 @@ ) ' keys by db names Dim separator As String = "" Dim paramsCounter As Int16 = 0 - Dim aiColumnName As String = If( - classMetaDescription.AutoIncrementColumn.HasValue, - classMetaDescription.AutoIncrementColumn.Value.DatabaseColumnName, "" - ) - For Each item As KeyValuePair(Of String, Object) In touched - If (item.Key = aiColumnName) Then Continue For - columnsSql += separator + item.Key - paramsSql += separator + "@param" + paramsCounter.ToString() - params.Add("param" + paramsCounter.ToString(), item.Value) - paramsCounter += 1 - separator = ", " - Next - Return Databasic.Statement.Prepare( + Dim aiColumnName As String = If( + classMetaDescription.AutoIncrementColumn.HasValue, + classMetaDescription.AutoIncrementColumn.Value.DatabaseColumnName, "" + ) + For Each item As KeyValuePair(Of String, MemberInfo) In classMetaDescription.ColumnsByDatabaseNames + If (item.Key = aiColumnName) Then Continue For + If (item.Value.MemberInfoType = MemberInfoType.Field) Then Continue For + If (Not touched.ContainsKey(item.Key)) Then Continue For + columnsSql += separator + item.Key + paramsSql += separator + "@param" + paramsCounter.ToString() + params.Add("param" + paramsCounter.ToString(), touched(item.Key)) + paramsCounter += 1 + separator = ", " + Next + Return Databasic.Statement.Prepare( $"INSERT INTO {ActiveRecord.Resource.Table(classMetaDescription)} ({columnsSql}) VALUES ({paramsSql})", transactionOrConnection diff --git a/ProviderResource/Manipulations/Save.vb b/ProviderResource/Manipulations/Save.vb index 82e6277..0908a78 100644 --- a/ProviderResource/Manipulations/Save.vb +++ b/ProviderResource/Manipulations/Save.vb @@ -15,18 +15,18 @@ Dim autoIncrementColName As String Dim trans As Transaction = Nothing Try - If classMetaDescription.AutoIncrementColumn.HasValue Then - trans = connection.BeginTransaction("Dtbsc.Prvdr.Rsrc.Save()", IsolationLevel.Unspecified) - result = Me.Insert(instance, trans, classMetaDescription) - lastInsertedId = Me.GetLastInsertedId(trans, classMetaDescription) - trans.Commit() - autoIncrementColName = classMetaDescription.AutoIncrementColumn.Value.CodeColumnName - instance.[Set]( - autoIncrementColName, lastInsertedId, True, - classMetaDescription.ColumnsByCodeNames(autoIncrementColName) - ) - Else - result = Me.Insert(instance, connection, classMetaDescription) + If classMetaDescription.AutoIncrementColumn.HasValue Then + trans = connection.BeginTransaction("Dtbsc.Prvdr.Rsrc.Save()", IsolationLevel.Unspecified) + result = Me.Insert(instance, trans, classMetaDescription) + lastInsertedId = Me.GetLastInsertedId(trans, classMetaDescription) + trans.Commit() + autoIncrementColName = classMetaDescription.AutoIncrementColumn.Value.CodeColumnName + instance.[Set]( + autoIncrementColName, lastInsertedId, True, + classMetaDescription.ColumnsByCodeNames(autoIncrementColName) + ) + Else + result = Me.Insert(instance, connection, classMetaDescription) End If Catch ex As Exception If TypeOf trans Is Transaction Then trans.Rollback() diff --git a/ProviderResource/Manipulations/Update.vb b/ProviderResource/Manipulations/Update.vb index b78cd85..3832803 100644 --- a/ProviderResource/Manipulations/Update.vb +++ b/ProviderResource/Manipulations/Update.vb @@ -36,15 +36,17 @@ classMetaDescription.AutoIncrementColumn.HasValue, classMetaDescription.AutoIncrementColumn.Value.DatabaseColumnName, "" ) - Dim separator As String = "" - For Each item As KeyValuePair(Of String, Object) In touched - If (item.Key = aiColumnName) Then Continue For - sql += separator + item.Key + " = @param" + paramsCounter.ToString() - params.Add("param" + paramsCounter.ToString(), item.Value) - paramsCounter += 1 - separator = ", " - Next - sql += " WHERE " + Dim separator As String = "" + For Each item As KeyValuePair(Of String, MemberInfo) In classMetaDescription.ColumnsByDatabaseNames + If (item.Key = aiColumnName) Then Continue For + If (item.Value.MemberInfoType = MemberInfoType.Field) Then Continue For + If (Not touched.ContainsKey(item.Key)) Then Continue For + sql += separator + item.Key + " = @param" + paramsCounter.ToString() + params.Add("param" + paramsCounter.ToString(), touched(item.Key)) + paramsCounter += 1 + separator = ", " + Next + sql += " WHERE " separator = "" For Each columnItem As KeyValuePair(Of String, String) In keyColumns.Columns sql += separator + columnItem.Value + " = @param" + paramsCounter.ToString()