From 2c4625e42678be3f4a1915aa4a1bfa9ad0f92d92 Mon Sep 17 00:00:00 2001 From: Twitchy Date: Thu, 4 Aug 2011 21:53:02 +1200 Subject: [PATCH] Adds SQL Queries for Update, Read and Insert --- TShockAPI/DB/IQueryBuilder.cs | 141 ++++++++++++++++++++++++++++++++++ TShockAPI/DB/SqlValue.cs | 53 +++++++++++++ TShockAPI/Extensions/DbExt.cs | 1 + TShockAPI/TShockAPI.csproj | 3 +- 4 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 TShockAPI/DB/SqlValue.cs diff --git a/TShockAPI/DB/IQueryBuilder.cs b/TShockAPI/DB/IQueryBuilder.cs index 8eede642..9967797e 100644 --- a/TShockAPI/DB/IQueryBuilder.cs +++ b/TShockAPI/DB/IQueryBuilder.cs @@ -13,6 +13,9 @@ namespace TShockAPI.DB string CreateTable(SqlTable table); string AlterTable(SqlTable from, SqlTable to); string DbTypeToString(MySqlDbType type, int? length); + string UpdateValue(string table, List values, List wheres); + string InsertValues(string table, List values); + string ReadColumn(string table, List wheres); } public class SqliteQueryCreator : IQueryBuilder @@ -44,9 +47,79 @@ namespace TShockAPI.DB CREATE TABLE "main"."Bans" ("IP" TEXT PRIMARY KEY ,"Name" TEXT) INSERT INTO "main"."Bans" SELECT "IP","Name" FROM "main"."oXHFcGcd04oXHFcGcd04_Bans" DROP TABLE "main"."oXHFcGcd04oXHFcGcd04_Bans" + * + * Twitchy - Oh. I get it! */ } + public string UpdateValue(string table, List values, List wheres) + { + var sbvalues = new StringBuilder(); + var sbwheres = new StringBuilder(); + int count = 0; + foreach (SqlValue value in values) + { + sbvalues.Append(value.Name + "=" + value.Value.ToString()); + if (count != values.Count - 1) + sbvalues.Append(","); + count++; + } + count = 0; + foreach (SqlValue where in wheres) + { + sbwheres.Append(where.Name + "=" + where.Value.ToString()); + if (count != wheres.Count - 1) + sbvalues.Append(" AND "); + count++; + } + if (wheres.Count > 0) + return "UPDATE '{0}' SET {1} WHERE {2}".SFormat(table, sbvalues.ToString(), sbwheres.ToString()); + else + return "UPDATE '{0}' SET {1}".SFormat(table, sbvalues.ToString()); + } + public string InsertValues(string table, List values) + { + var sbnames = new StringBuilder(); + var sbvalues = new StringBuilder(); + int count = 0; + + foreach (SqlValue name in values) + { + sbnames.Append(name.Name); + + if (count != values.Count - 1) + sbnames.Append(", "); + count++; + } + count = 0; + foreach (SqlValue value in values) + { + sbvalues.Append(value.Value.ToString()); + if (count != values.Count - 1) + sbvalues.Append(", "); + count++; + } + + return "INSERT INTO '{0}' ({1}) VALUES ({2})".SFormat(table, sbnames.ToString(), sbvalues.ToString()); + } + public string ReadColumn(string table, List wheres) + { + var sbwheres = new StringBuilder(); + int count = 0; + + foreach (SqlValue where in wheres) + { + sbwheres.Append(where.Name + "=" + where.Value.ToString()); + if (count != wheres.Count - 1) + sbwheres.Append(" AND "); + count++; + } + + if(wheres.Count > 0) + return "SELECT * FROM {0} WHERE {1}".SFormat(table, sbwheres.ToString()); + else + return "SELECT * FROM {0}".SFormat(table); + } static readonly Dictionary TypesAsStrings = new Dictionary { @@ -92,7 +165,75 @@ namespace TShockAPI.DB var drop = "DROP TABLE {0}_{1}".SFormat(rstr, from.Name); return "{0}; {1}; {2}; {3};".SFormat(alter, create, insert, drop); } + public string UpdateValue(string table, List values, List wheres) + { + var sbvalues = new StringBuilder(); + var sbwheres = new StringBuilder(); + int count = 0; + foreach (SqlValue value in values) + { + sbvalues.Append(value.Name + "=" + value.Value.ToString()); + if (count != values.Count - 1) + sbvalues.Append("AND"); + count++; + } + count = 0; + foreach (SqlValue where in wheres) + { + sbwheres.Append(where.Name + "=" + where.Value.ToString()); + if (count != wheres.Count - 1) + sbvalues.Append(" AND "); + count++; + } + if (wheres.Count > 0) + return "UPDATE {0} SET {1} WHERE {2}".SFormat(table, sbvalues.ToString(), sbwheres.ToString()); + else + return "UPDATE {0} SET {1}".SFormat(table, sbvalues.ToString()); + } + public string InsertValues(string table, List values) + { + var sbnames = new StringBuilder(); + var sbvalues = new StringBuilder(); + int count = 0; + + foreach (SqlValue name in values) + { + sbnames.Append(name.Name); + + if (count != values.Count - 1) + sbnames.Append(", "); + count++; + } + count = 0; + foreach (SqlValue value in values) + { + sbvalues.Append(value.Value.ToString()); + if (count != values.Count - 1) + sbvalues.Append(", "); + count++; + } + + return "INSERT INTO {0} ({1}) VALUES ({2})".SFormat(table, sbnames.ToString(), sbvalues.ToString()); + } + public string ReadColumn(string table, List wheres) + { + var sbwheres = new StringBuilder(); + int count = 0; + + foreach (SqlValue where in wheres) + { + sbwheres.Append(where.Name + "=" + where.Value.ToString()); + if (count != wheres.Count - 1) + sbwheres.Append(" AND "); + count++; + } + + if (wheres.Count > 0) + return "SELECT * FROM {0} WHERE {1}".SFormat(table, sbwheres.ToString()); + else + return "SELECT * FROM {0}".SFormat(table); + } static readonly Dictionary TypesAsStrings = new Dictionary { diff --git a/TShockAPI/DB/SqlValue.cs b/TShockAPI/DB/SqlValue.cs new file mode 100644 index 00000000..f74d3149 --- /dev/null +++ b/TShockAPI/DB/SqlValue.cs @@ -0,0 +1,53 @@ +using System.Data; +using MySql.Data.MySqlClient; +using System.Collections.Generic; + +namespace TShockAPI.DB +{ + public class SqlValue + { + public string Name { get; set; } + public object Value { get; set; } + + public SqlValue(string name, object value) + { + Name = name; + Value = value; + } + } + + public class SqlTableEditor + { + IDbConnection database; + IQueryBuilder creator; + + public SqlTableEditor(IDbConnection db, IQueryBuilder provider) + { + database = db; + creator = provider; + } + + public void UpdateValues(string table, List values, List wheres) + { + database.Query(creator.UpdateValue(table, values, wheres)); + } + + public void InsertValues(string table, List values) + { + database.Query(creator.InsertValues(table, values)); + } + + public List ReadColumn(string table, string column, List wheres) + { + List values = new List(); + + using (var reader = database.QueryReader(creator.ReadColumn(table, wheres))) + { + while (reader.Read()) + values.Add(reader.Reader.Get(column)); + } + + return values; + } + } +} diff --git a/TShockAPI/Extensions/DbExt.cs b/TShockAPI/Extensions/DbExt.cs index 25429577..e33459a7 100644 --- a/TShockAPI/Extensions/DbExt.cs +++ b/TShockAPI/Extensions/DbExt.cs @@ -87,6 +87,7 @@ namespace TShockAPI.DB {typeof(decimal), (s, i) => s.GetDecimal(i)}, {typeof(float), (s, i) => s.GetFloat(i)}, {typeof(double), (s, i) => s.GetDouble(i)}, + {typeof(object), (s, i) => s.GetValue(i)}, }; public static T Get(this IDataReader reader, string column) diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 476636cd..640654d5 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -98,6 +98,7 @@ + @@ -178,7 +179,7 @@ - +