diff --git a/osrepodbmgr/ChangeLog b/osrepodbmgr/ChangeLog index 083d2e2..d1ba238 100644 --- a/osrepodbmgr/ChangeLog +++ b/osrepodbmgr/ChangeLog @@ -1,3 +1,13 @@ +2017-04-23 Natalia Portillo + + * Core.cs: + * DBOps.cs: + * DBCore.cs: + * SQLite.cs: + * Program.cs: + * MainWindow.cs: + Adds files to an OS table. + 2017-04-23 Natalia Portillo * DBOps.cs: diff --git a/osrepodbmgr/Core.cs b/osrepodbmgr/Core.cs index 2f819fd..7442fec 100644 --- a/osrepodbmgr/Core.cs +++ b/osrepodbmgr/Core.cs @@ -100,11 +100,12 @@ namespace osrepodbmgr { try { - MainClass.hashes = new Dictionary(); + MainClass.hashes = new Dictionary(); long counter = 1; foreach(string file in MainClass.files) { string filesPath; + FileInfo fi = new FileInfo(file); if(!string.IsNullOrEmpty(MainClass.tmpFolder) && Directory.Exists(MainClass.tmpFolder)) filesPath = MainClass.tmpFolder; @@ -148,7 +149,17 @@ namespace osrepodbmgr fileStream.Close(); string hash = stringify(sha256Context.Final()); - MainClass.hashes.Add(relpath, hash); + + DBFile dbFile = new DBFile(); + dbFile.Attributes = fi.Attributes; + dbFile.CreationTimeUtc = fi.CreationTimeUtc; + dbFile.LastAccessTimeUtc = fi.LastAccessTimeUtc; + dbFile.LastWriteTimeUtc = fi.LastWriteTimeUtc; + dbFile.Length = fi.Length; + dbFile.Path = relpath; + dbFile.Sha256 = hash; + + MainClass.hashes.Add(relpath, dbFile); counter++; } if(Finished != null) @@ -168,13 +179,13 @@ namespace osrepodbmgr try { long counter = 0; - foreach(KeyValuePair kvp in MainClass.hashes) + foreach(KeyValuePair kvp in MainClass.hashes) { if(UpdateProgress != null) UpdateProgress(null, "Checking files in database", counter, MainClass.hashes.Count); if(AddEntry != null) - AddEntry(kvp.Key, kvp.Value, dbCore.DBEntries.ExistsFile(kvp.Value)); + AddEntry(kvp.Key, kvp.Value.Sha256, dbCore.DBOps.ExistsFile(kvp.Value.Sha256)); counter++; } @@ -195,20 +206,35 @@ namespace osrepodbmgr try { long counter = 0; - foreach(KeyValuePair kvp in MainClass.hashes) + foreach(KeyValuePair kvp in MainClass.hashes) { if(UpdateProgress != null) UpdateProgress(null, "Adding files to database", counter, MainClass.hashes.Count); - if(!dbCore.DBEntries.ExistsFile(kvp.Value)) - dbCore.DBEntries.AddFile(kvp.Value); + if(!dbCore.DBOps.ExistsFile(kvp.Value.Sha256)) + dbCore.DBOps.AddFile(kvp.Value.Sha256); counter++; } if(UpdateProgress != null) UpdateProgress(null, "Adding OS information", counter, MainClass.hashes.Count); - dbCore.DBEntries.AddOS(MainClass.dbInfo); + long osId; + dbCore.DBOps.AddOS(MainClass.dbInfo, out osId); + if(UpdateProgress != null) + UpdateProgress(null, "Creating OS table", counter, MainClass.hashes.Count); + dbCore.DBOps.CreateTableForOS(osId); + + counter = 0; + foreach(KeyValuePair kvp in MainClass.hashes) + { + if(UpdateProgress != null) + UpdateProgress(null, "Adding files to OS in database", counter, MainClass.hashes.Count); + + dbCore.DBOps.AddFileToOS(kvp.Value, osId); + + counter++; + } if(Finished != null) Finished(); diff --git a/osrepodbmgr/DBCore.cs b/osrepodbmgr/DBCore.cs index d72a68d..31cf145 100644 --- a/osrepodbmgr/DBCore.cs +++ b/osrepodbmgr/DBCore.cs @@ -37,8 +37,13 @@ namespace osrepodbmgr public abstract bool CreateDB(string database, string server, string user, string password); - public DBOps DBEntries; + public DBOps DBOps; public abstract IDbDataAdapter GetNewDataAdapter(); + + public abstract long LastInsertRowId + { + get; + } } } \ No newline at end of file diff --git a/osrepodbmgr/DBOps.cs b/osrepodbmgr/DBOps.cs index 676eb37..a9bfcdb 100644 --- a/osrepodbmgr/DBOps.cs +++ b/osrepodbmgr/DBOps.cs @@ -28,6 +28,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.IO; namespace osrepodbmgr { @@ -50,6 +51,18 @@ namespace osrepodbmgr public bool netinstall; } + public struct DBFile + { + public ulong Id; + public string Path; + public string Sha256; + public long Length; + public DateTime CreationTimeUtc; + public DateTime LastAccessTimeUtc; + public DateTime LastWriteTimeUtc; + public FileAttributes Attributes; + } + public class DBOps { readonly IDbConnection dbCon; @@ -180,7 +193,7 @@ namespace osrepodbmgr return dbcmd; } - public bool AddOS(DBEntry entry) + public bool AddOS(DBEntry entry, out long id) { IDbCommand dbcmd = GetOSCommand(entry); IDbTransaction trans = dbCon.BeginTransaction(); @@ -195,6 +208,8 @@ namespace osrepodbmgr trans.Commit(); dbcmd.Dispose(); + id = dbCore.LastInsertRowId; + return true; } @@ -249,6 +264,100 @@ namespace osrepodbmgr return false; } + + IDbCommand GetFileCommand(DBFile person) + { + IDbCommand dbcmd = dbCon.CreateCommand(); + + IDbDataParameter param1 = dbcmd.CreateParameter(); + IDbDataParameter param2 = dbcmd.CreateParameter(); + IDbDataParameter param3 = dbcmd.CreateParameter(); + IDbDataParameter param4 = dbcmd.CreateParameter(); + IDbDataParameter param5 = dbcmd.CreateParameter(); + IDbDataParameter param6 = dbcmd.CreateParameter(); + IDbDataParameter param7 = dbcmd.CreateParameter(); + + param1.ParameterName = "@path"; + param2.ParameterName = "@sha256"; + param3.ParameterName = "@length"; + param4.ParameterName = "@creation"; + param5.ParameterName = "@access"; + param6.ParameterName = "@modification"; + param7.ParameterName = "@attributes"; + + param1.DbType = DbType.String; + param2.DbType = DbType.String; + param3.DbType = DbType.String; + param4.DbType = DbType.String; + param5.DbType = DbType.String; + param6.DbType = DbType.String; + param7.DbType = DbType.Int32; + + param1.Value = person.Path; + param2.Value = person.Sha256; + param3.Value = person.Length; + param4.Value = person.CreationTimeUtc.ToString("yyyy-MM-dd HH:mm"); + param5.Value = person.LastAccessTimeUtc.ToString("yyyy-MM-dd HH:mm"); + param6.Value = person.LastWriteTimeUtc.ToString("yyyy-MM-dd HH:mm"); + param7.Value = (int)person.Attributes; + + dbcmd.Parameters.Add(param1); + dbcmd.Parameters.Add(param2); + dbcmd.Parameters.Add(param3); + dbcmd.Parameters.Add(param4); + dbcmd.Parameters.Add(param5); + dbcmd.Parameters.Add(param6); + dbcmd.Parameters.Add(param7); + + return dbcmd; + } + + public bool AddFileToOS(DBFile file, long os) + { + IDbCommand dbcmd = GetFileCommand(file); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = string.Format("INSERT INTO `os_{0}` (`path`, `sha256`, `length`, `creation`, `access`, `modification`, `attributes`)" + + " VALUES (@path, @sha256, @length, @creation, @access, @modification, @attributes)", os); + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } + + + public bool CreateTableForOS(long id) + { + IDbCommand dbcmd = dbCon.CreateCommand(); + IDbTransaction trans = dbCon.BeginTransaction(); + dbcmd.Transaction = trans; + + string sql = string.Format("DROP TABLE IF EXISTS `os_{0}`;\n\n" + + "CREATE TABLE IF NOT EXISTS `os_{0}` (\n"+ + " `id` INTEGER PRIMARY KEY AUTOINCREMENT,\n" + + " `path` VARCHAR(8192) NOT NULL,\n" + + " `sha256` VARCHAR(64) NOT NULL,\n\n" + + " `length` BIGINT NOT NULL,\n" + + " `creation` DATETIME NULL,\n" + + " `access` DATETIME NULL,\n" + + " `modification` DATETIME NULL,\n" + + " `attributes` INTEGER NULL);\n\n" + + "CREATE UNIQUE INDEX `os_{0}_id_UNIQUE` ON `os_{0}` (`id` ASC);\n\n" + + "CREATE INDEX `os_{0}_path_idx` ON `os_{0}` (`path` ASC);", id); + + dbcmd.CommandText = sql; + + dbcmd.ExecuteNonQuery(); + trans.Commit(); + dbcmd.Dispose(); + + return true; + } } } diff --git a/osrepodbmgr/MainWindow.cs b/osrepodbmgr/MainWindow.cs index 285d42c..78d8e19 100644 --- a/osrepodbmgr/MainWindow.cs +++ b/osrepodbmgr/MainWindow.cs @@ -612,10 +612,10 @@ public partial class MainWindow : Window long counter = 0; view.Clear(); - foreach(KeyValuePair kvp in MainClass.hashes) + foreach(KeyValuePair kvp in MainClass.hashes) { UpdateProgress(null, "Updating table", counter, MainClass.hashes.Count); - view.AppendValues(kvp.Key, kvp.Value, true, "green", "black"); + view.AppendValues(kvp.Key, kvp.Value.Path, true, "green", "black"); counter++; } @@ -741,7 +741,7 @@ public partial class MainWindow : Window btnPack.Sensitive = false; btnClose.Sensitive = true; - MessageDialog dlgMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Correctly packed to " + text); + MessageDialog dlgMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Correctly packed to " + text); dlgMsg.Run(); dlgMsg.Destroy(); }); diff --git a/osrepodbmgr/Program.cs b/osrepodbmgr/Program.cs index 79aded4..7f78325 100644 --- a/osrepodbmgr/Program.cs +++ b/osrepodbmgr/Program.cs @@ -35,7 +35,7 @@ namespace osrepodbmgr static class MainClass { public static List files; - public static Dictionary hashes; + public static Dictionary hashes; public static string path; public static DBEntry dbInfo; public static bool unarUsable; diff --git a/osrepodbmgr/SQLite.cs b/osrepodbmgr/SQLite.cs index 231bdb7..1a899be 100644 --- a/osrepodbmgr/SQLite.cs +++ b/osrepodbmgr/SQLite.cs @@ -65,7 +65,7 @@ namespace osrepodbmgr return false; } - DBEntries = new DBOps(dbCon, this); + DBOps = new DBOps(dbCon, this); return true; } @@ -83,7 +83,7 @@ namespace osrepodbmgr if(dbCon != null) dbCon.Close(); - DBEntries = null; + DBOps = null; } public override bool CreateDB(string database, string server, string user, string password) @@ -132,6 +132,11 @@ namespace osrepodbmgr return new SQLiteDataAdapter(); } + public override long LastInsertRowId + { + get { return dbCon.LastInsertRowId;} + } + #endregion } }