Skip to content

Commit

Permalink
Added execute with temp table extension for IDatabase. (#84)
Browse files Browse the repository at this point in the history
* Pridanie execute with temp table extension nad IDatabase.

* Suggestions from PR,
Added readme description,
guid to string with specifier

* PR suggestions: added simple example to readme.md

* Update README.md

Co-authored-by: Miňo Martiniak <[email protected]>

* Update README.md

Co-authored-by: Miňo Martiniak <[email protected]>

* PR suggestion: readme comment removed

Co-authored-by: Miňo Martiniak <[email protected]>
  • Loading branch information
misho048 and Burgyn authored Jun 23, 2021
1 parent 6dd0d75 commit 2b6b73f
Show file tree
Hide file tree
Showing 10 changed files with 692 additions and 3 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,41 @@ await database.UpsertAsync(people);

```


#### Execute with temp table

Kros.KORM offers special execute commands for SQL databases, that inserts provided simple data into temp table and
then executes some specified action using those temporary data.
You can find these extension methods in `IDatabaseExtensions` class.

```CSharp
database.ExecuteWithTempTable<TValue>(IEnumerable<TValue> values, action);
await database.ExecuteWithTempTableAsync<TValue>(IEnumerable<TValue> values, function);

database.ExecuteWithTempTable<TKey, TValue>(IDictionary<TKey, TValue> values, action);
await database.ExecuteWithTempTable<TKey, TValue>(IDictionary<TKey, TValue> values, function);

T database.ExecuteWithTempTable<T, TValue> (IEnumerable<TValue> values, action);
await T database.ExecuteWithTempTable<T, TValue> (IEnumerable<TValue> values, function);

T database.ExecuteWithTempTable<T, TKey, TValue> (IDictionary<TKey, TValue> values, action);
await T database.ExecuteWithTempTable<T,TKey, TValue> (IDictionary<TKey, TValue> values, function);

public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

var ids = new List<int>(){ 0, 1, 2, 3 };

_database.ExecuteWithTempTable(ids, (database, tableName)
=> database.Query<Person>()
.From($"PERSON AS P INNER JOIN {tableName} AS T ON (P.Id = T.Value)")
.ToList());
```

### SQL commands executing

Kros.KORM supports SQL commands execution. There are three types of commands:
Expand Down
117 changes: 117 additions & 0 deletions src/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Kros.KORM.Properties;
using System;
using System.Collections.Generic;
using System.Data;

namespace Kros.KORM.Extensions
{
/// <summary>
/// .NET clr type extensions.
/// </summary>
internal static class TypeExtensions
{
private static readonly Dictionary<Type, DbType> _dbTypeMap = new()
{
{ typeof(bool), DbType.Boolean },
{ typeof(bool?), DbType.Boolean },
{ typeof(byte[]), DbType.Binary },
{ typeof(byte), DbType.Byte },
{ typeof(byte?), DbType.Byte },
{ typeof(sbyte), DbType.SByte },
{ typeof(sbyte?), DbType.SByte },
{ typeof(short), DbType.Int16 },
{ typeof(short?), DbType.Int16 },
{ typeof(ushort), DbType.UInt16 },
{ typeof(ushort?), DbType.UInt16 },
{ typeof(int), DbType.Int32 },
{ typeof(int?), DbType.Int32 },
{ typeof(uint), DbType.UInt32 },
{ typeof(uint?), DbType.UInt32 },
{ typeof(long), DbType.Int64 },
{ typeof(long?), DbType.Int64 },
{ typeof(ulong), DbType.UInt64 },
{ typeof(ulong?), DbType.UInt64 },
{ typeof(float), DbType.Single },
{ typeof(float?), DbType.Single },
{ typeof(decimal), DbType.Decimal },
{ typeof(decimal?), DbType.Decimal },
{ typeof(double), DbType.Double },
{ typeof(double?), DbType.Double },
{ typeof(DateTime), DbType.DateTime },
{ typeof(DateTime?), DbType.DateTime },
{ typeof(Guid), DbType.Guid },
{ typeof(object), DbType.Binary },
{ typeof(string), DbType.String }
};

private static readonly Dictionary<Type, string> _sqlTypeMap = new()
{
{ typeof(bool), "bit" },
{ typeof(bool?), "bit" },
{ typeof(byte[]), "varBinary" },
{ typeof(byte), "tinyInt" },
{ typeof(byte?), "tinyInt" },
{ typeof(sbyte), "tinyInt" },
{ typeof(sbyte?), "tinyInt" },
{ typeof(short), "smallInt" },
{ typeof(short?), "smallInt" },
{ typeof(ushort), "smallInt" },
{ typeof(ushort?), "smallInt" },
{ typeof(int), "int" },
{ typeof(int?), "int" },
{ typeof(uint), "int" },
{ typeof(uint?), "int" },
{ typeof(long), "bigInt" },
{ typeof(long?), "bigInt" },
{ typeof(ulong), "bigInt" },
{ typeof(ulong?), "bigInt" },
{ typeof(float), "real" },
{ typeof(float?), "real" },
{ typeof(decimal), "decimal" },
{ typeof(decimal?), "decimal" },
{ typeof(double), "float" },
{ typeof(double?), "float" },
{ typeof(DateTime), "dateTime" },
{ typeof(DateTime?), "dateTime" },
{ typeof(Guid), "uniqueIdentifier" },
{ typeof(object), "varBinary" },
{ typeof(string), "nVarChar(255)" }
};

/// <summary>
/// Converts .NET clr type to DbType.
/// </summary>
/// <param name="type">Clr type.</param>
/// <returns>DbType.</returns>
/// <exception cref="NotSupportedException">When clr type is not supported.</exception>
public static DbType ToDbType(this Type type)
{
if (type.IsEnum)
{
type = Enum.GetUnderlyingType(type);
}

return _dbTypeMap.TryGetValue(type, out DbType dbType)
? dbType
: throw new NotSupportedException(string.Format(Resources.ConversionFromTypeIsNotSupported, type.Name));
}

/// <summary>
/// Converts .NET clr type to SQL data type.
/// </summary>
/// <param name="type">Clr type.</param>
/// <returns>SQL data type.</returns>
/// <exception cref="NotSupportedException">When clr type is not supported.</exception>
public static string ToSqlDataType(this Type type)
{
if (type.IsEnum)
{
type = Enum.GetUnderlyingType(type);
}

return _sqlTypeMap.TryGetValue(type, out string sqlDataType)
? sqlDataType
: throw new NotSupportedException(string.Format(Resources.ConversionFromTypeIsNotSupported, type.Name));
}
}
}
Loading

0 comments on commit 2b6b73f

Please sign in to comment.