Skip to content

Commit

Permalink
Sync Algorithm updated for better performance on large data
Browse files Browse the repository at this point in the history
  • Loading branch information
RohitM-IN committed Jan 12, 2024
1 parent 176b67d commit cb019dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
17 changes: 12 additions & 5 deletions src/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

namespace DbSyncKit.PostgreSQL
{
/// <summary>
/// Represents a connection to a database, implementing the <see cref="IDatabase"/> interface.
/// </summary>
/// <remarks>
/// This class provides a generic representation of a database connection and includes methods
/// defined in the <see cref="IDatabase"/> interface for executing queries and managing transactions.
/// </remarks>
public class Connection : IDatabase
{
#region Declaration
Expand Down Expand Up @@ -51,7 +58,7 @@ public Connection(string host, int port, string database, string userID, string
/// <returns>A string representing the PostgreSQL connection string.</returns>
public string GetConnectionString()
{
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder
NpgsqlConnectionStringBuilder builder = new()
{
Host = Host,
Port = Port,
Expand All @@ -74,12 +81,12 @@ public DataSet ExecuteQuery(string query, string tableName)
{
try
{
using (NpgsqlConnection npgSqlConnection = new NpgsqlConnection(GetConnectionString()))
using (NpgsqlConnection npgSqlConnection = new(GetConnectionString()))
{
npgSqlConnection.Open();

using (NpgsqlDataAdapter npgSqlDataAdapter = new NpgsqlDataAdapter(query, npgSqlConnection))
using (DataSet dataset = new DataSet())
using (NpgsqlDataAdapter npgSqlDataAdapter = new(query, npgSqlConnection))
using (DataSet dataset = new())
{
npgSqlDataAdapter.Fill(dataset, tableName);
return dataset;
Expand All @@ -101,7 +108,7 @@ public bool TestConnection()
{
try
{
using (NpgsqlConnection npgSqlConnection = new NpgsqlConnection(GetConnectionString()))
using (NpgsqlConnection npgSqlConnection = new(GetConnectionString()))
{
npgSqlConnection.Open();
npgSqlConnection.Close();
Expand Down
16 changes: 8 additions & 8 deletions src/QueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public QueryGenerator()
/// <param name="schemaName">Optional schema name, default is 'public'.</param>
/// <returns>Select Query in string.</returns>
/// <exception cref="ArgumentException">Thrown when table name or columns are null or empty.</exception>
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName) where T : IDataContractComparer
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName) where T : IDataContract
{
if (string.IsNullOrEmpty(tableName) || listOfColumns == null || listOfColumns.Count == 0)
{
Expand Down Expand Up @@ -78,11 +78,11 @@ public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumn
/// <param name="excludedColumns">List of excluded columns.</param>
/// <param name="editedProperties">Dictionary of edited properties.</param>
/// <returns>Update Query in string.</returns>
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, Dictionary<string, object> editedProperties) where T : IDataContractComparer
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, (string propName, object propValue)[] editedProperties) where T : IDataContract
{
string tableName = GetTableName<T>();
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
List<string> setClause = editedProperties.Select(kv => $"{EscapeColumn(kv.Key)} = '{EscapeValue(kv.Value)}'").ToList();
List<string> setClause = editedProperties.Select(kv => $"{EscapeColumn(kv.propName)} = '{EscapeValue(kv.propValue)}'").ToList();
List<string> condition = GetCondition(DataContract, keyColumns);

return _template.UpdateTemplate.Render(new TemplateContext(new
Expand All @@ -101,7 +101,7 @@ public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, Li
/// <param name="entity">The entity to be deleted.</param>
/// <param name="keyColumns">List of key columns.</param>
/// <returns>Delete Query in string.</returns>
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContractComparer
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T : IDataContract
{
string tableName = GetTableName<T>();
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
Expand All @@ -123,7 +123,7 @@ public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns) where T
/// <param name="keyColumns">List of key columns.</param>
/// <param name="excludedColumns">List of excluded columns.</param>
/// <returns>Insert Query in string.</returns>
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContractComparer
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns) where T : IDataContract
{
string tableName = GetTableName<T>();
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
Expand Down Expand Up @@ -183,11 +183,11 @@ public void Dispose()
/// <summary>
/// Generates a SQL WHERE clause based on the specified entity and key columns.
/// </summary>
/// <typeparam name="T">The type of the entity that implements IDataContractComparer.</typeparam>
/// <typeparam name="T">The type of the entity that implements IDataContract.</typeparam>
/// <param name="entity">The entity for which the condition is generated.</param>
/// <param name="keyColumns">The list of key columns used to create the condition.</param>
/// <returns>A string representing the SQL WHERE clause based on the key columns of the entity.</returns>
public List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContractComparer
public List<string> GetCondition<T>(T entity, List<string> keyColumns) where T : IDataContract
{
Type entityType = typeof(T);
PropertyInfo[] keyProperties = GetKeyProperties<T>();
Expand Down Expand Up @@ -218,7 +218,7 @@ public string EscapeColumn(string? input)
if(input is string && input.Contains(" "))
return $"\"{input}\"";

return input.ToString();
return input?.ToString() ?? string.Empty;
}

/// <summary>
Expand Down

0 comments on commit cb019dc

Please sign in to comment.