forked from robson-paproski/AspNetCore.Identity.PostgreSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned solution and upgraded to last .netcore with Visual Studio 2017
- Loading branch information
1 parent
aa06ca4
commit 8d81030
Showing
265 changed files
with
772 additions
and
28,973 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26430.4 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Identity.PostgreSQL", "AspNetCore.Identity.PostgreSQL\AspNetCore.Identity.PostgreSQL.csproj", "{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebExample", "WebExample\WebExample.csproj", "{1F910597-E02B-4F95-9F1B-67E27368891C}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F204EC76-7505-47B7-810A-3E87AE87CDFA}" | ||
ProjectSection(SolutionItems) = preProject | ||
PostgresIdentity.sql = PostgresIdentity.sql | ||
EndProjectSection | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{9CE6763F-FE14-4FFB-AEF5-F69AA712F55E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{1F910597-E02B-4F95-9F1B-67E27368891C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{1F910597-E02B-4F95-9F1B-67E27368891C}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{1F910597-E02B-4F95-9F1B-67E27368891C}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{1F910597-E02B-4F95-9F1B-67E27368891C}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
AspNetCore.Identity.PostgreSQL/AspNetCore.Identity.PostgreSQL.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.2" /> | ||
<PackageReference Include="Npgsql" Version="3.2.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Newtonsoft.Json"> | ||
<HintPath>..\..\..\..\.nuget\packages\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
|
||
</Project> |
249 changes: 249 additions & 0 deletions
249
AspNetCore.Identity.PostgreSQL/Context/PostgreSQLDatabase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Threading; | ||
using Microsoft.Extensions.Configuration; | ||
using Newtonsoft.Json.Linq; | ||
using Npgsql; | ||
using NpgsqlTypes; | ||
|
||
namespace AspNetCore.Identity.PostgreSQL.Context | ||
{ | ||
public class PostgreSQLDatabase | ||
{ | ||
private static NpgsqlConnection _connection; | ||
private readonly IConfigurationRoot _configurationRoot; | ||
private static object _consulta = new object(); | ||
|
||
public PostgreSQLDatabase(IConfigurationRoot configurationRoot) | ||
{ | ||
_configurationRoot = configurationRoot; | ||
_connection = new Npgsql.NpgsqlConnection(_configurationRoot.GetConnectionString("PostgreSQLBaseConnection")); | ||
} | ||
|
||
|
||
public int ExecuteSQL(string commandText, Dictionary<string, object> parameters) | ||
{ | ||
lock (_consulta) | ||
{ | ||
var result = 0; | ||
|
||
if (string.IsNullOrEmpty(commandText)) | ||
{ | ||
throw new ArgumentException("Command text cannot be null or empty."); | ||
} | ||
|
||
try | ||
{ | ||
OpenConnection(); | ||
var command = CreateCommand(commandText, parameters); | ||
result = command.ExecuteNonQuery(); | ||
} | ||
finally | ||
{ | ||
|
||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
|
||
public object ExecuteQueryGetSingleObject(string commandText, Dictionary<string, object> parameters) | ||
{ | ||
lock (_consulta) | ||
{ | ||
object result = null; | ||
|
||
if (string.IsNullOrEmpty(commandText)) | ||
{ | ||
throw new ArgumentException("Command text cannot be null or empty."); | ||
} | ||
|
||
try | ||
{ | ||
OpenConnection(); | ||
var command = CreateCommand(commandText, parameters); | ||
result = command.ExecuteScalar(); | ||
} | ||
finally | ||
{ | ||
CloseConnection(); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
public Dictionary<string, string> ExecuteQueryGetSingleRow(string commandText, | ||
Dictionary<string, object> parameters) | ||
{ | ||
lock (_consulta) | ||
{ | ||
Dictionary<string, string> row = null; | ||
if (string.IsNullOrEmpty(commandText)) | ||
{ | ||
throw new ArgumentException("Command text cannot be null or empty."); | ||
} | ||
|
||
try | ||
{ | ||
OpenConnection(); | ||
var command = CreateCommand(commandText, parameters); | ||
using (NpgsqlDataReader reader = command.ExecuteReader()) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
row = new Dictionary<string, string>(); | ||
for (var i = 0; i < reader.FieldCount; i++) | ||
{ | ||
var columnName = reader.GetName(i); | ||
var columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i).ToString(); | ||
row.Add(columnName, columnValue); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
CloseConnection(); | ||
} | ||
|
||
return row; | ||
} | ||
} | ||
|
||
public List<Dictionary<string, string>> ExecuteQuery(string commandText, Dictionary<string, object> parameters) | ||
{ | ||
lock (_consulta) | ||
{ | ||
List<Dictionary<string, string>> rows = null; | ||
if (string.IsNullOrEmpty(commandText)) | ||
{ | ||
throw new ArgumentException("Command text cannot be null or empty."); | ||
} | ||
|
||
try | ||
{ | ||
OpenConnection(); | ||
var command = CreateCommand(commandText, parameters); | ||
using (NpgsqlDataReader reader = command.ExecuteReader()) | ||
{ | ||
rows = new List<Dictionary<string, string>>(); | ||
while (reader.Read()) | ||
{ | ||
var row = new Dictionary<string, string>(); | ||
for (var i = 0; i < reader.FieldCount; i++) | ||
{ | ||
var columnName = reader.GetName(i); | ||
var columnValue = reader.IsDBNull(i) ? null : reader.GetValue(i).ToString(); | ||
row.Add(columnName, columnValue); | ||
} | ||
rows.Add(row); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
CloseConnection(); | ||
} | ||
|
||
return rows; | ||
} | ||
} | ||
|
||
private NpgsqlCommand CreateCommand(string commandText, Dictionary<string, object> parameters) | ||
{ | ||
|
||
NpgsqlCommand command = _connection.CreateCommand(); | ||
command.CommandText = commandText; | ||
AddParameters(command, parameters); | ||
|
||
return command; | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Adds the parameters to a PostgreSQL command. | ||
/// </summary> | ||
/// <param name="commandText">The PostgreSQL query to execute.</param> | ||
/// <param name="parameters">Parameters to pass to the PostgreSQL query.</param> | ||
private static void AddParameters(NpgsqlCommand command, Dictionary<string, object> parameters) | ||
{ | ||
if (parameters == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var param in parameters) | ||
{ | ||
var parameter = command.CreateParameter(); | ||
parameter.ParameterName = param.Key; | ||
parameter.Value = param.Value ?? DBNull.Value; | ||
|
||
var s = param.Value as string; | ||
if ((s != null) && s.StartsWith("JSON")) | ||
{ | ||
parameter.Value = JObject.Parse(s.Replace("JSON", "")); | ||
parameter.NpgsqlDbType = NpgsqlDbType.Json; | ||
} | ||
|
||
command.Parameters.Add(parameter); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Helper method to return query a string value. | ||
/// </summary> | ||
/// <param name="commandText">The PostgreSQL query to execute.</param> | ||
/// <param name="parameters">Parameters to pass to the PostgreSQL query.</param> | ||
/// <returns>The string value resulting from the query.</returns> | ||
private string GetStrValue(string commandText, Dictionary<string, object> parameters) | ||
{ | ||
var value = ExecuteQueryGetSingleObject(commandText, parameters) as string; | ||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Opens a connection if not open. | ||
/// </summary> | ||
public void OpenConnection() | ||
{ | ||
if (_connection == null) _connection = new Npgsql.NpgsqlConnection(_configurationRoot.GetConnectionString("PostgreSQLBaseConnection")); | ||
var retries = 20; | ||
if (_connection.State == ConnectionState.Open) | ||
{ | ||
} | ||
else | ||
{ | ||
while (retries >= 0 && _connection.State != ConnectionState.Open) | ||
{ | ||
lock (_consulta) | ||
{ | ||
_connection.Close(); | ||
_connection.Open(); | ||
} | ||
retries--; | ||
Thread.Sleep(50); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Closes the connection if it is open. | ||
/// </summary> | ||
public void CloseConnection() | ||
{ | ||
/* if (_connection.State == ConnectionState.Open) | ||
{ | ||
// _connection.Close(); | ||
}*/ | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.