Skip to content

Commit 658ed89

Browse files
committed
Add support for PostgreSQL DB
1 parent 4dd4ca7 commit 658ed89

File tree

15 files changed

+230
-7
lines changed

15 files changed

+230
-7
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
uses: abhinavminhas/replace-tokens@main
3636
with:
3737
files: '${{ github.workspace }}/QueryDB.Core.Tests/App.config'
38-
replacements: '__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }}'
38+
replacements: '__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }}'
3939

4040
- name: Build
4141
run: dotnet build QueryDB.Core.Tests/QueryDB.Core.Tests.csproj --configuration Release
@@ -90,7 +90,7 @@ jobs:
9090
uses: abhinavminhas/replace-tokens@main
9191
with:
9292
files: '${{ github.workspace }}/QueryDB.Core.Tests/App.config'
93-
replacements: '__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }}'
93+
replacements: '__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }}'
9494

9595
- name: Build
9696
run: dotnet build QueryDB.Core.Tests/QueryDB.Core.Tests.csproj --configuration Release

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: abhinavminhas/replace-tokens@main
2727
with:
2828
files: '${{ github.workspace }}/QueryDB.Core.Tests/App.config'
29-
replacements: '__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }}'
29+
replacements: '__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }},__DB_PASSWORD__=${{ secrets.DB_PASSWORD }}'
3030

3131
- name: Build
3232
run: dotnet build QueryDB.Core.Tests/QueryDB.Core.Tests.csproj --configuration Release

.github/workflows/docker-compose-start-dbs/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ runs:
1313
uses: abhinavminhas/replace-tokens@main
1414
with:
1515
files: '${{ github.workspace }}/docker-compose.yml'
16-
replacements: '__DB_PASSWORD__=${{ inputs.dbPassword }},__DB_PASSWORD__=${{ inputs.dbPassword }},__DB_PASSWORD__=${{ inputs.dbPassword }}'
16+
replacements: '__DB_PASSWORD__=${{ inputs.dbPassword }},__DB_PASSWORD__=${{ inputs.dbPassword }},__DB_PASSWORD__=${{ inputs.dbPassword }},__DB_PASSWORD__=${{ inputs.dbPassword }}'
1717

1818
- name: Docker Compose (Pull)
1919
shell: bash

QueryDB.Core.Tests/App.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<add key="MSSQLConnection" value="Data Source=(local);Initial Catalog=master;User Id=SA;Password=__DB_PASSWORD__;Encrypt=False;" />
66
<add key="MySQLConnection" value="server=localhost;database=mysql;uid=root;pwd=__DB_PASSWORD__;" />
77
<add key="OracleConnection" value="Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SID = XE))); User Id = sys;Password=__DB_PASSWORD__;DBA Privilege=SYSDBA" />
8+
<add key="PostgreSQLConnection" value="Host=localhost;Port=5432;Database=postgres;Username=sys;Password=__DB_PASSWORD__;" />
89
</appSettings>
910
</configuration>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
services:
2+
postgres-db:
3+
image: postgres:16.4
4+
container_name: postgres-db
5+
environment:
6+
POSTGRES_USER: "sys"
7+
POSTGRES_PASSWORD: ""
8+
POSTGRES_DB: postgres
9+
ports:
10+
- "5432:5432"
11+
volumes:
12+
- ./QueryDB.Core.Tests/SeedData:/home
13+
healthcheck:
14+
test: ["CMD-SHELL", "pg_isready -U sys"]
15+
interval: 5s
16+
timeout: 5s
17+
retries: 5
18+
start_period: 0s
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace QueryDB.Core.Tests
4+
{
5+
[TestClass]
6+
public class PostgreSQLTests : TestBase
7+
{
8+
9+
#region PostgreSQL DB Tests
10+
11+
#region Smoke Tests
12+
13+
[TestMethod]
14+
[TestCategory(DB_TESTS), TestCategory(POSTGRESQL_TESTS), TestCategory(SMOKE_TESTS)]
15+
public void Test_PostgreSQL_FetchData()
16+
{
17+
var selectSql = Queries.PostgreSQLQueries.Smoke.SelectSql;
18+
var data = new DBContext(DB.PostgreSQL, PostgreSQLConnectionString).FetchData(selectSql);
19+
Assert.IsTrue(data.Count > 0);
20+
Assert.AreEqual("postgres", data[0].ReferenceData["current_database"]);
21+
22+
var dbContext = new DBContext(DB.PostgreSQL, PostgreSQLConnectionString);
23+
data = dbContext.FetchData(selectSql);
24+
Assert.IsTrue(data.Count > 0);
25+
Assert.AreEqual("postgres", data[0].ReferenceData["current_database"]);
26+
}
27+
28+
#endregion
29+
30+
#endregion
31+
32+
}
33+
}

