Skip to content

Commit 721898a

Browse files
Merge pull request #2 from abhinavminhas/dev
MySQL - Database Addition
2 parents 6689204 + dc173c2 commit 721898a

File tree

11 files changed

+156
-6
lines changed

11 files changed

+156
-6
lines changed

QueryDB.Core.Tests/App.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<appSettings>
44
<add key="UseDocker" value="true" />
55
<add key="OracleConnection" value="Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SID = XE))); User Id = system;Password=Secret@1234;" />
6-
<add key="SQLServerConnection" value="Data Source=(local);Initial Catalog=master;User Id=SA;Password=Secret@1234;Encrypt=False" />
6+
<add key="SQLServerConnection" value="Data Source=(local);Initial Catalog=master;User Id=SA;Password=Secret@1234;Encrypt=False;" />
7+
<add key="MySQLConnection" value="server=localhost;database=mysql;uid=root;pwd=Secret@1234;" />
78
</appSettings>
89
</configuration>

QueryDB.Core.Tests/DatabaseTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,18 @@ public void Test_SqlServer_RetrieveData()
3131
data = dbContext.RetrieveData(selectSql);
3232
Assert.IsTrue(data.Count > 0);
3333
}
34+
35+
[TestMethod]
36+
[TestCategory(DB_TESTS)]
37+
public void Test_MySql_RetrieveData()
38+
{
39+
var selectSql = Queries.MySqlQuery.SelectSql;
40+
var data = new DBContext(DB.MySql, MySqlDatabaseString).RetrieveData(selectSql);
41+
Assert.IsTrue(data.Count > 0);
42+
43+
var dbContext = new DBContext(DB.MySql, MySqlDatabaseString);
44+
data = dbContext.RetrieveData(selectSql);
45+
Assert.IsTrue(data.Count > 0);
46+
}
3447
}
3548
}

QueryDB.Core.Tests/Queries.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ internal static class SqlServerQuery
1111
{
1212
public static string SelectSql = @"SELECT NAME FROM SYS.DATABASES";
1313
}
14+
15+
internal static class MySqlQuery
16+
{
17+
public static string SelectSql = @"SELECT SYSDATE()";
18+
}
1419
}
1520
}

QueryDB.Core.Tests/TestBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class TestBase
88
private readonly string _useDocker = ConfigurationManager.AppSettings["UseDocker"];
99
protected readonly string OracleDatabaseString = ConfigurationManager.AppSettings["OracleConnection"];
1010
protected readonly string SqlServerDatabaseString = ConfigurationManager.AppSettings["SQLServerConnection"];
11+
protected readonly string MySqlDatabaseString = ConfigurationManager.AppSettings["MySQLConnection"];
1112
protected const string DB_TESTS = "DB-TESTS";
1213

1314
[AssemblyInitialize]

QueryDB/Adapter/MySqlAdapter.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using MySql.Data.MySqlClient;
2+
using System.Data;
3+
4+
namespace QueryDB.Adapter
5+
{
6+
/// <summary>
7+
/// 'MySQL' adapter.
8+
/// </summary>
9+
internal class MySqlAdapter
10+
{
11+
/// <summary>
12+
/// Gets the 'MySQL' data reader.
13+
/// </summary>
14+
/// <param name="selectSql">'Select' sql value.</param>
15+
/// <param name="connection">'MySQL' connection.</param>
16+
/// <returns>'MySQL' data reader for the select sql.</returns>
17+
internal MySqlDataReader GetSqlReader(string selectSql, MySqlConnection connection)
18+
{
19+
connection.Open();
20+
using (var sqlCommand = new MySqlCommand(selectSql, connection) { CommandType = CommandType.Text })
21+
{
22+
return sqlCommand.ExecuteReader();
23+
}
24+
}
25+
}
26+
}

QueryDB/Connection/ConnectionBuilder.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace QueryDB.Connection
88
internal class ConnectionBuilder
99
{
1010
/// <summary>
11-
/// Creates 'Oracle' database connection.
11+
/// Gets 'Oracle' database connection.
1212
/// </summary>
1313
internal OracleDBConnection GetOracleConnection
1414
{
@@ -20,7 +20,7 @@ internal OracleDBConnection GetOracleConnection
2020
}
2121

2222
/// <summary>
23-
/// Creates 'Sql Server' database connection.
23+
/// Gets 'Sql Server' database connection.
2424
/// </summary>
2525
internal SqlDBConnection GetSqlServerConnection
2626
{
@@ -30,5 +30,17 @@ internal SqlDBConnection GetSqlServerConnection
3030
return connection;
3131
}
3232
}
33+
34+
/// <summary>
35+
/// Gets 'MySQL' database connection.
36+
/// </summary>
37+
internal MySqlDBConnection GetMySqlConnection
38+
{
39+
get
40+
{
41+
var connection = new MySqlDBConnection(DBContext.MySqlConnectionString);
42+
return connection;
43+
}
44+
}
3345
}
3446
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using MySql.Data.MySqlClient;
2+
using System;
3+
4+
namespace QueryDB.Connection.Database
5+
{
6+
/// <summary>
7+
/// 'MySQL' database connection.
8+
/// </summary>
9+
internal class MySqlDBConnection : IDisposable
10+
{
11+
/// <summary>
12+
/// Holds 'MySQL' connection.
13+
/// </summary>
14+
internal MySqlConnection MySqlConnection { get; private set; }
15+
16+
/// <summary>
17+
/// Creates 'MySQL' database connection.
18+
/// </summary>
19+
/// <param name="connectionString">'MySQL' connection string value.</param>
20+
internal MySqlDBConnection(string connectionString)
21+
{
22+
MySqlConnection = new MySqlConnection(connectionString);
23+
}
24+
25+
/// <summary>
26+
/// Disposes 'MySQL' connection.
27+
/// </summary>
28+
public void Dispose()
29+
{
30+
if (MySqlConnection != null)
31+
MySqlConnection.Dispose();
32+
}
33+
34+
}
35+
}

QueryDB/DBContext.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class DBContext
2626
/// </summary>
2727
internal static string SqlServerConnectionString;
2828

29+
/// <summary>
30+
/// Holds 'MySQL' connection string value for the DBContext created.
31+
/// </summary>
32+
internal static string MySqlConnectionString;
33+
2934
/// <summary>
3035
/// Defines database type and connection string to connect to.
3136
/// </summary>
@@ -38,6 +43,8 @@ public DBContext(DB database, string connectionString)
3843
OracleConnectionString = connectionString;
3944
else if (Database.Equals(DB.SqlServer))
4045
SqlServerConnectionString = connectionString;
46+
else if (Database.Equals(DB.MySql))
47+
MySqlConnectionString = connectionString;
4148
}
4249

4350
/// <summary>
@@ -91,6 +98,26 @@ public List<DataDictionary> RetrieveData(string selectSql, bool upperCaseKeys =
9198
}
9299
}
93100
}
101+
else if (Database.Equals(DB.MySql))
102+
{
103+
using (var mySqlDBConnection = GetMySqlConnection())
104+
{
105+
var _systemAdapter = new MySqlAdapter();
106+
var reader = _systemAdapter.GetSqlReader(selectSql, mySqlDBConnection.MySqlConnection);
107+
while (reader.Read())
108+
{
109+
var addedRow = new DataDictionary();
110+
for (int i = 0; i < reader.FieldCount; i++)
111+
{
112+
if (upperCaseKeys)
113+
addedRow.Data.Add(reader.GetName(i).ToUpper(), reader.GetValue(i).ToString());
114+
else
115+
addedRow.Data.Add(reader.GetName(i), reader.GetValue(i).ToString());
116+
}
117+
dataList.Add(addedRow);
118+
}
119+
}
120+
}
94121
return dataList;
95122
}
96123

@@ -107,12 +134,22 @@ private OracleDBConnection GetOracleConnection()
107134
/// <summary>
108135
/// Gets 'Sql Server' connection.
109136
/// </summary>
110-
/// <returns>'Sql Server' Connection</returns>
137+
/// <returns>'Sql Server' Connection.</returns>
111138
private SqlDBConnection GetSqlServerConnection()
112139
{
113140
var _connectionBuilder = new ConnectionBuilder();
114141
return _connectionBuilder.GetSqlServerConnection;
115142
}
143+
144+
/// <summary>
145+
/// Gets 'MySQL' connection.
146+
/// </summary>
147+
/// <returns>'MySQL' Connection.</returns>
148+
private MySqlDBConnection GetMySqlConnection()
149+
{
150+
var _connectionBuilder = new ConnectionBuilder();
151+
return _connectionBuilder.GetMySqlConnection;
152+
}
116153
}
117154

118155
/// <summary>
@@ -121,6 +158,7 @@ private SqlDBConnection GetSqlServerConnection()
121158
public enum DB
122159
{
123160
Oracle,
124-
SqlServer
161+
SqlServer,
162+
MySql
125163
}
126164
}

QueryDB/QueryDB.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
9+
<PackageReference Include="MySql.Data" Version="8.0.31" />
910
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.170" />
1011
</ItemGroup>
1112

docker-compose-mysql.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "3"
2+
3+
services:
4+
mysql-db:
5+
image: mysql:8.0.31
6+
container_name: mysql-db
7+
environment:
8+
MYSQL_ROOT_PASSWORD: "Secret@1234"
9+
ports:
10+
- "3306:3306"

0 commit comments

Comments
 (0)