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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue