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
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