Skip to content

Commit bd5457e

Browse files
committed
Sync Algorithm updated for better performance on large data
1 parent 0ca78af commit bd5457e

9 files changed

Lines changed: 59 additions & 77 deletions

File tree

src/Attributes/KeyPropertyAttribute.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@ public class KeyPropertyAttribute : Attribute
2323
/// <value><c>true</c> if the property is a primary key; otherwise, <c>false</c>.</value>
2424
public bool IsPrimaryKey { get; }
2525

26+
/// <summary>
27+
/// Gets a value indicating whether the property is comparable.
28+
/// </summary>
29+
public bool IsComparable { get; }
30+
2631
/// <summary>
2732
/// Initializes a new instance of the <see cref="KeyPropertyAttribute"/> class.
2833
/// </summary>
29-
/// <param name="keyProperty">Indicates whether the property should be considered as a key property.
30-
/// Default is <c>true</c>.</param>
31-
/// <param name="isPrimaryKey">Indicates whether the property is a primary key.
32-
/// Default is <c>false</c>.</param>
33-
public KeyPropertyAttribute(bool keyProperty = true, bool isPrimaryKey = false)
34+
/// <param name="keyProperty">Indicates whether the property should be considered as a key property. Default is <c>true</c>.</param>
35+
/// <param name="isPrimaryKey">Indicates whether the property is a primary key. Default is <c>false</c>.</param>
36+
/// <param name="isComparable">Indicates whether the property is comparable. Default is <c>false</c>.</param>
37+
public KeyPropertyAttribute(bool keyProperty = true, bool isPrimaryKey = false, bool isComparable = false)
3438
{
3539
KeyProperty = keyProperty;
3640
IsPrimaryKey = isPrimaryKey;
41+
IsComparable = isComparable;
3742
}
3843
}
3944
}

src/DatabaseManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public DatabaseManager(T databaseProvider)
2828
/// <param name="query">The SQL query to execute.</param>
2929
/// <param name="tableName">The name of the table associated with the query.</param>
3030
/// <returns>A list of results of type <typeparamref name="TItem"/>.</returns>
31-
public List<TItem> ExecuteQuery<TItem>(string query, string tableName) where TItem : IDataContractComparer
31+
public List<TItem> ExecuteQuery<TItem>(string query, string tableName) where TItem : IDataContract
3232
{
3333
List<TItem> result = new List<TItem>();
3434

src/Extensions/DataRowExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class DataRowExtensions
1515
/// <param name="columnName">The name of the column.</param>
1616
/// <returns>The value of the specified column, converted to the specified type.</returns>
1717
/// <remarks>
18-
/// This extension method is intended for use with classes that inherit from <see cref="IDataContractComparer"/>.
18+
/// This extension method is intended for use with classes that inherit from <see cref="IDataContract"/>.
1919
/// </remarks>
2020
public static T GetValue<T>(this DataRow row, string columnName)
2121
{

src/Helper/QueryHelper.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ public class QueryHelper
1212
/// <summary>
1313
/// Gets the table name of a specified type, considering the TableNameAttribute if present.
1414
/// </summary>
15-
/// <typeparam name="T">The type for which to get the table name. Must implement <see cref="IDataContractComparer"/>.</typeparam>
15+
/// <typeparam name="T">The type for which to get the table name. Must implement <see cref="IDataContract"/>.</typeparam>
1616
/// <returns>The table name.</returns>
17-
public string GetTableName<T>() where T : IDataContractComparer
17+
public string GetTableName<T>() where T : IDataContract
1818
{
1919
return CacheManager.GetTableName(typeof(T));
2020
}
2121

2222
/// <summary>
2323
/// Gets the table schema of a specified type, considering the TableSchemaAttribute if present.
2424
/// </summary>
25-
/// <typeparam name="T">The type for which to get the table schema. Must implement <see cref="IDataContractComparer"/>.</typeparam>
25+
/// <typeparam name="T">The type for which to get the table schema. Must implement <see cref="IDataContract"/>.</typeparam>
2626
/// <returns>The table schema name or null if not specified.</returns>
27-
public string? GetTableSchema<T>() where T : IDataContractComparer
27+
public string? GetTableSchema<T>() where T : IDataContract
2828
{
2929
return CacheManager.GetTableSchema(typeof(T));
3030
}
3131

3232
/// <summary>
3333
/// Gets whether the type specifies to generate an INSERT query with ID, considering the GenerateInsertWithIDAttribute if present.
3434
/// </summary>
35-
/// <typeparam name="T">The type for which to determine the generation of INSERT query with ID. Must implement <see cref="IDataContractComparer"/>.</typeparam>
35+
/// <typeparam name="T">The type for which to determine the generation of INSERT query with ID. Must implement <see cref="IDataContract"/>.</typeparam>
3636
/// <returns>True if the INSERT query should include ID, otherwise false.</returns>
37-
public bool GetInsertWithID<T>() where T : IDataContractComparer
37+
public bool GetInsertWithID<T>() where T : IDataContract
3838
{
3939
return CacheManager.GetInsertWithID(typeof(T));
4040
}
@@ -44,80 +44,80 @@ public bool GetInsertWithID<T>() where T : IDataContractComparer
4444
/// during insert query generation, considering the GenerateInsertWithIDAttribute if present.
4545
/// </summary>
4646
/// <typeparam name="T">The type for which to determine the inclusion of identity insert statements.
47-
/// Must implement <see cref="IDataContractComparer"/>.</typeparam>
47+
/// Must implement <see cref="IDataContract"/>.</typeparam>
4848
/// <returns><c>true</c> if identity insert statements should be included; otherwise, <c>false</c>.</returns>
49-
public bool GetIncludeIdentityInsert<T>() where T : IDataContractComparer
49+
public bool GetIncludeIdentityInsert<T>() where T : IDataContract
5050
{
5151
return CacheManager.GetIncludeIdentityInsert(typeof(T));
5252
}
5353

5454
/// <summary>
5555
/// Gets the names of properties marked as key columns for a specified type.
5656
/// </summary>
57-
/// <typeparam name="T">The type for which to get the key columns. Must implement <see cref="IDataContractComparer"/>.</typeparam>
57+
/// <typeparam name="T">The type for which to get the key columns. Must implement <see cref="IDataContract"/>.</typeparam>
5858
/// <returns>A list of key column names.</returns>
59-
/// <seealso cref="IDataContractComparer"/>
60-
public List<string> GetKeyColumns<T>() where T : IDataContractComparer
59+
/// <seealso cref="IDataContract"/>
60+
public List<string> GetKeyColumns<T>() where T : IDataContract
6161
{
6262
return CacheManager.GetKeyColumns(typeof(T));
6363
}
6464

6565
/// <summary>
6666
/// Gets the names of properties marked as excluded properties for a specified type.
6767
/// </summary>
68-
/// <typeparam name="T">The type for which to get the excluded properties. Must implement <see cref="IDataContractComparer"/>.</typeparam>
68+
/// <typeparam name="T">The type for which to get the excluded properties. Must implement <see cref="IDataContract"/>.</typeparam>
6969
/// <returns>A list of excluded property names.</returns>
70-
/// <seealso cref="IDataContractComparer"/>
71-
public List<string> GetExcludedColumns<T>() where T : IDataContractComparer
70+
/// <seealso cref="IDataContract"/>
71+
public List<string> GetExcludedColumns<T>() where T : IDataContract
7272
{
7373
return CacheManager.GetExcludedColumns(typeof(T));
7474
}
7575

7676
/// <summary>
7777
/// Gets the names of all properties for a specified type.
7878
/// </summary>
79-
/// <typeparam name="T">The type for which to get all properties. Must implement <see cref="IDataContractComparer"/>.</typeparam>
79+
/// <typeparam name="T">The type for which to get all properties. Must implement <see cref="IDataContract"/>.</typeparam>
8080
/// <returns>A list of all property names.</returns>
81-
/// <seealso cref="IDataContractComparer"/>
82-
public List<string> GetAllColumns<T>() where T : IDataContractComparer
81+
/// <seealso cref="IDataContract"/>
82+
public List<string> GetAllColumns<T>() where T : IDataContract
8383
{
8484
return CacheManager.GetAllColumns(typeof(T));
8585
}
8686

8787
/// <summary>
8888
/// Retrieves a list of identity columns for a specified data contract type <typeparamref name="T"/>.
8989
/// </summary>
90-
/// <typeparam name="T">The type implementing the IDataContractComparer interface.</typeparam>
90+
/// <typeparam name="T">The type implementing the IDataContract interface.</typeparam>
9191
/// <returns>A list containing the names of identity columns for the specified data contract type <typeparamref name="T"/>.</returns>
9292
/// <remarks>
9393
/// This method uses reflection to analyze the properties of the specified type <typeparamref name="T"/> and retrieves properties marked with a [Key] attribute, indicating identity columns.
9494
/// </remarks>
95-
/// <seealso cref="IDataContractComparer"/>
96-
public List<string> GetIdentityColumns<T>() where T : IDataContractComparer
95+
/// <seealso cref="IDataContract"/>
96+
public List<string> GetIdentityColumns<T>() where T : IDataContract
9797
{
9898
return CacheManager.GetIdentityColumns(typeof(T));
9999
}
100100

101101
/// <summary>
102102
/// Retrieves an array of <see cref="PropertyInfo"/> objects representing the properties that are used for data comparison
103103
/// in objects of type <typeparamref name="T"/>. These properties are determined based on the implementation of the
104-
/// <see cref="IDataContractComparer"/> interface.
104+
/// <see cref="IDataContract"/> interface.
105105
/// </summary>
106106
/// <typeparam name="T">The type of objects for which to retrieve comparable properties.</typeparam>
107107
/// <returns>An array of <see cref="PropertyInfo"/> objects representing the comparable properties of type <typeparamref name="T"/>.</returns>
108-
public PropertyInfo[] GetComparableProperties<T>() where T: IDataContractComparer
108+
public PropertyInfo[] GetComparableProperties<T>() where T: IDataContract
109109
{
110110
return CacheManager.GetComparableProperties(typeof(T));
111111
}
112112

113113
/// <summary>
114114
/// Retrieves an array of <see cref="PropertyInfo"/> objects representing the properties that are used as key properties
115115
/// for uniquely identifying objects of type <typeparamref name="T"/>. These key properties are determined based on the
116-
/// implementation of the <see cref="IDataContractComparer"/> interface.
116+
/// implementation of the <see cref="IDataContract"/> interface.
117117
/// </summary>
118118
/// <typeparam name="T">The type of objects for which to retrieve key properties.</typeparam>
119119
/// <returns>An array of <see cref="PropertyInfo"/> objects representing the key properties of type <typeparamref name="T"/>.</returns>
120-
public PropertyInfo[] GetKeyProperties<T>() where T : IDataContractComparer
120+
public PropertyInfo[] GetKeyProperties<T>() where T : IDataContract
121121
{
122122
return CacheManager.GetKeyProperties(typeof(T));
123123
}

src/Interface/IDataContractComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <remarks>
77
/// Implement this interface in data contracts to enable comparison functionality.
88
/// </remarks>
9-
public interface IDataContractComparer
9+
public interface IDataContract
1010
{
1111
}
1212
}

src/Interface/IMetadata.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Interface/IQueryGenerator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@ public interface IQueryGenerator : IDisposable
88
/// <summary>
99
/// Generates a SELECT query for retrieving data from a database table.
1010
/// </summary>
11-
/// <typeparam name="T">The type of entity that implements IDataContractComparer.</typeparam>
11+
/// <typeparam name="T">The type of entity that implements IDataContract.</typeparam>
1212
/// <param name="tableName">The name of the database table.</param>
1313
/// <param name="ListOfColumns">The list of columns to be selected.</param>
1414
/// <param name="schemaName">The schema name of the database table.</param>
1515
/// <returns>A string representing the generated SELECT query.</returns>
16-
string GenerateSelectQuery<T>(string tableName, List<string> ListOfColumns, string schemaName) where T : IDataContractComparer;
16+
string GenerateSelectQuery<T>(string tableName, List<string> ListOfColumns, string schemaName) where T : IDataContract;
1717

1818
/// <summary>
1919
/// Generates an UPDATE query for updating data in a database table.
2020
/// </summary>
21-
/// <typeparam name="T">The type of entity that implements IDataContractComparer.</typeparam>
21+
/// <typeparam name="T">The type of entity that implements IDataContract.</typeparam>
2222
/// <param name="DataContract">The entity with the updated data.</param>
2323
/// <param name="keyColumns">The list of key columns used for updating.</param>
2424
/// <param name="excludedColumns">The list of columns to be excluded from the update.</param>
2525
/// <param name="editedProperties">A dictionary representing the properties and their new values to be updated.</param>
2626
/// <returns>A string representing the generated UPDATE query.</returns>
27-
string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, Dictionary<string, object> editedProperties) where T : IDataContractComparer;
27+
string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, (string propName, object propValue)[] editedProperties) where T : IDataContract;
2828

2929
/// <summary>
3030
/// Generates a DELETE query for deleting data from a database table.
3131
/// </summary>
32-
/// <typeparam name="T">The type of entity that implements IDataContractComparer.</typeparam>
32+
/// <typeparam name="T">The type of entity that implements IDataContract.</typeparam>
3333
/// <param name="entity">The entity representing the data to be deleted.</param>
3434
/// <param name="keyColumns">The list of key columns used for deletion.</param>
3535
/// <returns>A string representing the generated DELETE query.</returns>
36-
string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContractComparer;
36+
string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContract;
3737

3838
/// <summary>
3939
/// Generates an INSERT query for inserting data into a database table.
4040
/// </summary>
41-
/// <typeparam name="T">The type of entity that implements IDataContractComparer.</typeparam>
41+
/// <typeparam name="T">The type of entity that implements IDataContract.</typeparam>
4242
/// <param name="entity">The entity representing the data to be inserted.</param>
4343
/// <param name="keyColumns">The list of key columns used for insertion.</param>
4444
/// <param name="excludedColumns">The list of columns to be excluded from the insertion.</param>
4545
/// <returns>A string representing the generated INSERT query.</returns>
46-
string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContractComparer;
46+
string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContract;
4747

4848
/// <summary>
4949
/// Generates a SQL comment.
@@ -55,11 +55,11 @@ public interface IQueryGenerator : IDisposable
5555
/// <summary>
5656
/// Gets a condition for use in a SQL WHERE clause based on the entity and key columns.
5757
/// </summary>
58-
/// <typeparam name="T">The type of entity that implements IDataContractComparer.</typeparam>
58+
/// <typeparam name="T">The type of entity that implements IDataContract.</typeparam>
5959
/// <param name="entity">The entity for which the condition is generated.</param>
6060
/// <param name="keyColumns">The list of key columns used to create the condition.</param>
6161
/// <returns>A string representing the generated condition for a SQL WHERE clause.</returns>
62-
List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContractComparer;
62+
List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContract;
6363

6464
/// <summary>
6565
/// Escapes special characters in the input to make it SQL-safe.

src/Manager/CacheManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,13 @@ public static PropertyInfo[] GetComparableProperties(Type type)
261261
return (PropertyInfo[])_properties;
262262
}
263263

264-
var keyProps = GetKeyColumns(type);
264+
var keyProps = GetTypeProperties(type)
265+
.Where(prop =>
266+
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
267+
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty &&
268+
!((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).IsComparable
269+
).Select(prop => prop.Name);
270+
265271
var excludeProps = GetExcludedColumns(type);
266272

267273
_properties = GetTypeProperties(type).Where(prop => !keyProps.Contains(prop.Name) && !excludeProps.Contains(prop.Name)).ToArray();

src/QueryGenerationManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ public QueryGenerationManager(IQueryGenerator querryGenerator)
2424

2525
#region Public Methods
2626
/// <inheritdoc />
27-
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName) where T : IDataContractComparer
27+
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName) where T : IDataContract
2828
{
2929
return _querryGenerator.GenerateSelectQuery<T>(tableName, listOfColumns, schemaName);
3030
}
3131

3232
/// <inheritdoc />
33-
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, Dictionary<string, object> editedProperties) where T : IDataContractComparer
33+
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, (string propName, object propValue)[] editedProperties) where T : IDataContract
3434
{
3535
return _querryGenerator.GenerateUpdateQuery<T>(DataContract, keyColumns, excludedColumns, editedProperties);
3636
}
3737

3838
/// <inheritdoc />
39-
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContractComparer
39+
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContract
4040
{
4141
return _querryGenerator.GenerateDeleteQuery<T>(entity, keyColumns);
4242
}
4343

4444
/// <inheritdoc />
45-
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContractComparer
45+
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContract
4646
{
4747
return _querryGenerator.GenerateInsertQuery<T>(entity, keyColumns, excludedColumns);
4848
}
@@ -54,7 +54,7 @@ public string GenerateComment(string comment)
5454
}
5555

5656
/// <inheritdoc />
57-
public List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContractComparer
57+
public List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContract
5858
{
5959
return _querryGenerator.GetCondition<T>(entity, keyColumns);
6060
}

0 commit comments

Comments
 (0)