Files
marechai/Marechai.Database/Mysql.cs

127 lines
4.2 KiB
C#
Raw Normal View History

2018-04-12 10:20:31 +01:00
/******************************************************************************
2020-02-10 01:52:56 +00:00
// MARECHAI: Master repository of computing history artifacts information
2018-04-12 10:20:31 +01:00
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-02-10 03:05:39 +00:00
// Copyright © 2003-2020 Natalia Portillo
2018-04-12 10:20:31 +01:00
*******************************************************************************/
2018-04-12 11:50:02 +01:00
2018-04-12 10:20:31 +01:00
using System;
2018-04-12 10:05:42 +01:00
using System.Data;
2020-12-20 16:12:51 +00:00
using MySqlConnector;
2018-04-12 05:35:47 +01:00
2020-02-10 02:10:18 +00:00
namespace Marechai.Database
2018-04-12 05:35:47 +01:00
{
2018-04-12 10:05:42 +01:00
public class Mysql : IDbCore
2018-04-12 05:35:47 +01:00
{
MySqlConnection connection;
2018-04-12 11:50:02 +01:00
/// <summary>Database operations</summary>
2018-04-12 10:05:42 +01:00
public Operations Operations { get; private set; }
2018-04-12 11:50:02 +01:00
/// <summary>Last inserted row's ID</summary>
2018-04-12 10:05:42 +01:00
public long LastInsertRowId
{
get
{
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT LAST_INSERT_ID()";
IDataReader reader = command.ExecuteReader();
2020-02-10 22:44:18 +00:00
if(reader == null ||
!reader.Read())
return 0;
2018-04-12 10:05:42 +01:00
long id = reader.GetInt64(0);
reader.Close();
2020-02-10 22:44:18 +00:00
2018-04-12 10:05:42 +01:00
return id;
}
}
2020-02-10 22:44:18 +00:00
/// <summary>Opens an existing database</summary>
2018-04-12 11:50:02 +01:00
/// <param name="server">Server</param>
/// <param name="user">User</param>
/// <param name="database">Database name</param>
/// <param name="password">Password</param>
/// <param name="port">Port</param>
/// <returns><c>true</c> if database opened correctly, <c>false</c> otherwise</returns>
2018-04-12 10:05:42 +01:00
public bool OpenDb(string server, string user, string database, string password, ushort port = 3306)
{
try
{
string connectionString =
$"server={server};user={user};database={database};port={port};password={password}";
connection = new MySqlConnection(connectionString);
2018-04-15 17:51:07 +01:00
connection.Open();
2018-04-12 10:05:42 +01:00
Operations = new Operations(connection, this);
bool res = Operations.UpdateDatabase();
2020-02-10 22:44:18 +00:00
if(res)
return true;
2018-04-12 10:05:42 +01:00
connection = null;
2020-02-10 22:44:18 +00:00
2018-04-12 10:05:42 +01:00
return false;
}
catch(MySqlException ex)
{
Console.WriteLine("Error opening database.");
Console.WriteLine(ex);
connection = null;
2020-02-10 22:44:18 +00:00
2018-04-12 10:05:42 +01:00
return false;
}
}
2020-02-10 22:44:18 +00:00
/// <summary>Closes the database</summary>
2018-04-12 10:05:42 +01:00
public void CloseDb()
2018-04-12 05:35:47 +01:00
{
connection?.Close();
2018-04-12 10:05:42 +01:00
connection = null;
}
2020-02-10 22:44:18 +00:00
/// <summary>Gets a data adapter for the opened database</summary>
2018-04-12 11:50:02 +01:00
/// <returns>Data adapter</returns>
2020-02-10 22:44:18 +00:00
public IDbDataAdapter GetNewDataAdapter() => new MySqlDataAdapter();
2018-04-12 10:05:42 +01:00
2018-04-15 17:51:07 +01:00
public bool TableExists(string tableName)
{
MySqlCommand cmd = connection.CreateCommand();
2020-02-10 22:44:18 +00:00
2018-04-15 17:51:07 +01:00
cmd.CommandText =
$"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{connection.Database}' AND table_name = '{tableName}'";
MySqlDataReader reader = cmd.ExecuteReader();
reader.Read();
int count = reader.GetInt32(0);
reader.Close();
2020-02-10 22:44:18 +00:00
2018-04-15 17:51:07 +01:00
return count > 0;
}
2020-02-10 22:44:18 +00:00
~Mysql() => CloseDb();
2018-04-12 05:35:47 +01:00
}
}