Skip to content

Commit

Permalink
Merge branch 'main' into feature/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
RohitM-IN committed Mar 5, 2024
2 parents 98e6e50 + ec26556 commit d4f567f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 111 deletions.
137 changes: 36 additions & 101 deletions src/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,87 +21,56 @@ public Connection()
}

/// <summary>
/// Initializes a new instance of the <see cref="Connection"/> class with the specified connection parameters.
/// Initializes a new instance of the <see cref="Connection"/> class with the specified parameters.
/// </summary>
/// <param name="dataSource">The data source or IP address.</param>
/// <param name="initialCatalog">The initial catalog or database name.</param>
/// <param name="integratedSecurity">Indicates whether to use integrated security.</param>
/// <param name="userID">The user ID for SQL authentication.</param>
/// <param name="password">The password for SQL authentication.</param>
public Connection(string dataSource, string? initialCatalog, bool integratedSecurity, string? userID = "", string? password = "")
/// <param name="serverAddress">The address of the server.</param>
/// <param name="useSQLAuthentication">Indicates whether to use SQL authentication.</param>
/// <param name="databaseName">The name of the database.</param>
/// <param name="sqlUsername">The username for SQL authentication.</param>
/// <param name="sqlPassword">The password for SQL authentication.</param>
public Connection(string serverAddress, bool useSQLAuthentication, string? databaseName = null, string? sqlUsername = null, string? sqlPassword = null)
{
DataSource = dataSource;
InitialCatalog = initialCatalog;
IntegratedSecurity = integratedSecurity;
UserID = userID;
Password = password;
}
Server = serverAddress;
UseSQLAuthentication = useSQLAuthentication;

if (databaseName != null)
DatabaseName = databaseName;

/// <summary>
/// Initializes a new instance of the <see cref="Connection"/> class with the specified connection parameters.
/// </summary>
/// <param name="server">The server address.</param>
/// <param name="database">The database name.</param>
/// <param name="initialCatalog">The initial catalog or database name.</param>
/// <param name="integratedSecurity">Indicates whether to use integrated security.</param>
/// <param name="userID">The user ID for SQL authentication.</param>
/// <param name="password">The password for SQL authentication.</param>
/// <param name="useServerAddress">Indicates whether to use the server address.</param>
public Connection(string server, string? database, string? initialCatalog, bool integratedSecurity, string? userID, string? password, bool useServerAddress)
{
UseServerAddress = useServerAddress;
Server = server;
Database = database;
IntegratedSecurity = integratedSecurity;
InitialCatalog = initialCatalog;
UserID = userID;
Password = password;
if (useSQLAuthentication)
{
SQLUsername = sqlUsername;
SQLPassword = sqlPassword;
}
}

#endregion


#region Properties
/// <summary>
/// Gets or sets a value indicating whether to use the server address.
/// </summary>
public bool UseServerAddress { get; set; }

/// <summary>
/// Gets or sets the data source or IP address.
/// </summary>
public string? DataSource { get; set; }

/// <summary>
/// Gets or sets the server address.
/// </summary>
public string? Server { get; set; }

/// <summary>
/// Gets or sets the database name.
/// Gets or sets the address of the server.
/// </summary>
public string? Database { get; set; }
private string Server { get; set; }

/// <summary>
/// Gets or sets the initial catalog or database name.
/// Gets or sets a value indicating whether to use SQL authentication.
/// </summary>
public string? InitialCatalog { get; set; }
private bool UseSQLAuthentication { get; set; }

/// <summary>
/// Gets or sets the user ID for SQL authentication.
/// Gets or sets the username for SQL authentication.
/// </summary>
public string? UserID { get; set; }
private string? SQLUsername { get; set; }

/// <summary>
/// Gets or sets the password for SQL authentication.
/// </summary>
public string? Password { get; set; }
private string? SQLPassword { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to use integrated security.
/// Gets or sets the name of the database.
/// </summary>
public bool IntegratedSecurity { get; set; }
private string? DatabaseName { get; set; }

/// <summary>
/// Gets the database provider type, which is MSSQL for this class.
Expand Down Expand Up @@ -148,58 +117,24 @@ public bool TestConnection()
/// <returns>The connection string.</returns>
public string GetConnectionString()
{
var connectionStringBuilder = new SqlConnectionStringBuilder();
SqlConnectionStringBuilder sqlConnectionStringBuilder = new();
sqlConnectionStringBuilder.DataSource = Server;
sqlConnectionStringBuilder.IntegratedSecurity = !UseSQLAuthentication;

if (UseServerAddress)
{
if (Server == null)
{
throw new Exception("Server cannot be null when connecting through server address.");
}
if (DatabaseName != null)
sqlConnectionStringBuilder.InitialCatalog = DatabaseName;

connectionStringBuilder.DataSource = Server;

if (Database != null)
{
connectionStringBuilder.InitialCatalog = Database;
}
}
else
if (UseSQLAuthentication)
{
if (DataSource == null)
{
throw new Exception("DataSource cannot be null");
}

connectionStringBuilder.DataSource = DataSource;

if (InitialCatalog != null)
{
connectionStringBuilder.InitialCatalog = InitialCatalog;
}
sqlConnectionStringBuilder.UserID = SQLUsername;
sqlConnectionStringBuilder.Password = SQLPassword;
}

connectionStringBuilder.IntegratedSecurity = IntegratedSecurity;
sqlConnectionStringBuilder.TrustServerCertificate = true;

if (!IntegratedSecurity)
{
if (UserID == null)
{
throw new Exception("UserID cannot be null when logging in using SQL Authentication.");
}

connectionStringBuilder.UserID = UserID;

if (Password == null)
{
throw new Exception("Password cannot be null when logging in using SQL Authentication.");
}

connectionStringBuilder.Password = Password;
}

return connectionStringBuilder.ConnectionString;
return sqlConnectionStringBuilder.ConnectionString;
}

#endregion

#region Static Method
Expand Down
6 changes: 3 additions & 3 deletions src/DbSyncKit.MSSQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
<Version>1.2.0.0</Version>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.0</FileVersion>
<Version>1.4.0.0</Version>
<PackageProjectUrl>https://dbsynckit.rohit-mahajan.in/</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/RohitM-IN/DbSyncKit</RepositoryUrl>
Expand Down
13 changes: 6 additions & 7 deletions src/QueryGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DbSyncKit.DB.Helper;
using DbSyncKit.DB.Interface;
using DbSyncKit.Templates.Interface;
using DbSyncKit.Templates.MSSQL;
using Fluid;
using System.Reflection;
Expand Down Expand Up @@ -44,7 +44,7 @@ public QueryGenerator()
/// <param name="schemaName">Optional schema name, default is 'dbo'.</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 : IDataContract
public string GenerateSelectQuery<T>(string tableName, List<string> listOfColumns, string schemaName)
{
if (string.IsNullOrEmpty(tableName) || listOfColumns == null || listOfColumns.Count == 0)
{
Expand Down Expand Up @@ -79,7 +79,7 @@ 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, (string propName, object propValue)[] editedProperties) where T : IDataContract
public string GenerateUpdateQuery<T>(T DataContract, List<string> keyColumns, List<string> excludedColumns, (string propName, object propValue)[] editedProperties)
{
string tableName = GetTableName<T>();
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
Expand All @@ -102,7 +102,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 : IDataContract
public string GenerateDeleteQuery<T>(T entity, List<string> keyColumns)
{
string tableName = GetTableName<T>();
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
Expand All @@ -124,7 +124,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 : IDataContract
public string GenerateInsertQuery<T>(T entity, List<string> keyColumns, List<string> excludedColumns)
{
string tableName = GetTableName<T>();
string schemaName = GetTableSchema<T>() ?? DEFAULT_SCHEMA_NAME;
Expand Down Expand Up @@ -184,11 +184,10 @@ 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 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 : IDataContract
public List<string> GetCondition<T>(T entity, List<string> keyColumns)
{
Type entityType = typeof(T);
PropertyInfo[] keyProperties = GetKeyProperties<T>();
Expand Down

0 comments on commit d4f567f

Please sign in to comment.