QueryDB.Core.Tests/Queries.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,23 @@ internal static class SalesDB
5555
Orders O ON O.Cust_Code = C.Cust_Code AND O.Agent_Code = A.Agent_Code";
5656
}
5757
}
58+
59+
internal static class PostgreSQLQueries
60+
{
61+
internal static class Smoke
62+
{
63+
public static string SelectSql = @"SELECT 'postgres' AS current_database";
64+
}
65+
internal static class SalesDB
66+
{
67+
public static string SelectSql = @"SELECT * FROM Agents";
68+
public static string SelectSql_Join = @"SELECT A.Agent_Code, A.Agent_Name, C.Cust_Code, C.Cust_Name, O.Ord_Num, O.Ord_Amount, O.Advance_Amount, O.Ord_Date, O.Ord_Description FROM Agents A INNER JOIN
69+
Customer C ON C.Agent_Code = A.Agent_Code INNER JOIN
70+
Orders O ON O.Cust_Code = C.Cust_Code AND O.Agent_Code = A.Agent_Code";
71+
public static string SelectSql_Alias = @"SELECT A.Agent_Name AS Agent, A.WORKING_AREA AS Agent_Location, C.Cust_Name AS Customer, C.WORKING_AREA AS Customer_Location, O.Agent_Code, O.Cust_Code FROM Agents A INNER JOIN
72+
Customer C ON C.Agent_Code = A.Agent_Code INNER JOIN
73+
Orders O ON O.Cust_Code = C.Cust_Code AND O.Agent_Code = A.Agent_Code";
74+
}
75+
}
5876
}
5977
}

QueryDB.Core.Tests/TestBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ public class TestBase
99
protected readonly string MSSQLConnectionString = ConfigurationManager.AppSettings["MSSQLConnection"];
1010
protected readonly string MySQLConnectionString = ConfigurationManager.AppSettings["MySQLConnection"];
1111
protected readonly string OracleConnectionString = ConfigurationManager.AppSettings["OracleConnection"];
12+
protected readonly string PostgreSQLConnectionString = ConfigurationManager.AppSettings["PostgreSQLConnection"];
1213
protected const string DB_TESTS = "DB-TESTS";
1314
protected const string SMOKE_TESTS = "SMOKE-TESTS";
14-
protected const string ORACLE_TESTS = "ORACLE-TESTS";
1515
protected const string MSSQL_TESTS = "MSSQL-TESTS";
1616
protected const string MYSQL_TESTS = "MYSQL-TESTS";
17+
protected const string ORACLE_TESTS = "ORACLE-TESTS";
18+
protected const string POSTGRESQL_TESTS = "POSTGRESQL-TESTS";
1719

1820
[AssemblyInitialize]
1921
internal void CheckDockerImages()

QueryDB/ConnectionBuilder.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,17 @@ internal static Oracle.Connection GetOracleConnection
4040
return connection;
4141
}
4242
}
43+
44+
/// <summary>
45+
/// Gets 'PostgreSQL' database connection.
46+
/// </summary>
47+
internal static PostgreSQL.Connection GetPostgreSqlConnection
48+
{
49+
get
50+
{
51+
var connection = new PostgreSQL.Connection(DBContext.PostgreSqlConnectionString);
52+
return connection;
53+
}
54+
}
4355
}
4456
}

QueryDB/DBContext.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public sealed class DBContext : IDBContext
2828
/// </summary>
2929
internal static string OracleConnectionString { get; private set; }
3030

31+
/// <summary>
32+
/// Holds 'PostgreSQL' connection string value for the DBContext created.
33+
/// </summary>
34+
internal static string PostgreSqlConnectionString { get; private set; }
35+
3136
/// <summary>
3237
/// Defines database type and connection string for connection.
3338
/// </summary>
@@ -42,6 +47,8 @@ public DBContext(DB database, string connectionString)
4247
MySqlConnectionString = connectionString;
4348
else if(Database.Equals(DB.Oracle))
4449
OracleConnectionString = connectionString;
50+
else if (Database.Equals(DB.PostgreSQL))
51+
PostgreSqlConnectionString = connectionString;
4552
}
4653

4754
/// <summary>
@@ -79,6 +86,14 @@ public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = fal
7986
dataList = _systemAdapter.FetchData(selectSql, oracleDBConnection.OracleConnection, upperCaseKeys);
8087
}
8188
}
89+
else if (Database.Equals(DB.PostgreSQL))
90+
{
91+
using (var postgreSqlDBConnection = GetPostgreSqlConnection())
92+
{
93+
var _systemAdapter = new PostgreSQL.Adapter();
94+
dataList = _systemAdapter.FetchData(selectSql, postgreSqlDBConnection.PostgreSQLConnection, upperCaseKeys);
95+
}
96+
}
8297
return dataList;
8398
}
8499

@@ -108,6 +123,15 @@ internal static Oracle.Connection GetOracleConnection()
108123
{
109124
return ConnectionBuilder.GetOracleConnection;
110125
}
126+
127+
/// <summary>
128+
/// Gets 'PostgreSQL' connection.
129+
/// </summary>
130+
/// <returns>'PostgreSQL' connection.</returns>
131+
internal static PostgreSQL.Connection GetPostgreSqlConnection()
132+
{
133+
return ConnectionBuilder.GetPostgreSqlConnection;
134+
}
111135
}
112136

113137
/// <summary>
@@ -117,6 +141,7 @@ public enum DB
117141
{
118142
MSSQL = 1,
119143
MySQL = 2,
120-
Oracle = 3
144+
Oracle = 3,
145+
PostgreSQL = 4
121146
}
122147
}

0 commit comments

Comments
 (0)