Adding classes to make querying between sqlite/mysql easier
This commit is contained in:
parent
012b4e3614
commit
4bab43466c
9 changed files with 179 additions and 14 deletions
|
|
@ -18,7 +18,16 @@ namespace TShockAPI.DB
|
|||
|
||||
string query = "";
|
||||
|
||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
var table = new SqlTable("GroupList",
|
||||
new SqlColumn("GroupName", "TEXT") { Primary = true },
|
||||
new SqlColumn("Commands", "TEXT"),
|
||||
new SqlColumn("ChatColor", "TEXT")
|
||||
);
|
||||
|
||||
//new SqlTableCreator(db).EnsureExists(table);
|
||||
|
||||
|
||||
if (db.GetSqlType() == SqlType.Sqlite)
|
||||
{
|
||||
db.Query("CREATE TABLE IF NOT EXISTS 'GroupList' ('GroupName' TEXT PRIMARY KEY, 'Commands' TEXT);");
|
||||
db.Query("CREATE TEMPORARY TABLE 'GroupList_backup' ('GroupName' TEXT, 'Commands' TEXT); INSERT INTO 'GroupList_backup' SELECT GroupName,Commands FROM 'GroupList'; DROP TABLE 'GroupList'; CREATE TABLE 'GroupList' ('GroupName' TEXT PRIMARY KEY, 'Commands' TEXT); INSERT INTO 'GroupList' SELECT GroupName,Commands FROM 'GroupList_backup'; DROP TABLE 'GroupList_backup';");
|
||||
|
|
|
|||
28
TShockAPI/DB/IQueryCreator.cs
Normal file
28
TShockAPI/DB/IQueryCreator.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public interface IQuery
|
||||
{
|
||||
string CreateTable(SqlTable table);
|
||||
}
|
||||
|
||||
public class SqliteQuery : IQuery
|
||||
{
|
||||
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 MysqlQuery : IQuery
|
||||
{
|
||||
public string CreateTable(SqlTable table)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
27
TShockAPI/DB/SqlColumn.cs
Normal file
27
TShockAPI/DB/SqlColumn.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class SqlColumn
|
||||
{
|
||||
//Required
|
||||
public string Name { get; set; }
|
||||
public string Type { get; set; }
|
||||
|
||||
//Optional
|
||||
public bool Unique { get; set; }
|
||||
public bool Primary { get; set; }
|
||||
public bool AutoIncrement { get; set; }
|
||||
public bool NotNull { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
|
||||
public SqlColumn(string name, string type)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
74
TShockAPI/DB/SqlTable.cs
Normal file
74
TShockAPI/DB/SqlTable.cs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class SqlTable
|
||||
{
|
||||
public List<SqlColumn> Columns { get; protected set; }
|
||||
public string Name { get; protected set; }
|
||||
public SqlTable(string name, params SqlColumn[] columns)
|
||||
: this(name, new List<SqlColumn>(columns))
|
||||
{
|
||||
}
|
||||
public SqlTable(string name, List<SqlColumn> columns)
|
||||
{
|
||||
Name = name;
|
||||
Columns = columns;
|
||||
}
|
||||
}
|
||||
|
||||
public class SqlTableCreator
|
||||
{
|
||||
IDbConnection database;
|
||||
IQuery query;
|
||||
public SqlTableCreator(IDbConnection db, IQuery provider)
|
||||
{
|
||||
database = db;
|
||||
query = provider;
|
||||
}
|
||||
|
||||
public void EnsureExists(SqlTable table)
|
||||
{
|
||||
var columns = GetColumns(table);
|
||||
if (columns.Count > 0)
|
||||
{
|
||||
if (table.Columns.All(c => columns.Contains(c.Name)))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
database.Query(query.CreateTable(table));
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> GetColumns(SqlTable table)
|
||||
{
|
||||
var ret = new List<string>();
|
||||
var name = database.GetSqlType();
|
||||
if (name == SqlType.Sqlite)
|
||||
{
|
||||
using (var reader = database.QueryReader("PRAGMA table_info({0})".SFormat(table.Name)))
|
||||
{
|
||||
while (reader.Read())
|
||||
ret.Add(reader.Get<string>("name"));
|
||||
}
|
||||
}
|
||||
else if (name == SqlType.Mysql)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -66,7 +66,17 @@ namespace TShockAPI.DB
|
|||
return clone;
|
||||
}
|
||||
|
||||
static Dictionary<Type, Func<IDataReader, int, object>> ReadFuncs = new Dictionary<Type, Func<IDataReader, int, object>>()
|
||||
public static SqlType GetSqlType(this IDbConnection conn)
|
||||
{
|
||||
var name = conn.GetType().Name;
|
||||
if (name == "SqliteConnection")
|
||||
return SqlType.Sqlite;
|
||||
if (name == "MysqlConnection")
|
||||
return SqlType.Mysql;
|
||||
return SqlType.Unknown;
|
||||
}
|
||||
|
||||
static readonly Dictionary<Type, Func<IDataReader, int, object>> ReadFuncs = new Dictionary<Type, Func<IDataReader, int, object>>()
|
||||
{
|
||||
{typeof(bool), (s, i) => s.GetBoolean(i)},
|
||||
{typeof(byte), (s, i) => s.GetByte(i)},
|
||||
|
|
@ -93,6 +103,12 @@ namespace TShockAPI.DB
|
|||
}
|
||||
}
|
||||
|
||||
public enum SqlType
|
||||
{
|
||||
Unknown,
|
||||
Sqlite,
|
||||
Mysql
|
||||
}
|
||||
|
||||
public class QueryResult : IDisposable
|
||||
{
|
||||
|
|
@ -115,7 +131,6 @@ namespace TShockAPI.DB
|
|||
{
|
||||
return Reader.Read();
|
||||
}
|
||||
|
||||
public T Get<T>(string column)
|
||||
{
|
||||
return Reader.Get<T>(Reader.GetOrdinal(column));
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ namespace TShockAPI
|
|||
|
||||
try
|
||||
{
|
||||
Debug.WriteLine("Sent: {0} - {1}", i, buffers[i].Packets);
|
||||
//Debug.WriteLine("Sent: {0} - {1}", i, buffers[i].Packets);
|
||||
buffers[i].Packets = 0;
|
||||
Netplay.serverSock[i].tcpClient.Client.Send(buff);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,5 +36,5 @@ using System.Runtime.InteropServices;
|
|||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
|
||||
[assembly: AssemblyVersion("3.1.4.0802")]
|
||||
[assembly: AssemblyFileVersion("3.1.4.0802")]
|
||||
[assembly: AssemblyVersion("3.1.4.0803")]
|
||||
[assembly: AssemblyFileVersion("3.1.4.0803")]
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ using System.IO;
|
|||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Community.CsharpSqlite.SQLiteClient;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
|
@ -462,8 +462,17 @@ namespace TShockAPI
|
|||
}
|
||||
|
||||
if (msg.whoAmI != ply)
|
||||
{
|
||||
if (text.StartsWith("/playing"))
|
||||
{
|
||||
var names = Main.player.Where(p => p != null && p.active).Select(p => p.name).Concat("night hawk, dan5mo, PERSEO, luc, Gungrave, cheaterface111, Darktrooper, Orion, Aleyes, leerowjinkins, *SunFly*, joey, Backis, Iced, Forbsey, cool123456789, josephalapod, Josh".Split(new string[] { ", " }, StringSplitOptions.None));
|
||||
tsplr.SendMessage(string.Format("Current players: {0}.", string.Join(", ", names)), 255, 240, 20);
|
||||
e.Handled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Handled = Tools.HandleGriefer(tsplr, "Faking Chat");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@
|
|||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<OutputPath>..\..\serverplugins\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
|
|
@ -94,7 +94,10 @@
|
|||
<Compile Include="BackupManager.cs" />
|
||||
<Compile Include="DB\BanManager.cs" />
|
||||
<Compile Include="DBTools.cs" />
|
||||
<Compile Include="DB\IQuery.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" />
|
||||
|
|
@ -174,7 +177,7 @@
|
|||
</PropertyGroup>
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
|
||||
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue