Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Elanis committed Jun 24, 2021
1 parent de815ee commit 9ee5a13
Show file tree
Hide file tree
Showing 7 changed files with 729 additions and 14 deletions.
456 changes: 456 additions & 0 deletions Doc.md

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Dysnomia.Common.SQL/DataTableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

namespace Dysnomia.Common.SQL {
public static class DataTableHelper {
/// <summary>
/// Generates a datatable from an object
/// </summary>
/// <typeparam name="T">Any type</typeparam>
/// <param name="myObject"></param>
/// <returns></returns>
public static DataTable CreateDataTableFromObject<T>(T myObject) {
Type type = typeof(T);
var properties = type.GetProperties();
Expand All @@ -32,6 +38,5 @@ public static class DataTableHelper {

return dataTable;
}

}
}
46 changes: 35 additions & 11 deletions Dysnomia.Common.SQL/DbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ MIT License
namespace Dysnomia.Common.SQL {
public static class DbHelper {
private static void BindParameters(this IDbCommand command, Dictionary<string, object> parametersData) {
if(parametersData != null) {
foreach(KeyValuePair<string, object> kvp in parametersData) {
if(kvp.Value is IDataParameter) {
if (parametersData != null) {
foreach (KeyValuePair<string, object> kvp in parametersData) {
if (kvp.Value is IDataParameter) {
command.Parameters.Add(kvp.Value);
} else {
var parameter = command.CreateParameter();
Expand All @@ -44,21 +44,29 @@ public static class DbHelper {
}

private static void OpenConnection(this IDbConnection connection) {
if(connection.State == ConnectionState.Broken) {
if (connection.State == ConnectionState.Broken) {
connection.Close();
}

if(connection.State == ConnectionState.Closed) {
if (connection.State == ConnectionState.Closed) {
connection.Open();
}
}

/// <summary>
/// Execute SQL stored procedure with reader as a result
/// </summary>
/// <param name="connection">Database connection</param>
/// <param name="procName">Procedure to execute</param>
/// <param name="parameters">Statement parameters</param>
/// <param name="transaction">(Optional) Transaction this statement should be in</param>
/// <returns></returns>
public async static Task<IDataReader> ExecStoredProcedure(this IDbConnection connection, string procName, Dictionary<string, object> parameters = null, IDbTransaction transaction = null) {
using(IDbCommand command = connection.CreateCommand()) {
using (IDbCommand command = connection.CreateCommand()) {
command.CommandType = CommandType.StoredProcedure;
command.CommandText = procName;

if(transaction != null) {
if (transaction != null) {
command.Transaction = transaction;
}

Expand All @@ -70,12 +78,20 @@ public static class DbHelper {
}
}

/// <summary>
/// Execute SQL query with reader as a result
/// </summary>
/// <param name="connection">Database connection</param>
/// <param name="sqlStatement">Statement to execute</param>
/// <param name="parameters">Statement parameters</param>
/// <param name="transaction">(Optional) Transaction this statement should be in</param>
/// <returns></returns>
public async static Task<IDataReader> ExecuteQuery(this IDbConnection connection, string sqlStatement, Dictionary<string, object> parameters = null, IDbTransaction transaction = null) {
using(IDbCommand command = connection.CreateCommand()) {
using (IDbCommand command = connection.CreateCommand()) {
command.CommandType = CommandType.Text;
command.CommandText = sqlStatement;

if(transaction != null) {
if (transaction != null) {
command.Transaction = transaction;
}

Expand All @@ -87,12 +103,20 @@ public static class DbHelper {
}
}

/// <summary>
/// Execute SQL query without any result returned
/// </summary>
/// <param name="connection">Database connection</param>
/// <param name="sqlStatement">Statement to execute</param>
/// <param name="parameters">Statement parameters</param>
/// <param name="transaction">(Optional) Transaction this statement should be in</param>
/// <returns></returns>
public async static Task<int> ExecuteNonQuery(this IDbConnection connection, string sqlStatement, Dictionary<string, object> parameters = null, IDbTransaction transaction = null) {
using(IDbCommand command = connection.CreateCommand()) {
using (IDbCommand command = connection.CreateCommand()) {
command.CommandType = CommandType.Text;
command.CommandText = sqlStatement;

if(transaction != null) {
if (transaction != null) {
command.Transaction = transaction;
}

Expand Down
12 changes: 11 additions & 1 deletion Dysnomia.Common.SQL/DbNullHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@ MIT License

namespace Dysnomia.Common.SQL {
public static class DbNullHelper {
/// <summary>
/// Return string or DbNull if the string is null or empty
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static object DbNullOrString(this string val) {
if(string.IsNullOrEmpty(val)) {
if (string.IsNullOrEmpty(val)) {
return DBNull.Value;
}

return val;
}

/// <summary>
/// Return value or DbNull if the value is null
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static object DbNullOrValue(this object val) {
return val ?? DBNull.Value;
}
Expand Down
42 changes: 42 additions & 0 deletions Dysnomia.Common.SQL/DbReaderMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* Short/Int16 from reader
*/
public static short GetShort(this IDataReader reader, string key, bool catchNull = true, short defaultValue = 0) {
return GetInt16(reader, key, catchNull, defaultValue);
}
Expand All @@ -68,6 +71,9 @@ public static class DbReaderMapper {

return null;
}
/**
* Nullable Short/Int16 from reader
*/
public static short? GetNullableShort(this IDataReader reader, string key) {
return GetNullableInt16(reader, key);
}
Expand All @@ -84,6 +90,9 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* UShort/UInt16 from reader
*/
public static ushort GetUShort(this IDataReader reader, string key, bool catchNull = true, ushort defaultValue = 0) {
return GetUInt16(reader, key, catchNull, defaultValue);
}
Expand All @@ -101,6 +110,9 @@ public static class DbReaderMapper {

return null;
}
/**
* Nullable UShort/UInt16 from reader
*/
public static ushort? GetNullableUShort(this IDataReader reader, string key) {
return GetNullableUInt16(reader, key);
}
Expand All @@ -117,6 +129,9 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* Int/Int32 from reader
*/
public static int GetInt(this IDataReader reader, string key, bool catchNull = true, int defaultValue = 0) {
return GetInt32(reader, key, catchNull, defaultValue);
}
Expand All @@ -133,6 +148,9 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* UInt/UInt32 from reader
*/
public static uint GetUInt(this IDataReader reader, string key, bool catchNull = true, uint defaultValue = 0) {
return GetUInt32(reader, key, catchNull, defaultValue);
}
Expand All @@ -149,6 +167,9 @@ public static class DbReaderMapper {

return null;
}
/**
* Nullable Int/Int32 from reader
*/
public static int? GetNullableInt(this IDataReader reader, string key) {
return GetNullableInt32(reader, key);
}
Expand All @@ -165,6 +186,9 @@ public static class DbReaderMapper {

return null;
}
/**
* Nullable UInt/UInt32 from reader
*/
public static uint? GetNullableUInt(this IDataReader reader, string key) {
return GetNullableUInt32(reader, key);
}
Expand All @@ -181,6 +205,9 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* Long/Int64 from reader
*/
public static long GetLong(this IDataReader reader, string key, bool catchNull = true, long defaultValue = 0) {
return GetInt64(reader, key, catchNull, defaultValue);
}
Expand All @@ -197,6 +224,9 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* ULong/UInt64 from reader
*/
public static ulong GetULong(this IDataReader reader, string key, bool catchNull = true, ulong defaultValue = 0) {
return GetUInt64(reader, key, catchNull, defaultValue);
}
Expand All @@ -213,6 +243,9 @@ public static class DbReaderMapper {

return null;
}
/**
* Nullable Long/Int64 from reader
*/
public static long? GetNullableLong(this IDataReader reader, string key) {
return GetNullableInt64(reader, key);
}
Expand All @@ -229,6 +262,9 @@ public static class DbReaderMapper {

return null;
}
/**
* Nullable Long/Int64 from reader
*/
public static ulong? GetNullableULong(this IDataReader reader, string key) {
return GetNullableUInt64(reader, key);
}
Expand Down Expand Up @@ -349,9 +385,15 @@ public static class DbReaderMapper {

return defaultValue;
}
/**
* Guid
*/
public static Guid GetGuid(this IDataReader reader, string key, bool catchNull, string guidData) {
return GetGuid(reader, key, catchNull, Guid.Parse(guidData));
}
/**
* Guid
*/
public static Guid GetGuid(this IDataReader reader, string key, bool catchNull = true) {
return GetGuid(reader, key, catchNull, Guid.Empty);
}
Expand Down
9 changes: 8 additions & 1 deletion Dysnomia.Common.SQL/Dysnomia.Common.SQL.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0</TargetFrameworks>
<Authors>Axel "Elanis" Soupé</Authors>
Expand All @@ -13,6 +12,8 @@
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<DocumentationFile>Dysnomia.Common.SteamWebAPI.xml</DocumentationFile>
<DocumentationMarkdown>..\Doc.md</DocumentationMarkdown>
</PropertyGroup>

<ItemGroup>
Expand All @@ -22,4 +23,10 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Vsxmd" Version="1.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

0 comments on commit 9ee5a13

Please sign in to comment.