[ALL] Get rid of rest of non-library usings

This commit is contained in:
Matt Nadareski
2016-09-22 15:59:03 -07:00
parent 74fbe60686
commit 863e936d07
5 changed files with 196 additions and 316 deletions

View File

@@ -314,12 +314,9 @@ namespace SabreTools
// Populate the List from the database // Populate the List from the database
string query = "SELECT UNIQUE value FROM data WHERE key=\"dat\""; string query = "SELECT UNIQUE value FROM data WHERE key=\"dat\"";
using (SqliteConnection dbc = new SqliteConnection(_connectionString)) SqliteConnection dbc = new SqliteConnection(_connectionString);
{ SqliteCommand slc = new SqliteCommand(query, dbc);
using (SqliteCommand slc = new SqliteCommand(query, dbc)) SqliteDataReader sldr = slc.ExecuteReader();
{
using (SqliteDataReader sldr = slc.ExecuteReader())
{
if (sldr.HasRows) if (sldr.HasRows)
{ {
sldr.Read(); sldr.Read();
@@ -329,8 +326,9 @@ namespace SabreTools
databaseDats.Add(hash); databaseDats.Add(hash);
} }
} }
}
} slc.Dispose();
sldr.Dispose();
// Now create a Dictionary of dats to parse from what's not in the database (SHA-1, Path) // Now create a Dictionary of dats to parse from what's not in the database (SHA-1, Path)
Dictionary<string, string> toscan = new Dictionary<string, string>(); Dictionary<string, string> toscan = new Dictionary<string, string>();
@@ -372,10 +370,9 @@ namespace SabreTools
+ "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))" + "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))"
+ "AND (key=\"md5\" AND value=\"" + rom.MD5 + "\" OR value=\"null\"))" + "AND (key=\"md5\" AND value=\"" + rom.MD5 + "\" OR value=\"null\"))"
+ "AND (key=\"sha1\" AND value=\"" + rom.SHA1 + "\" OR value=\"null\")))"; + "AND (key=\"sha1\" AND value=\"" + rom.SHA1 + "\" OR value=\"null\")))";
using (SqliteCommand slc = new SqliteCommand(query, dbc)) slc = new SqliteCommand(query, dbc);
{ sldr = slc.ExecuteReader();
using (SqliteDataReader sldr = slc.ExecuteReader())
{
// If the hash exists in the database, add the dat hash for that id // If the hash exists in the database, add the dat hash for that id
if (sldr.HasRows) if (sldr.HasRows)
{ {
@@ -383,55 +380,44 @@ namespace SabreTools
string id = sldr.GetString(0); string id = sldr.GetString(0);
string squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"dat\", \"" + key + "\")"; string squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"dat\", \"" + key + "\")";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) SqliteCommand sslc = new SqliteCommand(squery, dbc);
{
sslc.ExecuteNonQuery(); sslc.ExecuteNonQuery();
} sslc.Dispose();
} }
// If it doesn't exist, add the hash and the dat hash for a new id // If it doesn't exist, add the hash and the dat hash for a new id
else else
{ {
string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")"; string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) SqliteCommand sslc = new SqliteCommand(squery, dbc);
{
sslc.ExecuteNonQuery(); sslc.ExecuteNonQuery();
}
long id = -1; long id = -1;
squery = "SELECT last_insertConstants.Rowid()"; squery = "SELECT last_insertConstants.Rowid()";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) sslc = new SqliteCommand(squery, dbc);
{
id = (long)sslc.ExecuteScalar(); id = (long)sslc.ExecuteScalar();
}
squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.CRC + "\")," squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.CRC + "\"),"
+ " (\"" + id + "\", \"md5\", \"" + rom.MD5 + "\")," + " (\"" + id + "\", \"md5\", \"" + rom.MD5 + "\"),"
+ " (\"" + id + "\", \"sha1\", \"" + rom.SHA1 + "\")," + " (\"" + id + "\", \"sha1\", \"" + rom.SHA1 + "\"),"
+ " (\"" + id + "\", \"dat\", \"" + key + "\")," + " (\"" + id + "\", \"dat\", \"" + key + "\"),"
+ " (\"" + id + "\", \"exists\", \"false\")"; + " (\"" + id + "\", \"exists\", \"false\")";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) sslc = new SqliteCommand(squery, dbc);
{
sslc.ExecuteNonQuery(); sslc.ExecuteNonQuery();
} }
} }
} }
} }
}
}
}
}
// Now loop through and remove all references to old Dats // Now loop through and remove all references to old Dats
// TODO: Remove orphaned files as well // TODO: Remove orphaned files as well
foreach (string dathash in databaseDats) foreach (string dathash in databaseDats)
{ {
query = "DELETE FROM data WHERE key=\"dat\" AND value=\"" + dathash + "\""; query = "DELETE FROM data WHERE key=\"dat\" AND value=\"" + dathash + "\"";
using (SqliteCommand slc = new SqliteCommand(query, dbc)) slc = new SqliteCommand(query, dbc);
{
slc.ExecuteNonQuery(); slc.ExecuteNonQuery();
} slc.Dispose();
} }
} }
} }

View File

@@ -982,12 +982,13 @@ namespace SabreTools.Helper
return; return;
} }
// Now get the hash of the stream // Create the hashers
uint tempCrc; uint tempCrc;
using (OptimizedCRC crc = new OptimizedCRC()) OptimizedCRC crc = new OptimizedCRC();
using (MD5 md5 = System.Security.Cryptography.MD5.Create()) MD5 md5 = System.Security.Cryptography.MD5.Create();
using (SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
{
// Now get the hash of the stream
BinaryReader fs = new BinaryReader(stream); BinaryReader fs = new BinaryReader(stream);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
@@ -1006,7 +1007,11 @@ namespace SabreTools.Helper
tempCrc = crc.UnsignedValue; tempCrc = crc.UnsignedValue;
_md5 = md5.Hash; _md5 = md5.Hash;
_sha1 = sha1.Hash; _sha1 = sha1.Hash;
}
// Dispose of the hashers
crc.Dispose();
md5.Dispose();
sha1.Dispose();
if (_compressionMethod == CompressionMethod.Deflated) if (_compressionMethod == CompressionMethod.Deflated)
{ {

View File

@@ -149,18 +149,14 @@ namespace SabreTools
{ {
bool exists = false; bool exists = false;
string query = @"SELECT * FROM data WHERE sha1='" + SHA1 + "' AND header='" + header + "'"; // Open the database connection
using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
{
dbc.Open(); dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{ string query = @"SELECT * FROM data WHERE sha1='" + SHA1 + "' AND header='" + header + "'";
using (SqliteDataReader sldr = slc.ExecuteReader()) SqliteCommand slc = new SqliteCommand(query, dbc);
{ SqliteDataReader sldr = slc.ExecuteReader();
exists = sldr.HasRows; exists = sldr.HasRows;
}
}
}
if (!exists) if (!exists)
{ {
@@ -168,15 +164,14 @@ namespace SabreTools
SHA1 + "', " + SHA1 + "', " +
"'" + header + "', " + "'" + header + "', " +
"'" + type.ToString() + "')"; "'" + type.ToString() + "')";
using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) slc = new SqliteCommand(query, dbc);
{
dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
_logger.Log("Result of inserting header: " + slc.ExecuteNonQuery()); _logger.Log("Result of inserting header: " + slc.ExecuteNonQuery());
} }
}
} // Dispose of database objects
slc.Dispose();
sldr.Dispose();
dbc.Dispose();
} }
/// <summary> /// <summary>
@@ -186,20 +181,22 @@ namespace SabreTools
/// <returns>True if a header was found and appended, false otherwise</returns> /// <returns>True if a header was found and appended, false otherwise</returns>
private bool RestoreHeader(string file) private bool RestoreHeader(string file)
{ {
bool success = true;
// First, get the SHA-1 hash of the file // First, get the SHA-1 hash of the file
Rom rom = FileTools.GetSingleFileInfo(file); Rom rom = FileTools.GetSingleFileInfo(file);
// Then try to pull the corresponding headers from the database // Then try to pull the corresponding headers from the database
string header = ""; string header = "";
string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'"; // Open the database connection
using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
{
dbc.Open(); dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{ string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'";
using (SqliteDataReader sldr = slc.ExecuteReader()) SqliteCommand slc = new SqliteCommand(query, dbc);
{ SqliteDataReader sldr = slc.ExecuteReader();
if (sldr.HasRows) if (sldr.HasRows)
{ {
int sub = 0; int sub = 0;
@@ -218,13 +215,15 @@ namespace SabreTools
else else
{ {
_logger.Warning("No matching header could be found!"); _logger.Warning("No matching header could be found!");
return false; success = false;
}
}
}
} }
return true; // Dispose of database objects
slc.Dispose();
sldr.Dispose();
dbc.Dispose();
return success;
} }
} }
} }

View File

@@ -26,7 +26,7 @@ namespace SabreTools.Helper
SqliteConnection.CreateFile(db); SqliteConnection.CreateFile(db);
} }
// Connect to the file // Open the database connection
SqliteConnection dbc = new SqliteConnection(connectionString); SqliteConnection dbc = new SqliteConnection(connectionString);
dbc.Open(); dbc.Open();
@@ -44,6 +44,7 @@ CREATE TABLE IF NOT EXISTS data (
)"; )";
SqliteCommand slc = new SqliteCommand(query, dbc); SqliteCommand slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery(); slc.ExecuteNonQuery();
slc.Dispose();
} }
else if (type == "headerer") else if (type == "headerer")
{ {
@@ -56,6 +57,7 @@ CREATE TABLE IF NOT EXISTS data (
)"; )";
SqliteCommand slc = new SqliteCommand(query, dbc); SqliteCommand slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery(); slc.ExecuteNonQuery();
slc.Dispose();
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -64,124 +66,7 @@ CREATE TABLE IF NOT EXISTS data (
} }
finally finally
{ {
// Close and return the database connection dbc.Dispose();
dbc.Close();
}
}
/// <summary>
/// Add a new source to the database if it doesn't already exist
/// </summary>
/// <param name="name">Source name</param>
/// <param name="url">Source URL(s)</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the source existed or could be added, false otherwise</returns>
public static bool AddSource(string name, string url, string connectionString)
{
string query = "SELECT id, name, url FROM source WHERE name='" + name + "'";
using (SqliteConnection dbc = new SqliteConnection(connectionString))
{
dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
using (SqliteDataReader sldr = slc.ExecuteReader())
{
// If nothing is found, add the source
if (!sldr.HasRows)
{
string squery = "INSERT INTO source (name, url) VALUES ('" + name + "', '" + url + "')";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc))
{
return sslc.ExecuteNonQuery() >= 1;
}
}
// Otherwise, update the source URL if it's different
else
{
sldr.Read();
if (url != sldr.GetString(2))
{
string squery = "UPDATE source SET url='" + url + "' WHERE id=" + sldr.GetInt32(0);
using (SqliteCommand sslc = new SqliteCommand(squery, dbc))
{
return sslc.ExecuteNonQuery() >= 1;
}
}
}
}
}
}
return true;
}
/// <summary>
/// Remove an existing source from the database
/// </summary>
/// <param name="id">Source ID to be removed from the database</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the source was removed, false otherwise</returns>
public static bool RemoveSource(int id, string connectionString)
{
string query = "DELETE FROM source WHERE id=" + id;
using (SqliteConnection dbc = new SqliteConnection(connectionString))
{
dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
return slc.ExecuteNonQuery() >= 1;
}
}
}
/// <summary>
/// Add a new system to the database if it doesn't already exist
/// </summary>
/// <param name="manufacturer">Manufacturer name</param>
/// <param name="system">System name</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the system existed or could be added, false otherwise</returns>
public static bool AddSystem(string manufacturer, string system, string connectionString)
{
string query = "SELECT id, manufacturer, name FROM system WHERE manufacturer='" + manufacturer + "' AND system='" + system + "'";
using (SqliteConnection dbc = new SqliteConnection(connectionString))
{
dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
using (SqliteDataReader sldr = slc.ExecuteReader())
{
// If nothing is found, add the system
if (!sldr.HasRows)
{
string squery = "INSERT INTO name (manufacturer, system) VALUES ('" + manufacturer + "', '" + system + "')";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc))
{
return sslc.ExecuteNonQuery() >= 1;
}
}
}
}
}
return true;
}
/// <summary>
/// Remove an existing system from the database
/// </summary>
/// <param name="id">System ID to be removed from the database</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the system was removed, false otherwise</returns>
public static bool RemoveSystem(int id, string connectionString)
{
string query = "DELETE FROM system WHERE id=" + id;
using (SqliteConnection dbc = new SqliteConnection(connectionString))
{
dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
return slc.ExecuteNonQuery() >= 1;
}
} }
} }
} }

View File

@@ -726,10 +726,11 @@ namespace SabreTools.Helper
try try
{ {
using (OptimizedCRC crc = new OptimizedCRC()) // Initialize the hashers
using (MD5 md5 = MD5.Create()) OptimizedCRC crc = new OptimizedCRC();
using (SHA1 sha1 = SHA1.Create()) MD5 md5 = MD5.Create();
{ SHA1 sha1 = SHA1.Create();
// Seek to the starting position, if one is set // Seek to the starting position, if one is set
if (offset > 0) if (offset > 0)
{ {
@@ -764,7 +765,11 @@ namespace SabreTools.Helper
sha1.TransformFinalBlock(buffer, 0, 0); sha1.TransformFinalBlock(buffer, 0, 0);
rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant();
} }
}
// Dispose of the hashers
crc.Dispose();
md5.Dispose();
sha1.Dispose();
} }
catch (IOException) catch (IOException)
{ {