From 9db6ab7db7c23dbb4fe67af8b696380921dde38a Mon Sep 17 00:00:00 2001 From: Lunastra Date: Sat, 11 Dec 2021 22:34:47 -0800 Subject: [PATCH] Create copy to file method --- .../ArchiveController.cs | 16 ++++++- .../Implementations/SqlDAO.cs | 42 ++++++++++++++++++- Source Code/main/Controller.cs | 15 ++++++- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs index 2661298..6fda930 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveController.cs @@ -17,7 +17,7 @@ namespace TeamHobby.HobbyProjectGenerator.Archive public bool CreateArchiveFolder(){ string curPath = Directory.GetCurrentDirectory(); //string temp = @ - string archiveFolder = curPath + "\\HobbyArhive"; + string archiveFolder = curPath + "\\HobbyArchive"; //Console.WriteLine("Creating a new folder at: {0}", archiveFolder); if (!Directory.Exists(archiveFolder)) { @@ -30,8 +30,14 @@ namespace TeamHobby.HobbyProjectGenerator.Archive // Return the name of the new output file with Path public string CreateOutFileName() { try { - string path = Directory.GetCurrentDirectory(); + string path = Directory.GetCurrentDirectory() + "\\HobbyArchive"; Console.WriteLine("The current directory is {0}", path); + string date = DateTime.Now.ToString("M_d_yyyy"); + string fileName = date + "_archive.csv"; + string filePath = System.IO.Path.Combine(path, fileName); + + Console.WriteLine("Date: {0}", date); + Console.WriteLine("Filepath: {0}", filePath); return path; } catch @@ -43,6 +49,12 @@ namespace TeamHobby.HobbyProjectGenerator.Archive // put everything toget here public bool Controller(){ + + // Create the folder for Archiving + CreateArchiveFolder(); + + // Create the file name for output + CreateOutFileName(); return true; } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index d68b6bf..c2ae271 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs @@ -36,7 +36,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess return _conn; } - // Closing a connection here will cause a problem + // TODO: 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) { @@ -175,7 +175,45 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess // Copy the Sql data to the a text file public bool CopyToFile(string filePath){ - return true; + try + { + _conn.Open(); + // Conver backward slash in to forward slash + filePath = filePath.Replace("\\", "/"); + string sqlQuery = "SELECT 'LtimeStamp', 'logID', 'LvName', 'catName', 'userOP', 'logMessage' " + + "UNION ALL " + + "SELECT* FROM log " + + "WHERE DATEDIFF(CURRENT_TIMESTAMP, log.LtimeStamp) > 30 " + + "INTO OUTFILE '" + filePath + "'" + + "FIELDS ENCLOSED BY ''" + + "TERMINATED BY ',' " + + "LINES TERMINATED BY '\r\n';"; + + Console.WriteLine(sqlQuery); + + OdbcCommand command = new OdbcCommand(sqlQuery, _conn); + //command.Parameters.AddWithValue("@filePath", filePath); + //command.Prepare(); + + Console.WriteLine("Output to a file started. "); + command.ExecuteNonQuery(); + Console.WriteLine("Output to a file completed. "); + + + _conn.Close(); + return true; + + } + catch + { + _conn.Close(); + Console.WriteLine("Error when copying query to a file !!"); + throw; + } + finally + { + _conn.Close(); + } } public bool RemoveOutputFile(string filePath){ diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index dfb67a9..ed04cef 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -70,12 +70,23 @@ namespace TeamHobby.HobbyProjectGenerator.Main // Testing archive Manager ArchiveController archive = new ArchiveController(datasource); - string curPath = archive.CreateOutFileName(); //Creating the folder Archive Console.WriteLine("Creating a new folder ..."); archive.CreateArchiveFolder(); - + Console.WriteLine(""); + + // Creating a file name: + Console.WriteLine("Creating file name ... "); + string curPath = archive.CreateOutFileName(); + + string pathForward = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\main\bin\Debug\net6.0\HobbyArchive"; + string pathTemp = "C:/Temp/oldlogs10.txt"; + string pathTempBack = @"C:\Temp\oldlogs10.txt"; + Console.WriteLine("----------------"); + + //Output SQL to a text file + sqlDS.CopyToFile(curPath); // 2.Inserting Data into the database: