From e5660a4b7ca969a07c0cc520492969a9449ed2a1 Mon Sep 17 00:00:00 2001 From: Jochen Wezel Date: Thu, 1 Aug 2024 11:36:18 +0200 Subject: [PATCH] several performance tweaks * performance improvements * resolved several compiler recommendations --- .../CsvTables/CsvReadBaseOptions.vb | 6 +---- .../DataQuery.AnyIDataProvider.vb | 4 ++++ CompuMaster.Data/DataQuery.DataProvider.vb | 4 ++-- CompuMaster.Data/DataTables.vb | 24 ++++++++++++------- CompuMaster.Data/Manipulation.vb | 16 ++++--------- CompuMaster.Test.Tools.Data/DataTablesTest.vb | 15 ++++++++++++ 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/CompuMaster.Data/CsvTables/CsvReadBaseOptions.vb b/CompuMaster.Data/CsvTables/CsvReadBaseOptions.vb index bcbf65b..e74d665 100644 --- a/CompuMaster.Data/CsvTables/CsvReadBaseOptions.vb +++ b/CompuMaster.Data/CsvTables/CsvReadBaseOptions.vb @@ -1,8 +1,4 @@ -Imports System.Diagnostics.Eventing -Imports System.IO -Imports System.Text - -Namespace CompuMaster.Data.CsvTables +Namespace CompuMaster.Data.CsvTables Public MustInherit Class CsvReadBaseOptions diff --git a/CompuMaster.Data/DataQuery.AnyIDataProvider.vb b/CompuMaster.Data/DataQuery.AnyIDataProvider.vb index 5e988b9..b7dacbc 100644 --- a/CompuMaster.Data/DataQuery.AnyIDataProvider.vb +++ b/CompuMaster.Data/DataQuery.AnyIDataProvider.vb @@ -971,7 +971,9 @@ Namespace CompuMaster.Data.DataQuery Else value = CType(MyReader(1), TValue) End If +#Disable Warning CA1864 ' Methode "Dictionary.TryAdd(TKey, TValue)" bevorzugen -> RECOMMENDED FEATURE NOT SUPPORTED BY .NET 4 If Result.ContainsKey(key) Then +#Enable Warning CA1864 ' Methode "Dictionary.TryAdd(TKey, TValue)" bevorzugen Result(key) = value Else Result.Add(key, value) @@ -1039,7 +1041,9 @@ Namespace CompuMaster.Data.DataQuery Else value = New TValue?(CType(MyReader(1), TValue)) End If +#Disable Warning CA1864 ' Methode "Dictionary.TryAdd(TKey, TValue)" bevorzugen -> RECOMMENDED FEATURE NOT SUPPORTED BY .NET 4 If Result.ContainsKey(key) Then +#Enable Warning CA1864 ' Methode "Dictionary.TryAdd(TKey, TValue)" bevorzugen Result(key) = value Else Result.Add(key, value) diff --git a/CompuMaster.Data/DataQuery.DataProvider.vb b/CompuMaster.Data/DataQuery.DataProvider.vb index 787e0c9..f5ef866 100644 --- a/CompuMaster.Data/DataQuery.DataProvider.vb +++ b/CompuMaster.Data/DataQuery.DataProvider.vb @@ -91,9 +91,9 @@ Namespace CompuMaster.Data.DataQuery Result = Strings.Replace(Me.ConnectionType.Name, "Connection", "") Result = Strings.Replace(Result, "Connection".ToLowerInvariant, "") Result = Strings.Replace(Result, "Connection".ToUpperInvariant, "") - If Result = "Sql" AndAlso (Me.AssemblyName.ToLowerInvariant = "system.data" OrElse Me.AssemblyName.ToLowerInvariant = "system.data.sqlclient") Then + If Result = "Sql" AndAlso (AssemblyName.Equals("system.data", StringComparison.OrdinalIgnoreCase) OrElse AssemblyName.Equals("system.data.sqlclient", StringComparison.OrdinalIgnoreCase)) Then Result = "SqlClient" - ElseIf Result = "Odbc" AndAlso (Me.AssemblyName.ToLowerInvariant = "system.data" OrElse Me.AssemblyName.ToLowerInvariant = "system.data.odbc") Then + ElseIf Result = "Odbc" AndAlso (AssemblyName.Equals("system.data", StringComparison.OrdinalIgnoreCase) OrElse AssemblyName.Equals("system.data.odbc", StringComparison.OrdinalIgnoreCase)) Then Result = "ODBC" End If Return Result diff --git a/CompuMaster.Data/DataTables.vb b/CompuMaster.Data/DataTables.vb index 3c2a29f..b575946 100644 --- a/CompuMaster.Data/DataTables.vb +++ b/CompuMaster.Data/DataTables.vb @@ -1146,7 +1146,7 @@ Namespace CompuMaster.Data Dim columnExistsInDestination As Boolean = False For MyDestTableCounter As Integer = 0 To destinationTable.Columns.Count - 1 If caseInsensitiveColumnNames Then - If destinationTable.Columns(MyDestTableCounter).ColumnName.ToUpperInvariant = sourceTable.Columns(MySourceTableCounter).ColumnName.ToUpperInvariant Then + If destinationTable.Columns(MyDestTableCounter).ColumnName.Equals(sourceTable.Columns(MySourceTableCounter).ColumnName, StringComparison.OrdinalIgnoreCase) Then columnExistsInDestination = True Exit For End If @@ -3335,6 +3335,18 @@ Namespace CompuMaster.Data ''' ''' Comparisons with DBNull.Value will return False or True, never DBNull.Value Public Shared Function CompareValuesOfUnknownType(ByVal value1 As Object, ByVal value2 As Object, ByVal compareStringsCaseInsensitive As Boolean) As Boolean + Return CompareValuesOfUnknownType(value1, value2, If(compareStringsCaseInsensitive, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal)) + End Function + + ''' + ''' Compare 2 values of unknown but same type + ''' + ''' 1st value + ''' 2nd value + ''' True to compare strings case insensitive, False for case sensitive + ''' + ''' Comparisons with DBNull.Value will return False or True, never DBNull.Value + Public Shared Function CompareValuesOfUnknownType(ByVal value1 As Object, ByVal value2 As Object, ByVal stringComparisonStyle As StringComparison) As Boolean Dim TypeCheckValue As Object If value1 Is Nothing Then TypeCheckValue = value2 @@ -3354,14 +3366,8 @@ Namespace CompuMaster.Data Return True ElseIf TypeCheckValue.GetType Is GetType(String) Then 'Strings - If compareStringsCaseInsensitive = False Then - If CType(value1, String) <> CType(value2, String) Then - Return False - End If - Else - If StringNotNothingOrEmpty(CType(value1, String)).ToLower(Globalization.CultureInfo.InvariantCulture) <> StringNotNothingOrEmpty(CType(value2, String)).ToLower(Globalization.CultureInfo.InvariantCulture) Then - Return False - End If + If Not StringNotNothingOrEmpty(CType(value1, String)).Equals(StringNotNothingOrEmpty(CType(value2, String)), stringComparisonStyle) Then + Return False End If ElseIf TypeCheckValue.GetType Is GetType(System.Double) Then 'Doubles diff --git a/CompuMaster.Data/Manipulation.vb b/CompuMaster.Data/Manipulation.vb index 4c625ce..42381a8 100644 --- a/CompuMaster.Data/Manipulation.vb +++ b/CompuMaster.Data/Manipulation.vb @@ -255,9 +255,7 @@ Namespace CompuMaster.Data ''' Private Shared Function CreateTableCommandText(ByVal tableName As String, ByVal primaryColumnName As String, ByVal ddlLanguage As DdlLanguage) As String Dim OpenBrackets, CloseBrackets As String -#Disable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" - If tableName.IndexOf("[", StringComparison.Ordinal) >= 0 AndAlso tableName.IndexOf("]", StringComparison.Ordinal) >= 0 Then -#Enable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" + If tableName.Contains("["c) AndAlso tableName.Contains("]"c) Then 'table name already in a well-formed syntax, e.g. "dbo.[Test - 123]" OpenBrackets = Nothing CloseBrackets = Nothing @@ -318,9 +316,7 @@ Namespace CompuMaster.Data Public Shared Function AddMissingColumnsCommandText(ByVal sourceTable As DataTable, ByVal destinationTable As DataTable, remoteTableName As String, ByVal ddlLanguage As DdlLanguage) As String Dim OpenBrackets, CloseBrackets As String -#Disable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" - If remoteTableName.IndexOf("[", StringComparison.Ordinal) >= 0 AndAlso remoteTableName.IndexOf("]", StringComparison.Ordinal) >= 0 Then -#Enable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" + If remoteTableName.Contains("["c) AndAlso remoteTableName.Contains("]"c) Then 'table name already in a well-formed syntax, e.g. "dbo.[Test - 123]" OpenBrackets = Nothing CloseBrackets = Nothing @@ -489,9 +485,7 @@ Namespace CompuMaster.Data ''' Public Shared Function LoadTableDataForManipulationViaCode(ByVal dataConnection As IDbConnection, ByVal tableName As String, ByVal commandTimeout As Integer, isSafeTableName As Boolean) As CompuMaster.Data.DataManipulationResult Dim OpenBrackets, CloseBrackets As String -#Disable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" - If tableName.IndexOf("[", StringComparison.Ordinal) >= 0 AndAlso tableName.IndexOf("]", StringComparison.Ordinal) >= 0 Then -#Enable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" + If tableName.Contains("["c) AndAlso tableName.Contains("]"c) Then 'table name already in a well-formed syntax, e.g. "dbo.[Test - 123]" OpenBrackets = Nothing CloseBrackets = Nothing @@ -837,9 +831,7 @@ Namespace CompuMaster.Data ''' Private Shared Function LoadTableStructureWith1RowFromConnection(ByVal tableName As String, ByVal dataConnection As IDbConnection, ByVal ignoreExceptions As Boolean) As DataTable Dim OpenBrackets, CloseBrackets As String -#Disable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" - If tableName.IndexOf("[", StringComparison.Ordinal) >= 0 AndAlso tableName.IndexOf("]", StringComparison.Ordinal) >= 0 Then -#Enable Warning CA2249 ' Erwägen Sie die Verwendung von "string.Contains" anstelle von "string.IndexOf" + If tableName.Contains("["c) AndAlso tableName.Contains("]"c) Then 'table name already in a well-formed syntax, e.g. "dbo.[Test - 123]" OpenBrackets = Nothing CloseBrackets = Nothing diff --git a/CompuMaster.Test.Tools.Data/DataTablesTest.vb b/CompuMaster.Test.Tools.Data/DataTablesTest.vb index c6d28ee..9fcde6e 100644 --- a/CompuMaster.Test.Tools.Data/DataTablesTest.vb +++ b/CompuMaster.Test.Tools.Data/DataTablesTest.vb @@ -105,6 +105,7 @@ Namespace CompuMaster.Test.Data dt.Rows.Add(e) Assert.AreEqual(0, CompuMaster.Data.DataTables.ColumnIndex(dt.Columns("hey"))) End Sub + Public Sub CompareValuesOfUnknownType() Dim dt As New DataTable dt.Columns.Add("DummmyColumn") @@ -112,7 +113,21 @@ Namespace CompuMaster.Test.Data Dim e As Integer = 23 Dim f As Integer = 24 Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "D", False)) + Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "D", True)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "E", False)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "E", True)) Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "d", False)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("É", "é", False)) + Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("É", "é", True)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType(e, f, False)) + Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "D", StringComparison.Ordinal)) + Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "D", StringComparison.OrdinalIgnoreCase)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "E", StringComparison.Ordinal)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "E", StringComparison.OrdinalIgnoreCase)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "d", StringComparison.Ordinal)) + Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("D", "d", StringComparison.OrdinalIgnoreCase)) + Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("É", "é", StringComparison.Ordinal)) + Assert.IsTrue(CompuMaster.Data.DataTables.CompareValuesOfUnknownType("É", "é", StringComparison.OrdinalIgnoreCase)) Assert.IsFalse(CompuMaster.Data.DataTables.CompareValuesOfUnknownType(e, f, False)) End Sub