diff --git a/Dysnomia.Common.SQL/DbHelper.cs b/Dysnomia.Common.SQL/DbHelper.cs new file mode 100644 index 0000000..dac84f6 --- /dev/null +++ b/Dysnomia.Common.SQL/DbHelper.cs @@ -0,0 +1,91 @@ +/* + MIT License + + Copyright (c) 2019 Axel "Elanis" Soupé + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +using System.Collections.Generic; +using System.Data; +using System.Threading.Tasks; + +namespace Dysnomia.Common.SQL { + public static class DbHelper { + private static void BindParameters(IDbCommand command, Dictionary parametersData) { + if(parametersData != null) { + foreach(KeyValuePair kvp in parametersData) { + var parameter = command.CreateParameter(); + parameter.ParameterName = kvp.Key; + parameter.Value = kvp.Value; + + command.Parameters.Add(parameter); + } + } + } + + private static void OpenConnection(IDbConnection connection) { + if(connection.State == ConnectionState.Broken) { + connection.Close(); + } + + if(connection.State == ConnectionState.Closed) { + connection.Open(); + } + } + + public async static Task ExecStoredProcedure(IDbConnection connection, string procName, Dictionary parameters = null) { + using(IDbCommand command = connection.CreateCommand()) { + command.CommandType = CommandType.StoredProcedure; + command.CommandText = procName; + + OpenConnection(command.Connection); + + BindParameters(command, parameters); + + return await Task.Run(() => command.ExecuteReader()); + } + } + + public async static Task ExecuteQuery(IDbConnection connection, string sqlStatement, Dictionary parameters = null) { + using(IDbCommand command = connection.CreateCommand()) { + command.CommandType = CommandType.Text; + command.CommandText = sqlStatement; + + OpenConnection(command.Connection); + + BindParameters(command, parameters); + + return await Task.Run(() => command.ExecuteReader()); + } + } + + public async static Task ExecuteNonQuery(IDbConnection connection, string sqlStatement, Dictionary parameters = null) { + using(IDbCommand command = connection.CreateCommand()) { + command.CommandType = CommandType.Text; + command.CommandText = sqlStatement; + + OpenConnection(command.Connection); + + BindParameters(command, parameters); + + return await Task.Run(() => command.ExecuteNonQuery()); + } + } + } +} diff --git a/Dysnomia.Common.SQL/DbReaderMapper.cs b/Dysnomia.Common.SQL/DbReaderMapper.cs index 4fa5b70..538ef92 100644 --- a/Dysnomia.Common.SQL/DbReaderMapper.cs +++ b/Dysnomia.Common.SQL/DbReaderMapper.cs @@ -1,4 +1,4 @@ -/* +/* MIT License Copyright (c) 2019 Axel "Elanis" Soupé @@ -20,7 +20,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ +*/ using System; using System.Data; diff --git a/Dysnomia.Common.SQL/Dysnomia.Common.SQL.csproj b/Dysnomia.Common.SQL/Dysnomia.Common.SQL.csproj index 49d5235..5b7acfc 100644 --- a/Dysnomia.Common.SQL/Dysnomia.Common.SQL.csproj +++ b/Dysnomia.Common.SQL/Dysnomia.Common.SQL.csproj @@ -4,20 +4,16 @@ netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0 Axel "Elanis" Soupé Dysnomia - A .NET Core Micro-ORM for PostgreSQL & SQL Server + A driver agnostic Micro-ORM for .NET Core LICENSE https://github.com/Dysnomia-Studio/Dysnomia.Common.SQL https://github.com/Dysnomia-Studio/Dysnomia.Common.SQL Github - Add: - - DbReaderMapper.GetGuid() (3 overrides) - -Fix: - - DbReaderMapper.GetInt64() were returning Int32 - - There were a typo in name of DbReaderMapper.GetNullableShort() - -(https://github.com/Dysnomia-Studio/Dysnomia.Common.SQL/commit/d99e1ee22a4b59a1ee235bc78441d96198722c35) - 1.0.2 + - Open connection only if needed +- Renamed ExecSelect to ExecuteQuery and ExecStatement to ExecuteNonQuery + 1.0.3-rc5 + 1.0.3.5 + 1.0.3.5 diff --git a/README.md b/README.md index b6c7994..2b373c5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ +[ +![](https://img.shields.io/nuget/dt/Dysnomia.Common.SQL?label=NuGet%20Downloads) +![](https://img.shields.io/nuget/v/Dysnomia.Common.SQL?label=NuGet%20Version) +](https://nuget.org/packages/Dysnomia.Common.SQL) + + # Dysnomia.Common.SQL -A .NET Core Micro-ORM for PostgreSQL & SQL Server +A driver agnostic Micro-ORM for .NET Core ## Why creating this Micro-ORM and publishing it to Github ? Because I like creating my own libraries, and anyway I was creating this so why not making it public ? There's no sensitive data, and it can help many peoples :)