Skip to content

Commit

Permalink
Move SQL Server logic to outside DataAccess.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
gumbarros committed Dec 3, 2024
1 parent bcca7af commit a2ca5ed
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 225 deletions.
225 changes: 84 additions & 141 deletions src/Commons/Data/DataAccess.cs

Large diffs are not rendered by default.

85 changes: 16 additions & 69 deletions src/Commons/Data/DataAccessAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ public async Task<DataTable> GetDataTableAsync(DataAccessCommand command, Cancel
var dataTable = new DataTable();
dataTable.Load(reader);

foreach (var parameter in command.Parameters)
{
if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
parameter.Value = dbCommand.Parameters[parameter.Name].Value;
}
SetOutputParameters(command, dbCommand);

return dataTable;
}
Expand Down Expand Up @@ -93,11 +89,7 @@ public async Task<DataSet> GetDataSetAsync(DataAccessCommand command, Cancellati
index++;
} while(!reader.IsClosed);

foreach (var parameter in command.Parameters)
{
if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
parameter.Value = dbCommand.Parameters[parameter.Name].Value;
}
SetOutputParameters(command, dbCommand);

return dataSet;
}
Expand All @@ -116,54 +108,46 @@ public async Task<DataSet> GetDataSetAsync(DataAccessCommand command, Cancellati
}

/// <inheritdoc cref="GetResult(DataAccessCommand)"/>
public async Task<object?> GetResultAsync(DataAccessCommand cmd, CancellationToken cancellationToken = default)
public async Task<object?> GetResultAsync(DataAccessCommand command, CancellationToken cancellationToken = default)
{
object? scalarResult;
try
{
using var dbCommand = CreateDbCommand(cmd);
using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = await CreateConnectionAsync(cancellationToken);
using (dbCommand.Connection)
{
scalarResult = await dbCommand.ExecuteScalarAsync(cancellationToken);

foreach (var parameter in cmd.Parameters)
{
if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
parameter.Value = dbCommand.Parameters[parameter.Name].Value;
}
SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
{
throw GetDataAccessException(ex, cmd);
throw GetDataAccessException(ex, command);
}

return scalarResult;
}

/// <inheritdoc cref="SetCommand(DataAccessCommand)"/>
public async Task<int> SetCommandAsync(DataAccessCommand cmd, CancellationToken cancellationToken = default)
public async Task<int> SetCommandAsync(DataAccessCommand command, CancellationToken cancellationToken = default)
{
int rowsAffected;
try
{
using var dbCommand = CreateDbCommand(cmd);
using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = await CreateConnectionAsync(cancellationToken);
using (dbCommand.Connection)
{
rowsAffected = await dbCommand.ExecuteNonQueryAsync(cancellationToken);

foreach (var parameter in cmd.Parameters)
{
if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
parameter.Value = dbCommand.Parameters[parameter.Name].Value;
}
SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
{
throw GetDataAccessException(ex, cmd);
throw GetDataAccessException(ex, command);
}

return rowsAffected;
Expand Down Expand Up @@ -254,11 +238,7 @@ await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow, cancellationToken)
}
}

foreach (var parameter in command.Parameters)
{
if (parameter.Direction is ParameterDirection.Output or ParameterDirection.InputOutput)
parameter.Value = dbCommand.Parameters[parameter.Name].Value;
}
SetOutputParameters(command, dbCommand);
}
}
catch (Exception ex)
Expand All @@ -269,13 +249,14 @@ await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow, cancellationToken)
return result;
}

public async Task<List<Dictionary<string, object?>>> GetDictionaryListAsync(DataAccessCommand cmd, CancellationToken cancellationToken = default)

public async Task<List<Dictionary<string, object?>>> GetDictionaryListAsync(DataAccessCommand command, CancellationToken cancellationToken = default)
{
var dictionaryList = new List<Dictionary<string, object?>>();

try
{
using var dbCommand = CreateDbCommand(cmd);
using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = await CreateConnectionAsync(cancellationToken);

using var connection = dbCommand.Connection;
Expand Down Expand Up @@ -304,27 +285,15 @@ await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow, cancellationToken)
}
}

