diff --git a/Database/Backends/IDatabaseBackend.cs b/Database/IDatabaseBackend.cs similarity index 100% rename from Database/Backends/IDatabaseBackend.cs rename to Database/IDatabaseBackend.cs diff --git a/Database/MySQL/MySQLBackend.cs b/Database/MySQL/MySQLBackend.cs new file mode 100644 index 000000000..229a62d0a --- /dev/null +++ b/Database/MySQL/MySQLBackend.cs @@ -0,0 +1,54 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.osedu.org/licenses/ECL-2.0 + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Data; +using MySql.Data.MySqlClient; + +namespace MCGalaxy.SQL { + + public sealed class MySQLBackend : IDatabaseBackend { + + public static IDatabaseBackend Instance = new MySQLBackend(); + static ParameterisedQuery queryInstance = new MySQLParameterisedQuery(); + + static string connFormat = "Data Source={0};Port={1};User ID={2};Password={3};Pooling={4}"; + public override string ConnectionString { + get { return String.Format(connFormat, Server.MySQLHost, Server.MySQLPort, + Server.MySQLUsername, Server.MySQLPassword, Server.DatabasePooling); } + } + + public override BulkTransaction CreateBulk() { + return new MySQLBulkTransaction(ConnectionString); + } + + public override ParameterisedQuery CreateParameterised() { + return new MySQLParameterisedQuery(); + } + + internal override ParameterisedQuery GetStaticParameterised() { + return queryInstance; + } + + + public override bool TableExists(string table) { + const string syntax = "SELECT * FROM information_schema.tables WHERE table_schema = @1 AND table_name = @0"; + using (DataTable results = Database.Fill(syntax, table, Server.MySQLDatabaseName)) + return results.Rows.Count > 0; + } + } +} diff --git a/Database/MySQL/MySQLBulkTransaction.cs b/Database/MySQL/MySQLBulkTransaction.cs new file mode 100644 index 000000000..1bd75a113 --- /dev/null +++ b/Database/MySQL/MySQLBulkTransaction.cs @@ -0,0 +1,44 @@ +/* + Copyright 2011 MCForge + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.osedu.org/licenses/ECL-2.0 + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Data; +using MySql.Data.MySqlClient; + +namespace MCGalaxy.SQL { + + public sealed class MySQLBulkTransaction : BulkTransaction { + + public MySQLBulkTransaction(string connString) { + connection = new MySqlConnection(connString); + connection.Open(); + connection.ChangeDatabase(Server.MySQLDatabaseName); + + transaction = connection.BeginTransaction(); + } + + public override IDbCommand CreateCommand(string query) { + return new MySqlCommand(query, (MySqlConnection)connection, (MySqlTransaction)transaction); + } + + public override IDataParameter CreateParam(string paramName, DbType type) { + MySqlParameter arg = new MySqlParameter(paramName, null); + arg.DbType = type; + return arg; + } + } +} diff --git a/Database/Backends/MySQLBackend.cs b/Database/MySQL/MySQLParameterisedQuery.cs similarity index 51% rename from Database/Backends/MySQLBackend.cs rename to Database/MySQL/MySQLParameterisedQuery.cs index 9622f6b87..fd667c267 100644 --- a/Database/Backends/MySQLBackend.cs +++ b/Database/MySQL/MySQLParameterisedQuery.cs @@ -1,6 +1,5 @@ /* Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) - Copyright 2011 MCForge Dual-licensed under the Educational Community License, Version 2.0 and the GNU General Public License, Version 3 (the "Licenses"); you may @@ -21,57 +20,6 @@ using System.Data; using MySql.Data.MySqlClient; namespace MCGalaxy.SQL { - - public sealed class MySQLBackend : IDatabaseBackend { - - public static IDatabaseBackend Instance = new MySQLBackend(); - static ParameterisedQuery queryInstance = new SQLiteParameterisedQuery(); - - static string connFormat = "Data Source={0};Port={1};User ID={2};Password={3};Pooling={4}"; - public override string ConnectionString { - get { return String.Format(connFormat, Server.MySQLHost, Server.MySQLPort, - Server.MySQLUsername, Server.MySQLPassword, Server.DatabasePooling); } - } - - public override BulkTransaction CreateBulk() { - return new MySQLBulkTransaction(ConnectionString); - } - - public override ParameterisedQuery CreateParameterised() { - return new MySQLParameterisedQuery(); - } - - internal override ParameterisedQuery GetStaticParameterised() { - return queryInstance; - } - - public override bool TableExists(string table) { - const string syntax = "SELECT * FROM information_schema.tables WHERE table_schema = @1 AND table_name = @0"; - using (DataTable results = Database.Fill(syntax, table, Server.MySQLDatabaseName)) - return results.Rows.Count > 0; - } - } - - public sealed class MySQLBulkTransaction : BulkTransaction { - - public MySQLBulkTransaction(string connString) { - connection = new MySqlConnection(connString); - connection.Open(); - connection.ChangeDatabase(Server.MySQLDatabaseName); - - transaction = connection.BeginTransaction(); - } - - public override IDbCommand CreateCommand(string query) { - return new MySqlCommand(query, (MySqlConnection)connection, (MySqlTransaction)transaction); - } - - public override IDataParameter CreateParam(string paramName, DbType type) { - MySqlParameter arg = new MySqlParameter(paramName, null); - arg.DbType = type; - return arg; - } - } public sealed class MySQLParameterisedQuery : ParameterisedQuery { diff --git a/Database/Backends/SQLiteBackend.cs b/Database/SQLite/SQLiteBackend.cs similarity index 52% rename from Database/Backends/SQLiteBackend.cs rename to Database/SQLite/SQLiteBackend.cs index b11717b8e..b7b37d532 100644 --- a/Database/Backends/SQLiteBackend.cs +++ b/Database/SQLite/SQLiteBackend.cs @@ -1,5 +1,5 @@ /* - Copyright 2011 MCForge + Copyright 2015 MCGalaxy Dual-licensed under the Educational Community License, Version 2.0 and the GNU General Public License, Version 3 (the "Licenses"); you may @@ -43,56 +43,11 @@ namespace MCGalaxy.SQL { return queryInstance; } + public override bool TableExists(string table) { const string syntax = "SELECT name FROM sqlite_master WHERE type='table' AND name=@0"; using (DataTable results = Database.Fill(syntax, table)) return results.Rows.Count > 0; } } - - public sealed class SQLiteBulkTransaction : BulkTransaction { - - public SQLiteBulkTransaction(string connString) { - connection = new SQLiteConnection(connString); - connection.Open(); - transaction = connection.BeginTransaction(); - } - - public override IDbCommand CreateCommand(string query) { - return new SQLiteCommand(query, (SQLiteConnection)connection, (SQLiteTransaction)transaction); - } - - public override IDataParameter CreateParam(string paramName, DbType type) { - return new SQLiteParameter(paramName, type); - } - } - - public sealed class SQLiteParameterisedQuery : ParameterisedQuery { - - public override void Execute(string query, string connString, bool createDB = false) { - using (var conn = new SQLiteConnection(connString)) { - conn.Open(); - using (SQLiteCommand cmd = new SQLiteCommand(query, conn)) { - foreach (var param in parameters) - cmd.Parameters.AddWithValue(param.Key, param.Value); - cmd.ExecuteNonQuery(); - } - conn.Close(); - } - } - - public override void Fill(string query, string connString, DataTable results) { - using (var conn = new SQLiteConnection(connString)) { - conn.Open(); - - using (SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn)) { - foreach (var param in parameters) - da.SelectCommand.Parameters.AddWithValue(param.Key, param.Value); - da.Fill(results); - da.SelectCommand.Dispose(); - } - conn.Close(); - } - } - } } diff --git a/Database/SQLite/SQLiteBulkTransaction.cs b/Database/SQLite/SQLiteBulkTransaction.cs new file mode 100644 index 000000000..5aa6c5db4 --- /dev/null +++ b/Database/SQLite/SQLiteBulkTransaction.cs @@ -0,0 +1,40 @@ +/* + Copyright 2011 MCForge + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.osedu.org/licenses/ECL-2.0 + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Data; +using System.Data.SQLite; + +namespace MCGalaxy.SQL { + + public sealed class SQLiteBulkTransaction : BulkTransaction { + + public SQLiteBulkTransaction(string connString) { + connection = new SQLiteConnection(connString); + connection.Open(); + transaction = connection.BeginTransaction(); + } + + public override IDbCommand CreateCommand(string query) { + return new SQLiteCommand(query, (SQLiteConnection)connection, (SQLiteTransaction)transaction); + } + + public override IDataParameter CreateParam(string paramName, DbType type) { + return new SQLiteParameter(paramName, type); + } + } +} diff --git a/Database/SQLite/SQLiteParameterisedQuery.cs b/Database/SQLite/SQLiteParameterisedQuery.cs new file mode 100644 index 000000000..b1d346996 --- /dev/null +++ b/Database/SQLite/SQLiteParameterisedQuery.cs @@ -0,0 +1,52 @@ +/* + Copyright 2011 MCForge + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.osedu.org/licenses/ECL-2.0 + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Data; +using System.Data.SQLite; + +namespace MCGalaxy.SQL { + + public sealed class SQLiteParameterisedQuery : ParameterisedQuery { + + public override void Execute(string query, string connString, bool createDB = false) { + using (var conn = new SQLiteConnection(connString)) { + conn.Open(); + using (SQLiteCommand cmd = new SQLiteCommand(query, conn)) { + foreach (var param in parameters) + cmd.Parameters.AddWithValue(param.Key, param.Value); + cmd.ExecuteNonQuery(); + } + conn.Close(); + } + } + + public override void Fill(string query, string connString, DataTable results) { + using (var conn = new SQLiteConnection(connString)) { + conn.Open(); + + using (SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn)) { + foreach (var param in parameters) + da.SelectCommand.Parameters.AddWithValue(param.Key, param.Value); + da.Fill(results); + da.SelectCommand.Dispose(); + } + conn.Close(); + } + } + } +} diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 0af1e301d..57d89a9be 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -405,11 +405,15 @@ - - - + + + + + + + @@ -694,7 +698,8 @@ - + +