Skip to content

Commit cb019dc

Browse files
committed
Sync Algorithm updated for better performance on large data
1 parent 176b67d commit cb019dc

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/Connection.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
namespace DbSyncKit.PostgreSQL
77
{
8+
/// <summary>
9+
/// Represents a connection to a database, implementing the <see cref="IDatabase"/> interface.
10+
/// </summary>
11+
/// <remarks>
12+
/// This class provides a generic representation of a database connection and includes methods
13+
/// defined in the <see cref="IDatabase"/> interface for executing queries and managing transactions.
14+
/// </remarks>
815
public class Connection : IDatabase
916
{
1017
#region Declaration
@@ -51,7 +58,7 @@ public Connection(string host, int port, string database, string userID, string
5158
/// <returns>A string representing the PostgreSQL connection string.</returns>
5259
public string GetConnectionString()
5360
{
54-
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder
61+
NpgsqlConnectionStringBuilder builder = new()
5562
{
5663
Host = Host,
5764
Port = Port,
@@ -74,12 +81,12 @@ public DataSet ExecuteQuery(string query, string tableName)
7481
{
7582
try
7683
{
77-
using (NpgsqlConnection npgSqlConnection = new NpgsqlConnection(GetConnectionString()))
84+
using (NpgsqlConnection npgSqlConnection = new(GetConnectionString()))
7885
{
7986
npgSqlConnection.Open();
8087

81-
using (NpgsqlDataAdapter npgSqlDataAdapter = new NpgsqlDataAdapter(query, npgSqlConnection))
82-
using (DataSet dataset = new DataSet())
88+
using (NpgsqlDataAdapter npgSqlDataAdapter = new(query, npgSqlConnection))
89+
using (DataSet dataset = new())
8390
{
8491
npgSqlDataAdapter.Fill(dataset, tableName);
8592
return dataset;
@@ -101,7 +108,7 @@ public bool TestConnection()
101108
{
102109
try
103110
{
104-
using (NpgsqlConnection npgSqlConnection = new NpgsqlConnection(GetConnectionString()))
111+
using (NpgsqlConnection npgSqlConnection = new(GetConnectionString()))
105112
{
106113
npgSqlConnection.Open();
107114
npgSqlConnection.Close();

src/QueryGenerator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public QueryGenerator()
4444
/// <param name="schemaName">Optional schema name, default is 'public'.</param>
4545
/// <returns>Select Query in string.</returns>
4646
/// <exception cref="ArgumentException">Thrown when table name or columns are null or empty.</exception>
47-
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName) where T : IDataContractComparer
47+
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName) where T : IDataContract
4848
{
4949
if (string.IsNullOrEmpty(tableName) || listOfColumns == null || listOfColumns.Count == 0)
5050
{
@@ -78,11 +78,11 @@ public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumn
7878
/// <param name="excludedColumns">List of excluded columns.</param>
7979
/// <param name="editedProperties">Dictionary of edited properties.</param>
8080
/// <returns>Update Query in string.</returns>
81-
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, Dictionary<string, object> editedProperties) where T : IDataContractComparer
81+
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, (string propName, object propValue)[] editedProperties) where T : IDataContract
8282
{
8383
string tableName = GetTableName<T>();
8484
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
85-
List<string> setClause = editedProperties.Select(kv => $"{EscapeColumn(kv.Key)} = '{EscapeValue(kv.Value)}'").ToList();
85+
List<string> setClause = editedProperties.Select(kv => $"{EscapeColumn(kv.propName)} = '{EscapeValue(kv.propValue)}'").ToList();
8686
List<string> condition = GetCondition(DataContract, keyColumns);
8787

8888
return _template.UpdateTemplate.Render(new TemplateContext(new
@@ -101,7 +101,7 @@ public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, Li
101101
/// <param name="entity">The entity to be deleted.</param>
102102
/// <param name="keyColumns">List of key columns.</param>
103103
/// <returns>Delete Query in string.</returns>
104-
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContractComparer
104+
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContract
105105
{
106106
string tableName = GetTableName<T>();
107107
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
@@ -123,7 +123,7 @@ public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T
123123
/// <param name="keyColumns">List of key columns.</param>
124124
/// <param name="excludedColumns">List of excluded columns.</param>
125125
/// <returns>Insert Query in string.</returns>
126-
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContractComparer
126+
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContract
127127
{
128128
string tableName = GetTableName<T>();
129129
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
@@ -183,11 +183,11 @@ public void Dispose()
183183
/// <summary>
184184
/// Generates a SQL WHERE clause based on the specified entity and key columns.
185185
/// </summary>
186-
/// <typeparam name="T">The type of the entity that implements IDataContractComparer.</typeparam>
186+
/// <typeparam name="T">The type of the entity that implements IDataContract.</typeparam>
187187
/// <param name="entity">The entity for which the condition is generated.</param>
188188
/// <param name="keyColumns">The list of key columns used to create the condition.</param>
189189
/// <returns>A string representing the SQL WHERE clause based on the key columns of the entity.</returns>
190-
public List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContractComparer
190+
public List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContract
191191
{
192192
Type entityType = typeof(T);
193193
PropertyInfo[] keyProperties = GetKeyProperties<T>();
@@ -218,7 +218,7 @@ public string EscapeColumn(string? input)
218218
if(input is string && input.Contains(" "))
219219
return $"\"{input}\"";
220220

221-
return input.ToString();
221+
return input?.ToString() ?? string.Empty;
222222
}
223223

224224
/// <summary>

0 commit comments

Comments
 (0)