From 698aa45318d9015b0515dc158f0da77c533568e7 Mon Sep 17 00:00:00 2001 From: Lunastra Date: Sat, 11 Dec 2021 15:34:18 -0800 Subject: [PATCH] Fixed DataSourceFactory --- .../Implementations/SqlDAO.cs | 5 ++ .../RelationalDataSourceFactory.cs | 13 ++--- Source Code/main/Controller.cs | 57 +++++++++++-------- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index 87e0367..d68b6bf 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs @@ -31,6 +31,11 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess // Getter and setter for Odbc public OdbcConnection Connection { get; set; } + public OdbcConnection getConnection() + { + return _conn; + } + // Closing a connection here will cause a problem // Make sure whoever called this need to close the connection until we can fixed it. public Object? ReadData(string cmd) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/RelationalDataSourceFactory.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/RelationalDataSourceFactory.cs index ac8fda6..656ebd1 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/RelationalDataSourceFactory.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/RelationalDataSourceFactory.cs @@ -3,29 +3,26 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.DataAccess; -namespace TeamHobby.HobbyProjectGenerator.Archive +namespace TeamHobby.HobbyProjectGenerator.DataAccess { public class RelationalDataSourceFactory { // Mehthod to create a specific data source suitable to the need - public IDataSource getDataSource(string name) + public IDataSource getDataSource(string name, string info) { try { if (String.Equals(name, "SQL", StringComparison.OrdinalIgnoreCase)) { - return null; - } - else - { - return null; + return new SqlDAO(info); } + return null; } catch (Exception ex) { + Console.WriteLine("RelationDataFactory: Data Access object creation failed!"); Console.WriteLine(ex.Message); return null; } diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index 6373ae7..1932939 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -26,12 +26,16 @@ namespace TeamHobby.HobbyProjectGenerator.Main public static void Main(string[] args) { - GetCredentials credentials = new GetCredentials(); - string? username = credentials.GetUserName(); - string? password = credentials.GetPassword(); - - - Console.WriteLine(value: $"username is {username}\npassword is {password}"); + //GetCredentials credentials = new GetCredentials(); + //string? username = credentials.GetUserName(); + //string? password = credentials.GetPassword(); + + + //Console.WriteLine(value: $"username is {username}\npassword is {password}"); + + // Creating the Factory class + string dbType = "sql"; + RelationalDataSourceFactory dbFactory = new RelationalDataSourceFactory(); // Testing Data Access Layer string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + @@ -40,26 +44,30 @@ namespace TeamHobby.HobbyProjectGenerator.Main "UID=root;" + "PASSWORD=Teamhobby;" + "OPTION=3"; - IDataSource datasource = new SqlDAO(dbInfo); + IDataSource datasource = dbFactory.getDataSource(dbType,dbInfo); string sqlQuery = "Select * from log;"; - //Object result = datasource.ReadData(sqlQuery); - //Console.WriteLine("type of Result: " + result.GetType()); - //OdbcDataReader reader = null; + Object result = datasource.ReadData(sqlQuery); + Console.WriteLine("type of Result: " + result.GetType()); + OdbcDataReader reader = null; - //if (result.GetType() == typeof(OdbcDataReader)) - //{ - // reader = (OdbcDataReader)result; + if (result.GetType() == typeof(OdbcDataReader)) + { + reader = (OdbcDataReader)result; - //} + } - //Console.WriteLine("Reading from the database"); - //while (reader.Read()) - //{ - // Console.WriteLine("Date={0} {1} {2} {3} {4} {5}", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]); - //} + Console.WriteLine("Reading from the database"); + while (reader.Read()) + { + Console.WriteLine("Date={0} {1} {2} {3} {4} {5}", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]); + } + SqlDAO sqlDS = (SqlDAO)datasource; - // Inserting Data into the database: + // Closing the connection + sqlDS.getConnection().Close(); + + // 2.Inserting Data into the database: //string sqlWrite = "INSERT into log(lvname, catname, userop, logmessage) values " + // "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');"; @@ -67,10 +75,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main //datasource.WriteData(sqlWrite); //Console.WriteLine("Writing completed. "); - string sqlRemove = "DELETE from log where logID = 28;"; - Console.WriteLine("Writing to the database... "); - datasource.WriteData(sqlRemove); - Console.WriteLine("Writing completed. "); + // 3. Removing from a database + //string sqlRemove = "DELETE from log where logID = 28;"; + //Console.WriteLine("Writing to the database... "); + //datasource.WriteData(sqlRemove); + //Console.WriteLine("Writing completed. ");