diff --git a/TShockAPI/DB/IQueryBuilder.cs b/TShockAPI/DB/IQueryBuilder.cs new file mode 100644 index 00000000..fa7c28d2 --- /dev/null +++ b/TShockAPI/DB/IQueryBuilder.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TShockAPI.Extensions; + +namespace TShockAPI.DB +{ + public interface IQueryBuilder + { + string CreateTable(SqlTable table); + string AlterTable(SqlTable from, SqlTable to); + } + + public class SqliteQueryCreator : IQueryBuilder + { + public string CreateTable(SqlTable table) + { + var columns = table.Columns.Select(c => "'{0}' {1} {2} {3} {4}".SFormat(c.Name, c.Type, c.Primary ? "PRIMARY KEY" : "", c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "", c.Unique ? "UNIQUE" : "")); + return "CREATE TABLE '{0}' ({1})".SFormat(table.Name, string.Join(", ", columns)); + } + static Random rand = new Random(); + public string AlterTable(SqlTable from, SqlTable to) + { + return ""; + /* + ALTER TABLE "main"."Bans" RENAME TO "oXHFcGcd04oXHFcGcd04_Bans" + 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" + */ + } + } + public class MysqlQueryCreator : IQueryBuilder + { + public string CreateTable(SqlTable table) + { + throw new NotImplementedException(); + } + + + public string AlterTable(SqlTable from, SqlTable to) + { + throw new NotImplementedException(); + } + } +} diff --git a/TShockAPI/DB/IQueryCreator.cs b/TShockAPI/DB/IQueryCreator.cs deleted file mode 100644 index d9e58512..00000000 --- a/TShockAPI/DB/IQueryCreator.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace TShockAPI.DB -{ - public interface IQueryCreator - { - string CreateTable(SqlTable table); - } - - public class SqliteQueryCreator : IQueryCreator - { - public string CreateTable(SqlTable table) - { - var columns = table.Columns.Select(c => "'{0}' {1} {2} {3} {4}".SFormat(c.Name, c.Type, c.Primary ? "PRIMARY KEY" : "", c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "", c.Unique ? "UNIQUE" : "")); - return "CREATE TABLE '{0}' ({1})".SFormat(table.Name, string.Join(", ", columns)); - } - } - public class MysqlQueryCreator : IQueryCreator - { - public string CreateTable(SqlTable table) - { - throw new NotImplementedException(); - } - } -} diff --git a/TShockAPI/DB/SqlTable.cs b/TShockAPI/DB/SqlTable.cs index 272cd8f2..e2910c9d 100644 --- a/TShockAPI/DB/SqlTable.cs +++ b/TShockAPI/DB/SqlTable.cs @@ -24,8 +24,8 @@ namespace TShockAPI.DB public class SqlTableCreator { IDbConnection database; - IQueryCreator creator; - public SqlTableCreator(IDbConnection db, IQueryCreator provider) + IQueryBuilder creator; + public SqlTableCreator(IDbConnection db, IQueryBuilder provider) { database = db; creator = provider; diff --git a/TShockAPI/Extensions/RandomExt.cs b/TShockAPI/Extensions/RandomExt.cs new file mode 100644 index 00000000..e49b50f0 --- /dev/null +++ b/TShockAPI/Extensions/RandomExt.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TShockAPI.Extensions +{ + public static class RandomExt + { + public static string NextString(this Random rand, int length) + { + var sb = new StringBuilder(); + for (int i = 0; i < length; i++) + { + switch (rand.Next(0, 3)) + { + case 0: + sb.Append((char)rand.Next('a', 'z' + 1)); + break; + case 1: + sb.Append((char)rand.Next('A', 'Z' + 1)); + break; + case 2: + sb.Append((char)rand.Next('0', '9' + 1)); + break; + } + } + return sb.ToString(); + } + } +} diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index d60839b6..476636cd 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -94,13 +94,14 @@ - + +