Fixed DataSourceFactory
This commit is contained in:
parent
f8bafc8b6d
commit
698aa45318
@ -31,6 +31,11 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
|
|||||||
// Getter and setter for Odbc
|
// Getter and setter for Odbc
|
||||||
public OdbcConnection Connection { get; set; }
|
public OdbcConnection Connection { get; set; }
|
||||||
|
|
||||||
|
public OdbcConnection getConnection()
|
||||||
|
{
|
||||||
|
return _conn;
|
||||||
|
}
|
||||||
|
|
||||||
// Closing a connection here will cause a problem
|
// Closing a connection here will cause a problem
|
||||||
// Make sure whoever called this need to close the connection until we can fixed it.
|
// Make sure whoever called this need to close the connection until we can fixed it.
|
||||||
public Object? ReadData(string cmd)
|
public Object? ReadData(string cmd)
|
||||||
|
|||||||
@ -3,29 +3,26 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using TeamHobby.HobbyProjectGenerator.Archive;
|
|
||||||
using TeamHobby.HobbyProjectGenerator.DataAccess;
|
using TeamHobby.HobbyProjectGenerator.DataAccess;
|
||||||
|
|
||||||
namespace TeamHobby.HobbyProjectGenerator.Archive
|
namespace TeamHobby.HobbyProjectGenerator.DataAccess
|
||||||
{
|
{
|
||||||
public class RelationalDataSourceFactory
|
public class RelationalDataSourceFactory
|
||||||
{
|
{
|
||||||
// Mehthod to create a specific data source suitable to the need
|
// 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
|
try
|
||||||
{
|
{
|
||||||
if (String.Equals(name, "SQL", StringComparison.OrdinalIgnoreCase))
|
if (String.Equals(name, "SQL", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return null;
|
return new SqlDAO(info);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("RelationDataFactory: Data Access object creation failed!");
|
||||||
Console.WriteLine(ex.Message);
|
Console.WriteLine(ex.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,12 +26,16 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
|||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
GetCredentials credentials = new GetCredentials();
|
//GetCredentials credentials = new GetCredentials();
|
||||||
string? username = credentials.GetUserName();
|
//string? username = credentials.GetUserName();
|
||||||
string? password = credentials.GetPassword();
|
//string? password = credentials.GetPassword();
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine(value: $"username is {username}\npassword is {password}");
|
//Console.WriteLine(value: $"username is {username}\npassword is {password}");
|
||||||
|
|
||||||
|
// Creating the Factory class
|
||||||
|
string dbType = "sql";
|
||||||
|
RelationalDataSourceFactory dbFactory = new RelationalDataSourceFactory();
|
||||||
|
|
||||||
// Testing Data Access Layer
|
// Testing Data Access Layer
|
||||||
string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" +
|
string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" +
|
||||||
@ -40,26 +44,30 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
|||||||
"UID=root;" +
|
"UID=root;" +
|
||||||
"PASSWORD=Teamhobby;" +
|
"PASSWORD=Teamhobby;" +
|
||||||
"OPTION=3";
|
"OPTION=3";
|
||||||
IDataSource<string> datasource = new SqlDAO(dbInfo);
|
IDataSource<string> datasource = dbFactory.getDataSource(dbType,dbInfo);
|
||||||
|
|
||||||
string sqlQuery = "Select * from log;";
|
string sqlQuery = "Select * from log;";
|
||||||
//Object result = datasource.ReadData(sqlQuery);
|
Object result = datasource.ReadData(sqlQuery);
|
||||||
//Console.WriteLine("type of Result: " + result.GetType());
|
Console.WriteLine("type of Result: " + result.GetType());
|
||||||
//OdbcDataReader reader = null;
|
OdbcDataReader reader = null;
|
||||||
|
|
||||||
//if (result.GetType() == typeof(OdbcDataReader))
|
if (result.GetType() == typeof(OdbcDataReader))
|
||||||
//{
|
{
|
||||||
// reader = (OdbcDataReader)result;
|
reader = (OdbcDataReader)result;
|
||||||
|
|
||||||
//}
|
}
|
||||||
|
|
||||||
//Console.WriteLine("Reading from the database");
|
Console.WriteLine("Reading from the database");
|
||||||
//while (reader.Read())
|
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("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 " +
|
//string sqlWrite = "INSERT into log(lvname, catname, userop, logmessage) values " +
|
||||||
// "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');";
|
// "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');";
|
||||||
|
|
||||||
@ -67,10 +75,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
|||||||
//datasource.WriteData(sqlWrite);
|
//datasource.WriteData(sqlWrite);
|
||||||
//Console.WriteLine("Writing completed. ");
|
//Console.WriteLine("Writing completed. ");
|
||||||
|
|
||||||
string sqlRemove = "DELETE from log where logID = 28;";
|
// 3. Removing from a database
|
||||||
Console.WriteLine("Writing to the database... ");
|
//string sqlRemove = "DELETE from log where logID = 28;";
|
||||||
datasource.WriteData(sqlRemove);
|
//Console.WriteLine("Writing to the database... ");
|
||||||
Console.WriteLine("Writing completed. ");
|
//datasource.WriteData(sqlRemove);
|
||||||
|
//Console.WriteLine("Writing completed. ");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user