foreach (var param in cmd.Parameters.Where(param =>
param.Direction is ParameterDirection.Output or ParameterDirection.InputOutput))
{
param.Value = dbCommand.Parameters[param.Name].Value;
}
SetOutputParameters(command, dbCommand);
}
catch (Exception ex)
{
throw GetDataAccessException(ex, cmd);
throw GetDataAccessException(ex, command);
}

return dictionaryList;
}

/// <inheritdoc cref="TableExists"/>
public async Task<bool> TableExistsAsync(string tableName, CancellationToken cancellationToken = default)
{
var command = GetTableExistsCommand(tableName);
var result = await GetResultAsync(command, cancellationToken);
return (int)result! == 1;
}

/// <inheritdoc cref="TryConnection"/>
public async Task<ConnectionResult> TryConnectionAsync(CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -401,26 +370,4 @@ public async Task<bool> ExecuteBatchAsync(string script, CancellationToken cance
await SetCommandAsync(sqlList, cancellationToken);
return true;
}

public async Task<bool> ColumnExistsAsync(string tableName, string columnName, CancellationToken cancellationToken = default)
{
var command = GetColumnExistsCommand(tableName, columnName);
try
{
using var dbCommand = CreateDbCommand(command);
dbCommand.Connection = await CreateConnectionAsync(cancellationToken);
using (dbCommand.Connection)
{
using (var reader =
await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleRow, cancellationToken))
{
return await reader.ReadAsync(cancellationToken);
}
}
}
catch (Exception ex)
{
throw GetDataAccessException(ex, command);
}
}
}
10 changes: 10 additions & 0 deletions src/Commons/Data/Entity/Providers/EntityProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JJMasterData.Commons.Configuration.Options;
using JJMasterData.Commons.Data.Entity.Models;
Expand Down Expand Up @@ -300,4 +301,13 @@ internal DataAccess GetDataAccess(Guid? connectionId)
var connection = Options.GetConnectionString(connectionId);
return new DataAccess(connection.Connection, connection.ConnectionProvider);
}

public abstract bool TableExists(string tableName, Guid? connectionId = null);
public abstract Task<bool> TableExistsAsync(string tableName, Guid? connectionId = null, CancellationToken cancellationToken = default);

public abstract Task<bool> ColumnExistsAsync(
string tableName,
string columnName,
Guid? connectionId = null,
CancellationToken cancellationToken = default);
}
18 changes: 18 additions & 0 deletions src/Commons/Data/Entity/Providers/OracleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JJMasterData.Commons.Configuration.Options;
using JJMasterData.Commons.Data.Entity.Models;
Expand Down Expand Up @@ -802,6 +803,23 @@ protected override DataAccessCommand GetInsertOrReplaceCommand(Element element,
return GetCommandWrite(string.Empty, element, values);
}

public override bool TableExists(string tableName, Guid? connectionId = null)
{
throw new NotImplementedException();
}

public override Task<bool> TableExistsAsync(string tableName, Guid? connectionId = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override Task<bool> ColumnExistsAsync(string tableName, string columnName, Guid? connectionId = null,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}


public override string GetAlterTableScript(Element element, IEnumerable<ElementField> fields)
{
return "Not implemented";
Expand Down
17 changes: 17 additions & 0 deletions src/Commons/Data/Entity/Providers/SQLiteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JJMasterData.Commons.Configuration.Options;
using JJMasterData.Commons.Data.Entity.Models;
Expand Down Expand Up @@ -269,6 +270,22 @@ protected override DataAccessCommand GetInsertOrReplaceCommand(Element element,
return GetScriptInsert(element, values, true);
}

public override bool TableExists(string tableName, Guid? connectionId = null)
{
throw new NotImplementedException();
}

public override Task<bool> TableExistsAsync(string tableName, Guid? connectionId = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override Task<bool> ColumnExistsAsync(string tableName, string columnName, Guid? connectionId = null,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public override DataAccessCommand GetReadCommand(Element element, EntityParameters entityParameters,
DataAccessParameter totalOfRecordsParameter)
{
Expand Down
62 changes: 61 additions & 1 deletion src/Commons/Data/Entity/Providers/SqlServer/SqlServerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using JJMasterData.Commons.Configuration.Options;
using JJMasterData.Commons.Data.Entity.Models;
Expand Down Expand Up @@ -301,7 +302,7 @@ public override async Task<Element> GetElementFromTableAsync(string tableName, G
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

if (!await dataAccess.TableExistsAsync(tableName))
if (!await TableExistsAsync(tableName))
throw new JJMasterDataException($"Table {tableName} not found");

var element = new Element
Expand Down Expand Up @@ -353,4 +354,63 @@ public override async Task<Element> GetElementFromTableAsync(string tableName, G

return element;
}

/// <summary>
/// Check if table exists in the database
/// </summary>
public override bool TableExists(string tableName, Guid? connectionId = null)
{
var dataAccess = GetDataAccess(connectionId);
var result = dataAccess.GetResult(GetTableExistsCommand(tableName));
return result as int? == 1;
}

/// <inheritdoc cref="TableExists"/>
public override async Task<bool> TableExistsAsync(string tableName, Guid? connectionId = null, CancellationToken cancellationToken = default)
{
var dataAccess = GetDataAccess(connectionId);
var command = GetTableExistsCommand(tableName);
var result = await dataAccess.GetResultAsync(command, cancellationToken);
return result as int? == 1;
}

public override async Task<bool> ColumnExistsAsync(string tableName, string columnName, Guid? connectionId = null, CancellationToken cancellationToken = default)
{
var dataAccess = GetDataAccess(connectionId);
var command = GetColumnExistsCommand(tableName, columnName);
var result = await dataAccess.GetResultAsync(command, cancellationToken);
return result as int? == 1;
}

private static DataAccessCommand GetTableExistsCommand(string table)
{
const string sql = "SELECT COUNT(1) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @Table";
var command = new DataAccessCommand
{
Sql = sql,
Parameters =
{
new DataAccessParameter
{
Name = "@Table",
Value = table
}
}
};

return command;
}

private static DataAccessCommand GetColumnExistsCommand(string tableName, string columnName)
{
var command = new DataAccessCommand
{
Sql = "SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName"
};

command.AddParameter("@TableName", tableName, DbType.AnsiString);
command.AddParameter("@ColumnName", columnName, DbType.AnsiString);

return command;
}
}
9 changes: 3 additions & 6 deletions src/Commons/Data/Entity/Repository/EntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ public Task<Element> GetElementFromTableAsync(string tableName, Guid? connection

public Task<bool> TableExistsAsync(string tableName, Guid? connectionId = null)
{
var dataAccess = GetDataAccess(connectionId);
return dataAccess.TableExistsAsync(tableName);
return provider.TableExistsAsync(tableName);
}

public bool TableExists(string tableName, Guid? connectionId = null)
{
var dataAccess = GetDataAccess(connectionId);
return dataAccess.TableExists(tableName);
return provider.TableExists(tableName, connectionId);
}

public async Task SetCommandAsync(DataAccessCommand command, Guid? connectionId = null)
Expand All @@ -97,8 +95,7 @@ public Task<int> SetCommandListAsync(IEnumerable<DataAccessCommand> commandList,

private Task<bool> ColumnExistsAsync(string tableName, string columnName, Guid? connectionId = null)
{
var dataAccess = GetDataAccess(connectionId);
return dataAccess.ColumnExistsAsync(tableName, columnName);
return provider.ColumnExistsAsync(tableName, columnName, connectionId);
}

public Task<bool> ExecuteBatchAsync(string script, Guid? connectionId = null)
Expand Down
8 changes: 0 additions & 8 deletions test/Commons.Test/Data/DataAccessTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ private void ConfigureSeedValues()
DataAccess.ExecuteBatch(sql);
}

[Theory]
[InlineData(TableName,true)]
[InlineData("Foo",false)]
public async Task TableExistsTest(string table, bool exists)
{
Assert.Equal(exists, await DataAccess.TableExistsAsync(table));
}

[Fact]
public async Task GetDataTableTest()
{
Expand Down

0 comments on commit a2ca5ed

Please sign in to comment.