From 4575792987f031a36f7849aac7ea84154cebef95 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Sat, 9 Dec 2017 16:45:31 +0100 Subject: [PATCH] Formatted a bit better and moved SqlColumnException to SqlColumn.cs --- TShockAPI/DB/IQueryBuilder.cs | 60 ++++++++++++++--------------------- TShockAPI/DB/SqlColumn.cs | 16 ++++++++++ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/TShockAPI/DB/IQueryBuilder.cs b/TShockAPI/DB/IQueryBuilder.cs index 614b38bc..917f634d 100644 --- a/TShockAPI/DB/IQueryBuilder.cs +++ b/TShockAPI/DB/IQueryBuilder.cs @@ -16,12 +16,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; -using MySql.Data.MySqlClient; using TShockAPI.Extensions; namespace TShockAPI.DB @@ -37,6 +37,7 @@ namespace TShockAPI.DB /// The SqlTable to create the table from /// The sql query for the table creation. string CreateTable(SqlTable table); + /// /// Alter a table from source to destination /// @@ -44,6 +45,7 @@ namespace TShockAPI.DB /// Must have column names and column types. /// The SQL Query string AlterTable(SqlTable from, SqlTable to); + /// /// Converts the MySqlDbType enum to it's string representation. /// @@ -51,6 +53,7 @@ namespace TShockAPI.DB /// The length of the datatype /// The string representation string DbTypeToString(MySqlDbType type, int? length); + /// /// A UPDATE Query /// @@ -59,6 +62,7 @@ namespace TShockAPI.DB /// /// The SQL query string UpdateValue(string table, List values, List wheres); + /// /// A INSERT query /// @@ -66,6 +70,7 @@ namespace TShockAPI.DB /// /// The SQL Query string InsertValues(string table, List values); + /// /// A SELECT query to get all columns /// @@ -73,6 +78,7 @@ namespace TShockAPI.DB /// /// The SQL query string ReadColumn(string table, List wheres); + /// /// Deletes row(s). /// @@ -80,6 +86,7 @@ namespace TShockAPI.DB /// /// The SQL query string DeleteRow(string table, List wheres); + /// /// Renames the given table. /// @@ -101,18 +108,18 @@ namespace TShockAPI.DB /// The sql query for the table creation. public override string CreateTable(SqlTable table) { - SqlColumnErrorCheck(table.Columns); + ValidateSqlColumnType(table.Columns); var columns = table.Columns.Select( c => - "'{0}' {1} {2} {3} {4} {5}".SFormat(c.Name, - DbTypeToString(c.Type, c.Length), + "'{0}' {1} {2} {3} {4} {5}".SFormat(c.Name, + DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "", - c.AutoIncrement ? "AUTOINCREMENT" : "", + c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "", c.DefaultCurrentTimestamp ? "DEFAULT CURRENT_TIMESTAMP" : "")); var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name); - return "CREATE TABLE {0} ({1} {2})".SFormat(EscapeTableName(table.Name), + return "CREATE TABLE {0} ({1} {2})".SFormat(EscapeTableName(table.Name), string.Join(", ", columns), uniques.Count() > 0 ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) : ""); } @@ -140,7 +147,7 @@ namespace TShockAPI.DB { MySqlDbType.Double, "REAL" }, { MySqlDbType.Int32, "INTEGER" }, { MySqlDbType.Blob, "BLOB" }, - { MySqlDbType.Int64, "BIGINT"}, + { MySqlDbType.Int64, "BIGINT"}, { MySqlDbType.DateTime, "DATETIME"}, }; @@ -181,7 +188,7 @@ namespace TShockAPI.DB /// The sql query for the table creation. public override string CreateTable(SqlTable table) { - SqlColumnErrorCheck(table.Columns); + ValidateSqlColumnType(table.Columns); var columns = table.Columns.Select( c => @@ -219,7 +226,7 @@ namespace TShockAPI.DB { MySqlDbType.Float, "FLOAT" }, { MySqlDbType.Double, "DOUBLE" }, { MySqlDbType.Int32, "INT" }, - { MySqlDbType.Int64, "BIGINT"}, + { MySqlDbType.Int64, "BIGINT"}, { MySqlDbType.DateTime, "DATETIME"}, }; @@ -254,18 +261,21 @@ namespace TShockAPI.DB public abstract class GenericQueryCreator { protected static Random rand = new Random(); + /// /// Escapes the table name /// /// The name of the table to be escaped /// protected abstract string EscapeTableName(string table); + /// /// Creates a table from a SqlTable object. /// /// The SqlTable to create the table from /// The sql query for the table creation. public abstract string CreateTable(SqlTable table); + /// /// Renames the given table. /// @@ -299,14 +309,14 @@ namespace TShockAPI.DB /// Check for errors in the columns. /// /// - /// description - public void SqlColumnErrorCheck(List columns) + /// + public void ValidateSqlColumnType(List columns) { columns.ForEach(x => { - if(x.DefaultCurrentTimestamp && x.Type != MySqlDbType.DateTime) + if (x.DefaultCurrentTimestamp && x.Type != MySqlDbType.DateTime) { - throw new SqlColumnExcepcion("Can't set to true SqlColumn.DefaultCurrentTimestamp " + + throw new SqlColumnException("Can't set to true SqlColumn.DefaultCurrentTimestamp " + "when the MySqlDbType is not DateTime"); } }); @@ -389,28 +399,4 @@ namespace TShockAPI.DB return "WHERE {0}".SFormat(string.Join(", ", wheres.Select(v => v.Name + " = " + v.Value))); } } - - /// - /// An excepcion generated by the Column check. - /// - [Serializable] - public class SqlColumnExcepcion : Exception - { - public SqlColumnExcepcion() - { - } - - public SqlColumnExcepcion(string message) - : base(message) - { - } - - public SqlColumnExcepcion(string message, Exception innerException) : base(message, innerException) - { - } - - protected SqlColumnExcepcion(SerializationInfo info, StreamingContext context) : base(info, context) - { - } - } } diff --git a/TShockAPI/DB/SqlColumn.cs b/TShockAPI/DB/SqlColumn.cs index cef6b3d8..d61501d6 100644 --- a/TShockAPI/DB/SqlColumn.cs +++ b/TShockAPI/DB/SqlColumn.cs @@ -17,6 +17,7 @@ along with this program. If not, see . */ using MySql.Data.MySqlClient; +using System; namespace TShockAPI.DB { @@ -70,4 +71,19 @@ namespace TShockAPI.DB Length = length; } } + + /// + /// Used when a SqlColumn has validation errors. + /// + [Serializable] + public class SqlColumnException : Exception + { + /// + /// Creates a new SqlColumnException with the given message. + /// + /// + public SqlColumnException(string message) : base(message) + { + } + } }