Added Random.NextString
Rename IQueryCreator to IQueryBuilder
This commit is contained in:
parent
9d4e2d6d9b
commit
3e045d51bf
5 changed files with 82 additions and 31 deletions
47
TShockAPI/DB/IQueryBuilder.cs
Normal file
47
TShockAPI/DB/IQueryBuilder.cs
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
31
TShockAPI/Extensions/RandomExt.cs
Normal file
31
TShockAPI/Extensions/RandomExt.cs
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -94,13 +94,14 @@
|
|||
<Compile Include="BackupManager.cs" />
|
||||
<Compile Include="DB\BanManager.cs" />
|
||||
<Compile Include="DBTools.cs" />
|
||||
<Compile Include="DB\IQueryCreator.cs" />
|
||||
<Compile Include="DB\IQueryBuilder.cs" />
|
||||
<Compile Include="DB\ItemManager.cs" />
|
||||
<Compile Include="DB\SqlColumn.cs" />
|
||||
<Compile Include="DB\SqlTable.cs" />
|
||||
<Compile Include="Extensions\DbExt.cs" />
|
||||
<Compile Include="DB\GroupManager.cs" />
|
||||
<Compile Include="DB\UserManager.cs" />
|
||||
<Compile Include="Extensions\RandomExt.cs" />
|
||||
<Compile Include="Extensions\StringExt.cs" />
|
||||
<Compile Include="IPackable.cs" />
|
||||
<Compile Include="Commands.cs" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue