Added query function for easier queries.

Normalized method names in GroupManager.cs
This commit is contained in:
high 2011-07-24 10:12:18 -04:00
parent cda0002889
commit 57a2d082ef
8 changed files with 205 additions and 276 deletions

View file

@ -18,48 +18,42 @@ namespace TShockAPI.DB
{
database = db;
using (var com = database.CreateCommand())
string query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
"CREATE TABLE IF NOT EXISTS 'ItemBans' ('ItemName' TEXT PRIMARY KEY);" :
"CREATE TABLE IF NOT EXISTS ItemBans (ItemName VARCHAR(255) PRIMARY);";
database.Query(query);
String file = Path.Combine(TShock.SavePath, "itembans.txt");
if (File.Exists(file))
{
if (TShock.Config.StorageType.ToLower() == "sqlite")
com.CommandText =
"CREATE TABLE IF NOT EXISTS 'ItemBans' ('ItemName' TEXT PRIMARY KEY);";
else if (TShock.Config.StorageType.ToLower() == "mysql")
com.CommandText =
"CREATE TABLE IF NOT EXISTS ItemBans (ItemName VARCHAR(255) PRIMARY);";
com.ExecuteNonQuery();
String file = Path.Combine(TShock.SavePath, "itembans.txt");
if (File.Exists(file))
using (StreamReader sr = new StreamReader(file))
{
using (StreamReader sr = new StreamReader(file))
String line;
while ((line = sr.ReadLine()) != null)
{
String line;
while ((line = sr.ReadLine()) != null)
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
{
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
{
if (TShock.Config.StorageType.ToLower() == "sqlite")
com.CommandText = "INSERT OR IGNORE INTO 'ItemBans' (ItemName) VALUES (@name);";
else if (TShock.Config.StorageType.ToLower() == "mysql")
com.CommandText = "INSERT IGNORE INTO ItemBans SET ItemName=@name;";
int id = 0;
int.TryParse(line, out id);
com.AddParameter("@name", Tools.GetItemById(id).name);
com.ExecuteNonQuery();
com.Parameters.Clear();
}
query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
"INSERT OR IGNORE INTO 'ItemBans' (ItemName) VALUES (@0);" :
"INSERT IGNORE INTO ItemBans SET ItemName=@0;";
int id = 0;
int.TryParse(line, out id);
database.Query(query, Tools.GetItemById(id).name);
}
}
String path = Path.Combine(TShock.SavePath, "old_configs");
String file2 = Path.Combine(path, "itembans.txt");
if (!Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
if (File.Exists(file2))
File.Delete(file2);
File.Move(file, file2);
}
String path = Path.Combine(TShock.SavePath, "old_configs");
String file2 = Path.Combine(path, "itembans.txt");
if (!Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
if (File.Exists(file2))
File.Delete(file2);
File.Move(file, file2);
}
UpdateItemBans();
@ -74,7 +68,7 @@ namespace TShockAPI.DB
using (var reader = com.ExecuteReader())
{
while (reader!=null&&reader.Read())
while (reader != null && reader.Read())
ItemBans.Add(reader.Get<string>("ItemName"));
}
}
@ -83,14 +77,9 @@ namespace TShockAPI.DB
{
try
{
using (var com = database.CreateCommand())
{
com.CommandText = "INSERT INTO ItemBans (ItemName) VALUES (@itemname);";
com.AddParameter("@itemname", Tools.GetItemByName(itemname)[0].name);
com.ExecuteNonQuery();
if( !ItemIsBanned( itemname ) )
ItemBans.Add(itemname);
}
database.Query("INSERT INTO ItemBans (ItemName) VALUES (@0);", Tools.GetItemByName(itemname)[0].name);
if (!ItemIsBanned(itemname))
ItemBans.Add(itemname);
}
catch (Exception ex)
{
@ -104,13 +93,8 @@ namespace TShockAPI.DB
return;
try
{
using (var com = database.CreateCommand())
{
com.CommandText = "Delete FROM 'ItemBans' WHERE ItemName=@itemname;";
com.AddParameter("@itemname", Tools.GetItemByName(itemname)[0].name);
com.ExecuteNonQuery();
ItemBans.Remove(itemname);
}
database.Query("Delete FROM 'ItemBans' WHERE ItemName=@0;", Tools.GetItemByName(itemname)[0].name);
ItemBans.Remove(itemname);
}
catch (Exception ex)
{