Fixed DataSourceFactory

This commit is contained in:
Lunastra 2021-12-11 15:34:18 -08:00
parent f8bafc8b6d
commit 698aa45318
3 changed files with 43 additions and 32 deletions

View File

@ -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)

View File

@ -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<string> getDataSource(string name)
public IDataSource<string> 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;
}

View File

@ -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<string> datasource = new SqlDAO(dbInfo);
IDataSource<string> 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. ");