Less hardcoding of MySQL / SQLite stuff

This commit is contained in:
UnknownShadow200 2017-05-10 18:54:47 +10:00
parent 29441bd5b9
commit e31836f218
4 changed files with 22 additions and 3 deletions

View File

@ -35,6 +35,12 @@ namespace MCGalaxy.SQL {
Server.MySQLUsername, Server.MySQLPassword, Server.DatabasePooling); } Server.MySQLUsername, Server.MySQLPassword, Server.DatabasePooling); }
} }
public override bool EnforcesTextLength { get { return true; } } public override bool EnforcesTextLength { get { return true; } }
public MySQLBackend() {
CaselessWhereSuffix = " COLLATE utf8_general_ci";
CaselessLikeSuffix = "";
}
public override void CreateDatabase() { public override void CreateDatabase() {
ParameterisedQuery query = GetStaticParameterised(); ParameterisedQuery query = GetStaticParameterised();

View File

@ -35,6 +35,12 @@ namespace MCGalaxy.SQL {
} }
public override bool EnforcesTextLength { get { return false; } } public override bool EnforcesTextLength { get { return false; } }
public SQLiteBackend() {
CaselessWhereSuffix = " COLLATE NOCASE";
CaselessLikeSuffix = " COLLATE NOCASE";
}
public override void CreateDatabase() { } public override void CreateDatabase() { }
public override BulkTransaction CreateBulk() { public override BulkTransaction CreateBulk() {

View File

@ -32,6 +32,13 @@ namespace MCGalaxy.SQL {
/// <summary> Whether this backend enforces the character length in VARCHAR columns. </summary> /// <summary> Whether this backend enforces the character length in VARCHAR columns. </summary>
public abstract bool EnforcesTextLength { get; } public abstract bool EnforcesTextLength { get; }
/// <summary> Suffix required after a WHERE clause for caseless string comparison. </summary>
public string CaselessWhereSuffix { get; protected set; }
/// <summary> Suffix required after a LIKE clause for caseless string comparison. </summary>
public string CaselessLikeSuffix { get; protected set; }
/// <summary> Creates the schema for this database (if required). </summary> /// <summary> Creates the schema for this database (if required). </summary>
public abstract void CreateDatabase(); public abstract void CreateDatabase();

View File

@ -188,13 +188,13 @@ namespace MCGalaxy {
internal static DataTable Query(string name, string columns) { internal static DataTable Query(string name, string columns) {
string suffix = Server.useMySQL ? " utf8_general_ci" : " NOCASE"; string suffix = Database.Backend.CaselessWhereSuffix;
return Database.Backend.GetRows("Players", columns, return Database.Backend.GetRows("Players", columns,
"WHERE Name=@0 COLLATE" + suffix, name); "WHERE Name=@0" + suffix, name);
} }
internal static DataRow QueryMulti(Player p, string name, string columns) { internal static DataRow QueryMulti(Player p, string name, string columns) {
string suffix = Server.useMySQL ? "" : " COLLATE NOCASE"; string suffix = Database.Backend.CaselessLikeSuffix;
using (DataTable results = Database.Backend.GetRows("Players", columns, using (DataTable results = Database.Backend.GetRows("Players", columns,
"WHERE Name LIKE @0 LIMIT 21" + suffix, "WHERE Name LIKE @0 LIMIT 21" + suffix,
"%" + name + "%")) { "%" + name + "%")) {