mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-21 19:42:37 -04:00
few less allocations for DB functions
This commit is contained in:
parent
35a46ea6bc
commit
b7f3c4523b
@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.Chatting {
|
||||
if (args.Length == 1) {
|
||||
Player.Message(p, "You need to provide either \"all\" or a number."); return;
|
||||
} else if (args[1].CaselessEq("all")) {
|
||||
Database.Backend.ClearTable("Inbox" + p.name);
|
||||
Database.Backend.DeleteRows("Inbox" + p.name);
|
||||
Player.Message(p, "Deleted all messages.");
|
||||
} else {
|
||||
DeleteByID(p, args[1], entries);
|
||||
@ -81,7 +81,8 @@ namespace MCGalaxy.Commands.Chatting {
|
||||
}
|
||||
|
||||
static void Output(Player p, string[] entry) {
|
||||
TimeSpan delta = DateTime.Now - DateTime.Parse(entry[i_sent]);
|
||||
DateTime time = DateTime.ParseExact(entry[i_sent], Database.DateFormat, null);
|
||||
TimeSpan delta = DateTime.Now - time;
|
||||
string sender = PlayerInfo.GetColoredName(p, entry[i_from]);
|
||||
|
||||
Player.Message(p, "From {0} &a{1} ago:", sender, delta.Shorten());
|
||||
|
@ -86,7 +86,7 @@ namespace MCGalaxy.Commands.Info {
|
||||
entry.OldRaw = Block.Invalid;
|
||||
|
||||
foreach (string[] row in entries) {
|
||||
DateTime time = DateTime.Parse(row[1]);
|
||||
DateTime time = DateTime.ParseExact(row[1], Database.DateFormat, null);
|
||||
TimeSpan delta = time - BlockDB.Epoch;
|
||||
entry.TimeDelta = (int)delta.TotalSeconds;
|
||||
entry.Flags = BlockDBFlags.ManualPlace;
|
||||
|
@ -29,7 +29,7 @@ namespace MCGalaxy.Commands.Info {
|
||||
public override bool UseableWhenFrozen { get { return true; } }
|
||||
|
||||
public override void Use(Player p, string message) {
|
||||
string end = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
string end = DateTime.Now.ToString(Database.DateFormat);
|
||||
string start = "thismonth", name = null;
|
||||
string[] args = message.SplitSpaces();
|
||||
|
||||
|
@ -129,13 +129,13 @@ namespace MCGalaxy.Commands.Maintenance {
|
||||
|
||||
static void SetDate(Player p, string[] args, string column, Player who, Action<DateTime> setter) {
|
||||
if (args.Length < 3) {
|
||||
Player.Message(p, "Dates must be in the format: yyyy-mm-dd hh:mm:ss");
|
||||
Player.Message(p, "Dates must be in the format: " + Database.DateFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
DateTime date;
|
||||
if (!DateTime.TryParseExact(args[2], Database.DateFormat, null, 0, out date)) {
|
||||
Player.Message(p, "Invalid date. (must be in format: yyyy-mm-dd hh:mm:ss");
|
||||
Player.Message(p, "Invalid date. It must be in format: " + Database.DateFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,8 @@ namespace MCGalaxy.SQL {
|
||||
|
||||
|
||||
public override void CreateDatabase() {
|
||||
string syntax = "CREATE DATABASE if not exists `" + ServerConfig.MySQLDatabaseName + "`";
|
||||
Database.Do(syntax, true, null, null);
|
||||
string sql = "CREATE DATABASE if not exists `" + ServerConfig.MySQLDatabaseName + "`";
|
||||
Database.Do(sql, true, null, null);
|
||||
}
|
||||
|
||||
public override BulkTransaction CreateBulk() {
|
||||
@ -106,14 +106,8 @@ namespace MCGalaxy.SQL {
|
||||
public override void RenameTable(string srcTable, string dstTable) {
|
||||
ValidateTable(srcTable);
|
||||
ValidateTable(dstTable);
|
||||
string syntax = "RENAME TABLE `" + srcTable + "` TO `" + dstTable + "`";
|
||||
Database.Execute(syntax);
|
||||
}
|
||||
|
||||
public override void ClearTable(string table) {
|
||||
ValidateTable(table);
|
||||
string syntax = "TRUNCATE TABLE `" + table + "`";
|
||||
Database.Execute(syntax);
|
||||
string sql = "RENAME TABLE `" + srcTable + "` TO `" + dstTable + "`";
|
||||
Database.Execute(sql, null);
|
||||
}
|
||||
|
||||
protected override void CreateTableColumns(StringBuilder sql, ColumnDesc[] columns) {
|
||||
@ -161,10 +155,9 @@ namespace MCGalaxy.SQL {
|
||||
|
||||
public override void AddColumn(string table, ColumnDesc col, string colAfter) {
|
||||
ValidateTable(table);
|
||||
string syntax = "ALTER TABLE `" + table + "` ADD COLUMN "
|
||||
+ col.Column + " " + col.FormatType();
|
||||
if (colAfter.Length > 0) syntax += " AFTER " + colAfter;
|
||||
Database.Execute(syntax);
|
||||
string sql = "ALTER TABLE `" + table + "` ADD COLUMN " + col.Column + " " + col.FormatType();
|
||||
if (colAfter.Length > 0) sql += " AFTER " + colAfter;
|
||||
Database.Execute(sql, null);
|
||||
}
|
||||
|
||||
public override void AddOrReplaceRow(string table, string columns, params object[] args) {
|
||||
|
@ -67,8 +67,8 @@ namespace MCGalaxy.SQL {
|
||||
}
|
||||
|
||||
public override List<string> AllTables() {
|
||||
const string syntax = "SELECT name from sqlite_master WHERE type='table'";
|
||||
List<string> tables = Database.GetStrings(syntax);
|
||||
const string sql = "SELECT name from sqlite_master WHERE type='table'";
|
||||
List<string> tables = Database.GetStrings(sql);
|
||||
|
||||
// exclude sqlite built-in database tables
|
||||
for (int i = tables.Count - 1; i >= 0; i--) {
|
||||
@ -88,21 +88,15 @@ namespace MCGalaxy.SQL {
|
||||
List<string> columns = new List<string>();
|
||||
|
||||
Database.Iterate("PRAGMA table_info(`" + table + "`)",
|
||||
columns, IterateColumnNames);
|
||||
columns, IterateColumnNames, null);
|
||||
return columns;
|
||||
}
|
||||
|
||||
public override void RenameTable(string srcTable, string dstTable) {
|
||||
ValidateTable(srcTable);
|
||||
ValidateTable(dstTable);
|
||||
string syntax = "ALTER TABLE `" + srcTable + "` RENAME TO `" + dstTable + "`";
|
||||
Database.Execute(syntax);
|
||||
}
|
||||
|
||||
public override void ClearTable(string table) {
|
||||
ValidateTable(table);
|
||||
string syntax = "DELETE FROM `" + table + "`";
|
||||
Database.Execute(syntax);
|
||||
string sql = "ALTER TABLE `" + srcTable + "` RENAME TO `" + dstTable + "`";
|
||||
Database.Execute(sql, null);
|
||||
}
|
||||
|
||||
protected override void CreateTableColumns(StringBuilder sql, ColumnDesc[] columns) {
|
||||
@ -136,12 +130,11 @@ namespace MCGalaxy.SQL {
|
||||
}
|
||||
|
||||
public override void PrintSchema(string table, TextWriter w) {
|
||||
const string syntax = "SELECT sql from sqlite_master WHERE tbl_name = @0 AND type = 'table'";
|
||||
List<string> all = Database.GetStrings(syntax + CaselessWhereSuffix, table);
|
||||
string sql = "SELECT sql from sqlite_master WHERE tbl_name = @0 AND type = 'table'";
|
||||
List<string> all = Database.GetStrings(sql + CaselessWhereSuffix, table);
|
||||
|
||||
for (int i = 0; i < all.Count; i++) {
|
||||
string sql = all[i];
|
||||
sql = sql.Replace(" " + table, " `" + table + "`");
|
||||
sql = all[i].Replace(" " + table, " `" + table + "`");
|
||||
sql = sql.Replace("CREATE TABLE `" + table + "`", "CREATE TABLE IF NOT EXISTS `" + table + "`");
|
||||
w.WriteLine(sql + ";");
|
||||
}
|
||||
@ -149,8 +142,8 @@ namespace MCGalaxy.SQL {
|
||||
|
||||
public override void AddColumn(string table, ColumnDesc col, string colAfter) {
|
||||
ValidateTable(table);
|
||||
string syntax = "ALTER TABLE `" + table + "` ADD COLUMN " + col.Column + " " + col.FormatType();
|
||||
Database.Execute(syntax);
|
||||
string sql = "ALTER TABLE `" + table + "` ADD COLUMN " + col.Column + " " + col.FormatType();
|
||||
Database.Execute(sql, null);
|
||||
}
|
||||
|
||||
public override void AddOrReplaceRow(string table, string columns, params object[] args) {
|
||||
|
@ -74,9 +74,6 @@ namespace MCGalaxy.SQL {
|
||||
/// <summary> Renames the source table to the given name. </summary>
|
||||
public abstract void RenameTable(string srcTable, string dstTable);
|
||||
|
||||
/// <summary> Removes all entries from the given table. </summary>
|
||||
public abstract void ClearTable(string table);
|
||||
|
||||
/// <summary> Creates a new table in the database (unless it already exists). </summary>
|
||||
public virtual void CreateTable(string table, ColumnDesc[] columns) {
|
||||
ValidateTable(table);
|
||||
@ -84,7 +81,7 @@ namespace MCGalaxy.SQL {
|
||||
sql.AppendLine("CREATE TABLE if not exists `" + table + "` (");
|
||||
CreateTableColumns(sql, columns);
|
||||
sql.AppendLine(");");
|
||||
Database.Execute(sql.ToString());
|
||||
Database.Execute(sql.ToString(), null);
|
||||
}
|
||||
|
||||
protected abstract void CreateTableColumns(StringBuilder sql, ColumnDesc[] columns);
|
||||
@ -92,8 +89,8 @@ namespace MCGalaxy.SQL {
|
||||
/// <summary> Completely removes the given table. </summary>
|
||||
public virtual void DeleteTable(string table) {
|
||||
ValidateTable(table);
|
||||
string syntax = "DROP TABLE `" + table + "`";
|
||||
Database.Execute(syntax);
|
||||
string sql = "DROP TABLE `" + table + "`";
|
||||
Database.Execute(sql, null);
|
||||
}
|
||||
|
||||
/// <summary> Prints/dumps the table schema of the given table. </summary>
|
||||
@ -114,8 +111,8 @@ namespace MCGalaxy.SQL {
|
||||
public virtual void CopyAllRows(string srcTable, string dstTable) {
|
||||
ValidateTable(srcTable);
|
||||
ValidateTable(dstTable);
|
||||
string syntax = "INSERT INTO `" + dstTable + "` SELECT * FROM `" + srcTable + "`";
|
||||
Database.Execute(syntax);
|
||||
string sql = "INSERT INTO `" + dstTable + "` SELECT * FROM `" + srcTable + "`";
|
||||
Database.Execute(sql, null);
|
||||
}
|
||||
|
||||
/// <summary> Iterates over read rows for the given table. </summary>
|
||||
@ -124,9 +121,9 @@ namespace MCGalaxy.SQL {
|
||||
public virtual object ReadRows(string table, string columns, object arg,
|
||||
ReaderCallback callback, string modifier = "", params object[] args) {
|
||||
ValidateTable(table);
|
||||
string syntax = "SELECT " + columns + " FROM `" + table + "`";
|
||||
if (modifier.Length > 0) syntax += " " + modifier;
|
||||
return Database.Iterate(syntax, arg, callback, args);
|
||||
string sql = "SELECT " + columns + " FROM `" + table + "`";
|
||||
if (modifier.Length > 0) sql += " " + modifier;
|
||||
return Database.Iterate(sql, arg, callback, args);
|
||||
}
|
||||
|
||||
/// <summary> Updates rows for the given table. </summary>
|
||||
@ -134,18 +131,18 @@ namespace MCGalaxy.SQL {
|
||||
public virtual void UpdateRows(string table, string columns,
|
||||
string modifier = "", params object[] args) {
|
||||
ValidateTable(table);
|
||||
string syntax = "UPDATE `" + table + "` SET " + columns;
|
||||
if (modifier.Length > 0) syntax += " " + modifier;
|
||||
Database.Execute(syntax, args);
|
||||
string sql = "UPDATE `" + table + "` SET " + columns;
|
||||
if (modifier.Length > 0) sql += " " + modifier;
|
||||
Database.Execute(sql, args);
|
||||
}
|
||||
|
||||
/// <summary> Deletes rows for the given table. </summary>
|
||||
/// <remarks> modifier is optional SQL which can be used to delete only certain rows.</remarks>
|
||||
public virtual void DeleteRows(string table, string modifier = "", params object[] args) {
|
||||
ValidateTable(table);
|
||||
string syntax = "DELETE FROM `" + table + "`";
|
||||
if (modifier.Length > 0) syntax += " " + modifier;
|
||||
Database.Execute(syntax, args);
|
||||
string sql = "DELETE FROM `" + table + "`";
|
||||
if (modifier.Length > 0) sql += " " + modifier;
|
||||
Database.Execute(sql, args);
|
||||
}
|
||||
|
||||
/// <summary> Adds a row to the given table. </summary>
|
||||
|
@ -17,6 +17,7 @@
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MCGalaxy.SQL;
|
||||
|
||||
namespace MCGalaxy.DB {
|
||||
|
||||
@ -112,7 +113,7 @@ namespace MCGalaxy.DB {
|
||||
}
|
||||
|
||||
public static string FormatDate(string input) {
|
||||
DateTime time = DateTime.Parse(input);
|
||||
DateTime time = DateTime.ParseExact(input, Database.DateFormat, null);
|
||||
TimeSpan delta = DateTime.Now - time;
|
||||
return string.Format("{0:H:mm} on {0:d} ({1} ago)", time, delta.Shorten());
|
||||
}
|
||||
|
@ -262,7 +262,6 @@ namespace MCGalaxy.Tasks {
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
bulk.Commit();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user