2014-07-24 16:42:30 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Data.SqlServerCe ;
2014-11-07 00:39:42 +00:00
using System.Data ;
2014-07-24 16:42:30 +01:00
using System.IO ;
2014-11-09 18:18:09 +00:00
using Saviour_Backup_System.Properties ;
2014-11-09 16:11:10 +00:00
2014-07-24 16:42:30 +01:00
namespace Saviour_Backup_System
{
2014-11-09 16:11:10 +00:00
class databaseTools
2014-07-24 16:42:30 +01:00
{
2014-11-11 22:55:14 +00:00
public static string databaseName = "saviour.sdf" ;
public static SqlCeConnection conn = new SqlCeConnection ( "Data Source = " + databaseName + "; password=12a712d7e6f71ed07822c219318da2c0" ) ; //password is a hash
2014-11-07 00:39:42 +00:00
private static SqlCeCommand cmd = conn . CreateCommand ( ) ;
2014-07-24 20:27:22 +01:00
2014-12-04 23:19:17 +00:00
private static void copyDatabase ( ) { File . WriteAllBytes ( @"" + databaseName , Resources . saviour ) ; } //copy the file from resources
2014-11-07 00:39:42 +00:00
2014-11-11 22:55:14 +00:00
public static void init ( ) {
2014-11-09 18:18:09 +00:00
if ( ! File . Exists ( databaseName ) ) { //if the database doesnt exists (program hasnt been run before)
2014-11-10 14:47:33 +00:00
copyDatabase ( ) ;
2014-11-09 18:18:09 +00:00
}
2014-12-30 17:31:14 +00:00
conn . Open ( ) ;
conn . Close ( ) ;
2014-07-24 16:42:30 +01:00
}
2014-12-04 12:40:50 +00:00
2014-12-04 23:19:17 +00:00
public static string getDriveName ( string id ) {
2014-12-29 18:58:04 +00:00
string name = "NONE" ;
2014-12-04 23:19:17 +00:00
conn . Open ( ) ;
2014-12-30 17:31:14 +00:00
cmd . CommandText = "SELECT Name FROM Drive WHERE ID LIKE ?;" ;
2014-12-04 23:19:17 +00:00
cmd . Parameters . Add ( new SqlCeParameter ( "Drive_ID" , SqlDbType . NText ) ) ;
cmd . Parameters [ "Drive_ID" ] . Value = id ;
SqlCeDataReader reader = cmd . ExecuteReader ( ) ;
while ( reader . Read ( ) ) { name = reader . GetString ( 0 ) ; }
conn . Close ( ) ;
reader . Close ( ) ;
cmd . Parameters . Clear ( ) ;
return name ;
}
public static string getBackupDirectory ( string id ) {
2014-12-29 18:58:04 +00:00
string directory = "NONE" ;
2014-11-10 16:36:14 +00:00
conn . Open ( ) ;
2014-12-30 17:31:14 +00:00
cmd . CommandText = "SELECT Backup_Location FROM Recordset WHERE Drive_ID LIKE ?" ;
2014-12-04 12:40:50 +00:00
cmd . Parameters . Add ( new SqlCeParameter ( "Drive_ID" , SqlDbType . NText ) ) ;
cmd . Parameters [ "Drive_ID" ] . Value = id ;
2014-12-29 18:58:04 +00:00
try {
SqlCeDataReader reader = cmd . ExecuteReader ( ) ;
while ( reader . Read ( ) ) { directory = reader . GetString ( 0 ) ; }
reader . Close ( ) ;
} catch { }
2014-12-04 23:19:17 +00:00
conn . Close ( ) ;
cmd . Parameters . Clear ( ) ;
return directory ;
2014-11-10 16:36:14 +00:00
}
2014-12-29 18:58:04 +00:00
public static Int64 getBackupCreationDate ( string id )
{
Int64 date = 0 ;
conn . Open ( ) ;
2014-12-30 17:31:14 +00:00
cmd . CommandText = "SELECT Creation_Date FROM Recordset WHERE Drive_ID LIKE ?" ;
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters [ "Drive ID" ] . Value = id ;
2014-12-29 18:58:04 +00:00
try
{
SqlCeDataReader reader = cmd . ExecuteReader ( ) ;
while ( reader . Read ( ) ) { date = reader . GetInt64 ( 0 ) ; }
reader . Close ( ) ;
}
catch { }
conn . Close ( ) ;
cmd . Parameters . Clear ( ) ;
return date ;
}
2014-12-04 12:40:50 +00:00
public static string [ ] getAutomaticBackups ( )
2014-11-10 16:36:14 +00:00
{
2014-12-04 12:40:50 +00:00
conn . Open ( ) ;
List < string > IDs = new List < string > ( ) ;
cmd . CommandText = "SELECT Drive_ID FROM Recordset WHERE Automatic = 1;" ;
SqlCeDataReader reader = cmd . ExecuteReader ( CommandBehavior . CloseConnection ) ;
while ( reader . Read ( ) )
{
string DriveID = reader . GetString ( 0 ) ;
IDs . Add ( DriveID ) ;
}
reader . Close ( ) ;
conn . Close ( ) ;
return IDs . ToArray ( ) ;
2014-11-10 16:36:14 +00:00
}
2014-12-12 18:06:43 +00:00
public static string getBackupName ( DriveInfo drive )
{
string name = "" ;
conn . Open ( ) ;
2014-12-30 17:31:14 +00:00
cmd . CommandText = "SELECT Name FROM Recordset WHERE Drive_ID LIKE ?;" ;
2014-12-12 18:06:43 +00:00
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters [ "Drive ID" ] . Value = USBTools . calculateDriveID ( drive ) ;
SqlCeDataReader reader = cmd . ExecuteReader ( ) ;
while ( reader . Read ( ) )
{
name = reader . GetString ( 0 ) ;
}
2014-12-30 17:31:14 +00:00
cmd . Parameters . Clear ( ) ;
reader . Close ( ) ;
conn . Close ( ) ;
2014-12-12 18:06:43 +00:00
return name ;
}
2014-12-15 16:12:57 +00:00
public static void createBackupRecord ( DriveInfo drive , Int64 startDate , Int64 duration , string hash )
2014-12-12 18:06:43 +00:00
{
string id = USBTools . calculateDriveID ( drive ) ;
2014-12-30 17:31:14 +00:00
string backupName = getBackupName ( drive ) ;
2014-12-12 18:06:43 +00:00
conn . Open ( ) ;
cmd . CommandText = "INSERT INTO Backups VALUES (?,?,?,?,?);" ;
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Start Date" , SqlDbType . BigInt ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Backup Name" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Hash" , SqlDbType . NText ) ) ;
cmd . Parameters . Add ( new SqlCeParameter ( "Duration" , SqlDbType . Int ) ) ;
cmd . Parameters [ "Drive ID" ] . Value = id ;
cmd . Parameters [ "Start Date" ] . Value = startDate ;
2014-12-30 17:31:14 +00:00
cmd . Parameters [ "Backup Name" ] . Value = backupName ;
2014-12-15 16:12:57 +00:00
cmd . Parameters [ "Hash" ] . Value = hash ;
cmd . Parameters [ "Duration" ] . Value = ( int ) duration ;
2014-12-12 18:06:43 +00:00
cmd . ExecuteNonQuery ( ) ;
cmd . Parameters . Clear ( ) ;
conn . Close ( ) ;
}
2014-12-08 18:09:16 +00:00
public static string getHashofRecentBackup ( string id )
{
conn . Open ( ) ;
2014-12-12 18:06:43 +00:00
string hash = "NONE" ;
cmd . CommandText = "SELECT Hash FROM Backups WHERE Drive_ID Like ?;" ;
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters [ "Drive ID" ] . Value = id ;
SqlCeDataReader reader = cmd . ExecuteReader ( ) ;
while ( reader . Read ( ) ) {
hash = reader . GetString ( 0 ) ;
}
2014-12-30 17:31:14 +00:00
cmd . Parameters . Clear ( ) ;
reader . Close ( ) ;
conn . Close ( ) ;
2014-12-12 18:06:43 +00:00
return hash ;
2014-12-08 18:09:16 +00:00
}
2015-01-01 16:45:23 +00:00
2015-01-04 14:35:19 +00:00
public static bool isCompression ( string id )
{
conn . Open ( ) ;
bool compression = false ;
cmd . CommandText = "SELECT Compression FROM Recordset WHERE Drive_ID Like ?;" ;
cmd . Parameters . Add ( new SqlCeParameter ( "Drive ID" , SqlDbType . NText ) ) ;
cmd . Parameters [ "Drive ID" ] . Value = id ;
SqlCeDataReader reader = cmd . ExecuteReader ( ) ;
while ( reader . Read ( ) ) {
compression = reader . GetBoolean ( 0 ) ;
}
cmd . Parameters . Clear ( ) ;
reader . Close ( ) ;
conn . Close ( ) ;
return compression ;
}
2015-01-01 16:45:23 +00:00
public static DataTable getAllDriveBackups ( )
{
DataTable table = new DataTable ( ) ;
conn . Open ( ) ;
cmd . CommandText =
"SELECT Recordset.Name as Backup_Name, Recordset.Creation_Date, Recordset.Backup_Location, Drive.Name, Drive.Capacity FROM Recordset, Drive" ; //write this out in compactview frst;
using ( SqlCeDataAdapter adapter = new SqlCeDataAdapter ( cmd . CommandText , conn ) ) {
adapter . Fill ( table ) ;
conn . Close ( ) ;
return table ;
}
}
2014-11-10 16:36:14 +00:00
}
2014-07-24 16:42:30 +01:00
}