diff --git a/TShockAPI/DB/DBTools.cs b/TShockAPI/DBTools.cs similarity index 65% rename from TShockAPI/DB/DBTools.cs rename to TShockAPI/DBTools.cs index 4cec3753..e92d7f32 100644 --- a/TShockAPI/DB/DBTools.cs +++ b/TShockAPI/DBTools.cs @@ -5,13 +5,19 @@ using System.Linq; using System.Text; using TShockAPI.DB; -namespace TShockAPI.DB +namespace TShockAPI { public class DBTools { - public static IDbConnection database; + internal static IDbConnection database; - public static void CreateTable(string name, bool IfNotExists, List columns) + /// + /// Creates a Table, within the current open DB + /// + /// Name of the Table + /// The list of columns that the Table will have + /// Only try create Table if it does not exist + public static void CreateTable(string name, List columns, bool IfNotExists =true) { //Build up Creation string :) StringBuilder sb = new StringBuilder(); @@ -57,11 +63,17 @@ namespace TShockAPI.DB using (var com = database.CreateCommand()) { com.CommandText = sb.ToString(); - com.ExecuteNonQuery(); } } - public static void InsertTable(string tablename, bool Ignore, List Values, List WhereAndStatements) + /// + /// Inserts a list of values into a Table, if conditions are met + /// + /// Name of the Table + /// Ignore insert if feild is unique and there is already a exact entry + /// The list of values to enter into the table + /// The list of where statements that must be met, can be an empty list + public static int InsertTable(string tablename, bool Ignore, List Values, List WhereStatements) { StringBuilder sb = new StringBuilder(); sb.Append("INSERT "); @@ -135,12 +147,12 @@ namespace TShockAPI.DB } //Where Statement (if any) - if (WhereAndStatements.Count > 0) + if (WhereStatements.Count > 0) { sb.Append("WHERE "); int count = 0; - foreach (ColumnData columnname in WhereAndStatements) + foreach (ColumnData columnname in WhereStatements) { count++; if (Values.Count != count) @@ -157,10 +169,18 @@ namespace TShockAPI.DB } com.CommandText = sb.ToString(); - com.ExecuteNonQuery(); + + using (var reader = com.ExecuteReader()) + return reader.RecordsAffected; } } + /// + /// Returns a list of values from a given Table, where conditions are met + /// + /// Name of the Table + /// The name of the column you are getting the values from + /// The list of where statements that must be met, can be an empty list public static List ReadTable(string tablename, string getcolumn, List WhereStatements) { StringBuilder sb = new StringBuilder(); @@ -198,9 +218,50 @@ namespace TShockAPI.DB ReturnedValues.Add(reader.Get(getcolumn)); } } - return ReturnedValues; } + + /// + /// Sets values in a Table, where statements are met + /// + /// Name of the Table + /// The column data you are setting + /// The list of where statements that must be met, can be an empty list + public static int SetTable(string tablename, ColumnData setcolumn, List WhereStatements) + { + StringBuilder sb = new StringBuilder(); + + sb.Append("UPDATE " + tablename + " SET " + setcolumn.Name + "=@setcolumn "); + + using (var com = database.CreateCommand()) + { + //Where Statement (if any) + if (WhereStatements.Count > 0) + { + sb.Append("WHERE "); + int count = 0; + + foreach (ColumnData columnname in WhereStatements) + { + count++; + if (WhereStatements.Count != count) + { + sb.Append(columnname.Name + " =" + columnname.Value + " AND "); + } + else + { + sb.Append(columnname.Name + " =" + columnname.Value); + } + } + } + + com.CommandText = sb.ToString(); + com.AddParameter("@setcolumn", setcolumn.Value); + + using (var reader = com.ExecuteReader()) + return reader.RecordsAffected; + } + } } public class Column @@ -210,7 +271,14 @@ namespace TShockAPI.DB public bool Unique { get; set; } public string Parameters { get; set; } - public Column(string name, bool unique, string type, string parameters) + /// + /// The class for creating a new column type + /// + /// Name of the column + /// Whether there can be more than one exact value in the column + /// The type of column, currently the api only supports "string" or "int" + /// Extra SQL parameters given, can cause errors cross different SQL (SQLite and MySql) + public Column(string name, bool unique, string type, string parameters = "") { Name = name; Type = type; @@ -232,6 +300,11 @@ namespace TShockAPI.DB public string Name { get; set; } public object Value { get; set; } + /// + /// The class for testing, inserting or setting column data + /// + /// Column Name + /// Column Value public ColumnData(string name, object value) { Name = name; diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index fcb8d801..4ca142f1 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -20,13 +20,13 @@ using System.IO; namespace TShockAPI { - internal class FileTools + public class FileTools { - public static string RulesPath { get { return Path.Combine(TShock.SavePath, "rules.txt"); } } - public static string MotdPath { get { return Path.Combine(TShock.SavePath, "motd.txt"); } } - public static string WhitelistPath { get { return Path.Combine(TShock.SavePath, "whitelist.txt"); } } - public static string RememberedPosPath { get { return Path.Combine(TShock.SavePath, "oldpos.xml"); } } - public static string ConfigPath { get { return Path.Combine(TShock.SavePath, "config.json"); } } + internal static string RulesPath { get { return Path.Combine(TShock.SavePath, "rules.txt"); } } + internal static string MotdPath { get { return Path.Combine(TShock.SavePath, "motd.txt"); } } + internal static string WhitelistPath { get { return Path.Combine(TShock.SavePath, "whitelist.txt"); } } + internal static string RememberedPosPath { get { return Path.Combine(TShock.SavePath, "oldpos.xml"); } } + internal static string ConfigPath { get { return Path.Combine(TShock.SavePath, "config.json"); } } public static void CreateFile(string file) { diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 10c54c58..3f1f90d7 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -93,7 +93,7 @@ - + diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index 985ffe8b..454788e3 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -28,7 +28,7 @@ using Terraria; namespace TShockAPI { - internal class Tools + public class Tools { public static Random Random = new Random(); //private static List groups = new List();