new sql server data client under .net core 2
This commit is contained in:
@@ -0,0 +1,116 @@
|
|||||||
|
namespace LaaProductionWeb.Data
|
||||||
|
{
|
||||||
|
using LaaProductionWeb.Data.Interfaces;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
public class FluentSQL : IFluentSQL
|
||||||
|
{
|
||||||
|
private readonly string connectionString;
|
||||||
|
|
||||||
|
public FluentSQL(string connectionString)
|
||||||
|
{
|
||||||
|
connectionString.ThrowIfNullOrWhiteSpace(nameof(connectionString));
|
||||||
|
|
||||||
|
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
|
||||||
|
|
||||||
|
this.connectionString = connectionStringBuilder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FluentSQLCommand CreateCommand(string commandText)
|
||||||
|
=> new FluentSQLCommand(this, commandText);
|
||||||
|
|
||||||
|
internal int ExecuteNonQuery(FluentSQLCommand command)
|
||||||
|
{
|
||||||
|
command.ThrowIfNull(nameof(command));
|
||||||
|
|
||||||
|
var rowsAffected = 0;
|
||||||
|
|
||||||
|
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||||
|
{
|
||||||
|
sqlConnection.OpenWithErrorHandling();
|
||||||
|
|
||||||
|
using (var sqlCommand = sqlConnection.CreateCommand())
|
||||||
|
{
|
||||||
|
sqlCommand.CommandText = command.CommandText;
|
||||||
|
|
||||||
|
sqlCommand.Parameters.AddRange(command.Parameters);
|
||||||
|
|
||||||
|
rowsAffected = sqlCommand.ExecuteNonQuery();
|
||||||
|
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rowsAffected;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal IEnumerable<T> ExecuteReader<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
||||||
|
{
|
||||||
|
command.ThrowIfNull(nameof(command));
|
||||||
|
expression.ThrowIfNull(nameof(expression));
|
||||||
|
|
||||||
|
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||||
|
{
|
||||||
|
sqlConnection.OpenWithErrorHandling();
|
||||||
|
|
||||||
|
using (var sqlCommand = sqlConnection.CreateCommand())
|
||||||
|
{
|
||||||
|
sqlCommand.CommandText = command.CommandText;
|
||||||
|
|
||||||
|
sqlCommand.Parameters.AddRange(command.Parameters);
|
||||||
|
|
||||||
|
using (FluentSQLReader sqlReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess))
|
||||||
|
{
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
|
||||||
|
while (sqlReader.Read())
|
||||||
|
{
|
||||||
|
yield return expression(sqlReader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal T ExecuteScalar<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
||||||
|
{
|
||||||
|
command.ThrowIfNull(nameof(command));
|
||||||
|
expression.ThrowIfNull(nameof(expression));
|
||||||
|
|
||||||
|
var scalarResult = default(T);
|
||||||
|
|
||||||
|
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||||
|
{
|
||||||
|
sqlConnection.OpenWithErrorHandling();
|
||||||
|
|
||||||
|
using (var sqlCommand = sqlConnection.CreateCommand())
|
||||||
|
{
|
||||||
|
sqlCommand.CommandText = command.CommandText;
|
||||||
|
|
||||||
|
sqlCommand.Parameters.AddRange(command.Parameters);
|
||||||
|
|
||||||
|
var scalarObject = sqlCommand.ExecuteScalar();
|
||||||
|
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
|
||||||
|
if (scalarObject is T scalarValue)
|
||||||
|
{
|
||||||
|
scalarResult = scalarValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return scalarResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal T FirstOrDefault<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
||||||
|
=> this
|
||||||
|
.ExecuteReader(command, expression)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
namespace LaaProductionWeb.Data
|
||||||
|
{
|
||||||
|
using LaaProductionWeb.Data.Interfaces;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
public class FluentSQLCommand : IFluentSQLCommand
|
||||||
|
{
|
||||||
|
private readonly FluentSQL fluentSql;
|
||||||
|
private readonly IDictionary<string, SqlParameter> parameters;
|
||||||
|
|
||||||
|
public FluentSQLCommand(FluentSQL fluentSql, string commandText)
|
||||||
|
{
|
||||||
|
this.fluentSql = fluentSql.EnsureNotNull(nameof(fluentSql));
|
||||||
|
this.CommandText = commandText.EnsureNotNullOrWhiteSpace(nameof(commandText));
|
||||||
|
this.parameters = new Dictionary<string, SqlParameter>();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal string CommandText { get; }
|
||||||
|
|
||||||
|
internal SqlParameter[] Parameters
|
||||||
|
=> this.parameters.Values.ToArray();
|
||||||
|
|
||||||
|
public int ExecuteNonQuery()
|
||||||
|
=> this.fluentSql.ExecuteNonQuery(this);
|
||||||
|
|
||||||
|
public IEnumerable<T> ExecuteReader<T>(Func<IFluentSQLReader, T> expression)
|
||||||
|
=> this.fluentSql.ExecuteReader(this, expression);
|
||||||
|
|
||||||
|
public T ExecuteScalar<T>(Func<IFluentSQLReader, T> expression)
|
||||||
|
=> this.fluentSql.ExecuteScalar(this, expression);
|
||||||
|
|
||||||
|
public T FirstOrDefault<T>(Func<IFluentSQLReader, T> expression)
|
||||||
|
=> this.fluentSql.FirstOrDefault(this, expression);
|
||||||
|
|
||||||
|
public IFluentSQLCommand SetParameter(string name, object value)
|
||||||
|
{
|
||||||
|
name.ThrowIfNullOrWhiteSpace(nameof(name));
|
||||||
|
|
||||||
|
if (value is null)
|
||||||
|
{
|
||||||
|
value = DBNull.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parameter = new SqlParameter(name, value);
|
||||||
|
|
||||||
|
this.parameters[name] = parameter;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IFluentSQLCommand SetParameters(IEnumerable<KeyValuePair<string, object>> parameters)
|
||||||
|
{
|
||||||
|
foreach (var kvp in parameters)
|
||||||
|
{
|
||||||
|
this.SetParameter(kvp.Key, kvp.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
namespace LaaProductionWeb.Data
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
internal static class FluentSQLExtensions
|
||||||
|
{
|
||||||
|
public static T EnsureNotNull<T>(this T value, [CallerMemberName] string memberName = "")
|
||||||
|
{
|
||||||
|
value.ThrowIfNull(memberName);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string EnsureNotNullOrWhiteSpace(this string value, [CallerMemberName] string memberName = "")
|
||||||
|
{
|
||||||
|
value.ThrowIfNullOrWhiteSpace(memberName);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ThrowIfNull<T>(this T value, [CallerMemberName] string memberName = "")
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{memberName} should not be null.", memberName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ThrowIfNullOrWhiteSpace(this string value, [CallerMemberName] string memberName = "")
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{memberName} should not be null or white space.", memberName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void OpenWithErrorHandling(this SqlConnection sqlConnection)
|
||||||
|
{
|
||||||
|
sqlConnection.ThrowIfNull(nameof(sqlConnection));
|
||||||
|
|
||||||
|
sqlConnection.FireInfoMessageEventOnUserErrors = true;
|
||||||
|
sqlConnection.InfoMessage += SqlConnectionInfoMessage;
|
||||||
|
|
||||||
|
sqlConnection.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void SqlConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
|
||||||
|
{
|
||||||
|
// TODO: Log error message to the caller or elsewhere.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
namespace LaaProductionWeb.Data
|
||||||
|
{
|
||||||
|
using LaaProductionWeb.Data.Interfaces;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
|
public class FluentSQLReader : IFluentSQLReader, IDisposable
|
||||||
|
{
|
||||||
|
private readonly SqlDataReader sqlDataReader;
|
||||||
|
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
private FluentSQLReader(SqlDataReader sqlDataReader)
|
||||||
|
=> this.sqlDataReader = sqlDataReader.EnsureNotNull(nameof(sqlDataReader));
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
this.sqlDataReader.Close();
|
||||||
|
this.sqlDataReader.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool GetBool()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return this.sqlDataReader.GetBoolean(this.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(bool);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] GetBytes()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
var bytes = default(byte[]);
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
bytes = this.sqlDataReader.GetFieldValue<byte[]>(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes ?? Array.Empty<byte>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTime GetDate()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return this.sqlDataReader.GetDateTime(this.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetInt()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return this.sqlDataReader.GetInt32(this.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long GetLong()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return this.sqlDataReader.GetInt64(this.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(long);
|
||||||
|
}
|
||||||
|
|
||||||
|
public short GetSmallint()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return this.sqlDataReader.GetInt16(this.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(short);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetString()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return $"{this.sqlDataReader.GetValue(this.index)}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T GetValue<T>()
|
||||||
|
{
|
||||||
|
this.index++;
|
||||||
|
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
if (this.sqlDataReader.GetValue(this.index) is T value)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object GetValue(string column)
|
||||||
|
{
|
||||||
|
column.EnsureNotNullOrWhiteSpace(nameof(column));
|
||||||
|
|
||||||
|
var colIndex = this.sqlDataReader.GetOrdinal(column);
|
||||||
|
|
||||||
|
if (colIndex >= 0 && colIndex < this.sqlDataReader.FieldCount)
|
||||||
|
{
|
||||||
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
||||||
|
{
|
||||||
|
return this.sqlDataReader.GetValue(this.index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return default(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal bool Read()
|
||||||
|
{
|
||||||
|
this.index = -1;
|
||||||
|
|
||||||
|
return this.sqlDataReader.Read();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator FluentSQLReader(SqlDataReader sqlDataReader)
|
||||||
|
=> new FluentSQLReader(sqlDataReader);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace LaaProductionWeb.Data.Interfaces
|
||||||
|
{
|
||||||
|
public interface IFluentSQL
|
||||||
|
{
|
||||||
|
FluentSQLCommand CreateCommand(string commandText);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
namespace LaaProductionWeb.Data.Interfaces
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
public interface IFluentSQLCommand
|
||||||
|
{
|
||||||
|
int ExecuteNonQuery();
|
||||||
|
|
||||||
|
IEnumerable<T> ExecuteReader<T>(Func<IFluentSQLReader, T> expression);
|
||||||
|
|
||||||
|
T ExecuteScalar<T>(Func<IFluentSQLReader, T> expression);
|
||||||
|
|
||||||
|
T FirstOrDefault<T>(Func<IFluentSQLReader, T> expression);
|
||||||
|
|
||||||
|
IFluentSQLCommand SetParameter(string name, object value);
|
||||||
|
|
||||||
|
IFluentSQLCommand SetParameters(IEnumerable<KeyValuePair<string, object>> parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace LaaProductionWeb.Data.Interfaces
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
|
||||||
|
public interface IFluentSQLReader
|
||||||
|
{
|
||||||
|
bool GetBool();
|
||||||
|
|
||||||
|
byte[] GetBytes();
|
||||||
|
|
||||||
|
DateTime GetDate();
|
||||||
|
|
||||||
|
int GetInt();
|
||||||
|
|
||||||
|
long GetLong();
|
||||||
|
|
||||||
|
short GetSmallint();
|
||||||
|
|
||||||
|
string GetString();
|
||||||
|
|
||||||
|
T GetValue<T>();
|
||||||
|
|
||||||
|
object GetValue(string column);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
public FluentSQL(string connectionString)
|
public FluentSQL(string connectionString)
|
||||||
{
|
{
|
||||||
|
connectionString.ThrowIfNullOrWhiteSpace(nameof(connectionString));
|
||||||
|
|
||||||
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
|
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
|
||||||
|
|
||||||
this.connectionString = connectionStringBuilder.ToString();
|
this.connectionString = connectionStringBuilder.ToString();
|
||||||
@@ -24,7 +26,7 @@
|
|||||||
|
|
||||||
internal int ExecuteNonQuery(FluentSQLCommand command)
|
internal int ExecuteNonQuery(FluentSQLCommand command)
|
||||||
{
|
{
|
||||||
command.ThrowIfNull();
|
command.ThrowIfNull(nameof(command));
|
||||||
|
|
||||||
var rowsAffected = 0;
|
var rowsAffected = 0;
|
||||||
|
|
||||||
@@ -49,8 +51,8 @@
|
|||||||
|
|
||||||
internal IEnumerable<T> ExecuteReader<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
internal IEnumerable<T> ExecuteReader<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
||||||
{
|
{
|
||||||
command.ThrowIfNull();
|
command.ThrowIfNull(nameof(command));
|
||||||
expression.ThrowIfNull();
|
expression.ThrowIfNull(nameof(expression));
|
||||||
|
|
||||||
using (var sqlConnection = new SqlConnection(this.connectionString))
|
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||||
{
|
{
|
||||||
@@ -77,8 +79,8 @@
|
|||||||
|
|
||||||
internal T ExecuteScalar<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
internal T ExecuteScalar<T>(FluentSQLCommand command, Func<IFluentSQLReader, T> expression)
|
||||||
{
|
{
|
||||||
command.ThrowIfNull();
|
command.ThrowIfNull(nameof(command));
|
||||||
expression.ThrowIfNull();
|
expression.ThrowIfNull(nameof(expression));
|
||||||
|
|
||||||
var scalarResult = default(T);
|
var scalarResult = default(T);
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
|
|
||||||
public FluentSQLCommand(FluentSQL fluentSql, string commandText)
|
public FluentSQLCommand(FluentSQL fluentSql, string commandText)
|
||||||
{
|
{
|
||||||
this.fluentSql = fluentSql.EnsureNotNull();
|
this.fluentSql = fluentSql.EnsureNotNull(nameof(fluentSql));
|
||||||
this.CommandText = commandText.EnsureNotNullOrWhiteSpace();
|
this.CommandText = commandText.EnsureNotNullOrWhiteSpace(nameof(commandText));
|
||||||
this.parameters = new Dictionary<string, SqlParameter>();
|
this.parameters = new Dictionary<string, SqlParameter>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
|
|
||||||
public IFluentSQLCommand SetParameter(string name, object value)
|
public IFluentSQLCommand SetParameter(string name, object value)
|
||||||
{
|
{
|
||||||
name.ThrowIfNullOrWhiteSpace();
|
name.ThrowIfNullOrWhiteSpace(nameof(name));
|
||||||
|
|
||||||
if (value is null)
|
if (value is null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,13 +38,15 @@
|
|||||||
|
|
||||||
internal static void OpenWithErrorHandling(this SqlConnection sqlConnection)
|
internal static void OpenWithErrorHandling(this SqlConnection sqlConnection)
|
||||||
{
|
{
|
||||||
|
sqlConnection.ThrowIfNull(nameof(sqlConnection));
|
||||||
|
|
||||||
sqlConnection.FireInfoMessageEventOnUserErrors = true;
|
sqlConnection.FireInfoMessageEventOnUserErrors = true;
|
||||||
sqlConnection.InfoMessage += SqlConnectionInfoMessage;
|
sqlConnection.InfoMessage += SqlConnectionInfoMessage;
|
||||||
|
|
||||||
sqlConnection.Open();
|
sqlConnection.Open();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SqlConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
|
internal static void SqlConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
|
||||||
{
|
{
|
||||||
// TODO: Log error message to the caller or elsewhere.
|
// TODO: Log error message to the caller or elsewhere.
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
private FluentSQLReader(SqlDataReader sqlDataReader)
|
private FluentSQLReader(SqlDataReader sqlDataReader)
|
||||||
=> this.sqlDataReader = sqlDataReader.EnsureNotNull();
|
=> this.sqlDataReader = sqlDataReader.EnsureNotNull(nameof(sqlDataReader));
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
@@ -123,7 +123,7 @@
|
|||||||
|
|
||||||
public object GetValue(string column)
|
public object GetValue(string column)
|
||||||
{
|
{
|
||||||
column.EnsureNotNullOrWhiteSpace();
|
column.EnsureNotNullOrWhiteSpace(nameof(column));
|
||||||
|
|
||||||
var colIndex = this.sqlDataReader.GetOrdinal(column);
|
var colIndex = this.sqlDataReader.GetOrdinal(column);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
namespace LaaProductionWeb.UnitTests.Data
|
||||||
|
{
|
||||||
|
using LaaProductionWeb.Data;
|
||||||
|
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class FluentSQLTestClass
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Constructor_ShouldThrow_ConnectionString_IsNull()
|
||||||
|
=> Assert.ThrowsException<ArgumentException>(() => new FluentSQL(null));
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Constructor_ShouldThrow_ConnectionString_IsWhiteSpace()
|
||||||
|
=> Assert.ThrowsException<ArgumentException>(() => new FluentSQL(string.Empty));
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Constructor_ShouldThrow_ConnectionString_KeyNotFound()
|
||||||
|
=> Assert.ThrowsException<ArgumentException>(() => new FluentSQL("invalid connection string"));
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Constructor_ShouldThrow_ConnectionString_NotWellFormed()
|
||||||
|
=> Assert.ThrowsException<ArgumentException>(() => new FluentSQL("Invalid Key=123"));
|
||||||
|
}
|
||||||
|
}
|
||||||
+71
@@ -0,0 +1,71 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{A14581DD-E1AC-4AB0-9329-B723D24A2F3A}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>LaaProductionWeb.UnitTests.Data</RootNamespace>
|
||||||
|
<AssemblyName>LaaProductionWeb.UnitTests.Data</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||||
|
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
|
||||||
|
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||||
|
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
|
||||||
|
<IsCodedUITest>False</IsCodedUITest>
|
||||||
|
<TestProjectType>UnitTest</TestProjectType>
|
||||||
|
<NuGetPackageImportStamp>
|
||||||
|
</NuGetPackageImportStamp>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="FluentSQLTestClass.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\LaaProductionWeb.Data\LaaProductionWeb.Data.csproj">
|
||||||
|
<Project>{4b5bf112-d3be-4e84-9a8e-74a0e314bba7}</Project>
|
||||||
|
<Name>LaaProductionWeb.Data</Name>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||||
|
<PropertyGroup>
|
||||||
|
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
|
||||||
|
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
|
||||||
|
</Target>
|
||||||
|
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
[assembly: AssemblyTitle("LaaProductionWeb.UnitTests.Data")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("LaaProductionWeb.UnitTests.Data")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2023")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
[assembly: Guid("a14581dd-e1ac-4ab0-9329-b723d24a2f3a")]
|
||||||
|
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
@@ -14,6 +14,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documents", "Documents", "{
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaaProductionWeb", "LaaProductionWeb\LaaProductionWeb.csproj", "{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaaProductionWeb", "LaaProductionWeb\LaaProductionWeb.csproj", "{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnitTests", "UnitTests", "{C01BB59F-25A6-4AC1-ADB5-C453EA57C35B}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaaProductionWeb.UnitTests.Data", "LaaProductionWeb.UnitTests.Data\LaaProductionWeb.UnitTests.Data.csproj", "{A14581DD-E1AC-4AB0-9329-B723D24A2F3A}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentSQL", "FluentSQL\FluentSQL.csproj", "{68986A99-0DBA-407C-90BE-D5ED63C41BD4}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -32,10 +38,21 @@ Global
|
|||||||
{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}.Release|Any CPU.Build.0 = Release|Any CPU
|
{D1AD077B-00E3-4663-AB9F-23FF65D98D2F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{A14581DD-E1AC-4AB0-9329-B723D24A2F3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{A14581DD-E1AC-4AB0-9329-B723D24A2F3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{A14581DD-E1AC-4AB0-9329-B723D24A2F3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{A14581DD-E1AC-4AB0-9329-B723D24A2F3A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{68986A99-0DBA-407C-90BE-D5ED63C41BD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{68986A99-0DBA-407C-90BE-D5ED63C41BD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{68986A99-0DBA-407C-90BE-D5ED63C41BD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{68986A99-0DBA-407C-90BE-D5ED63C41BD4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
GlobalSection(NestedProjects) = preSolution
|
||||||
|
{A14581DD-E1AC-4AB0-9329-B723D24A2F3A} = {C01BB59F-25A6-4AC1-ADB5-C453EA57C35B}
|
||||||
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {9D3C9EBA-FB4B-4BF4-8158-4A9FE47D3E88}
|
SolutionGuid = {9D3C9EBA-FB4B-4BF4-8158-4A9FE47D3E88}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
|||||||
Reference in New Issue
Block a user