2020-07-07 22:25:02 +00:00
using log4net ;
2020-07-08 21:52:52 +00:00
using MySql.Data.MySqlClient ;
2020-07-07 22:25:02 +00:00
using Nini.Config ;
using OpenSim.Region.Framework.Scenes ;
using System ;
using System.Collections.Generic ;
2020-07-08 21:52:52 +00:00
using System.Data ;
2020-07-07 22:25:02 +00:00
using System.Linq ;
using System.Reflection ;
using System.Text ;
using System.Threading.Tasks ;
2020-07-08 21:52:52 +00:00
using System.Timers ;
2020-07-07 22:25:02 +00:00
namespace OpenSim.Modules.DataValue.Storage
{
class MySQL : iStorage
{
private static readonly ILog m_log = LogManager . GetLogger ( MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
private Scene m_scene = null ;
2020-07-08 21:52:52 +00:00
private Timer m_timer = null ;
private String m_connectionString = null ;
private MySqlConnection m_mySQLClient = null ;
2020-07-07 22:25:02 +00:00
public MySQL ( Scene scene , IConfig config )
{
m_scene = scene ;
2020-07-08 21:52:52 +00:00
2020-07-08 22:17:13 +00:00
m_connectionString = config . GetString ( "DataValueConnectionString" , String . Empty ) . Trim ( ) ;
2020-07-08 21:52:52 +00:00
if ( m_connectionString = = String . Empty )
return ;
m_timer = new Timer ( ) ;
m_timer . Interval = 10000 ;
m_timer . Elapsed + = mysqlping ;
m_timer . Start ( ) ;
2020-07-08 23:05:02 +00:00
try
{
m_mySQLClient = new MySqlConnection ( m_connectionString ) ;
m_mySQLClient . Open ( ) ;
createEmptyTable ( ) ;
}
catch ( Exception _error )
{
m_log . Error ( "[MySQL] " + m_connectionString + " : " + _error . Message ) ;
}
2020-07-08 21:52:52 +00:00
}
private void mysqlping ( object sender , ElapsedEventArgs e )
{
if ( ! m_mySQLClient . Ping ( ) )
m_mySQLClient . Open ( ) ;
2020-07-07 22:25:02 +00:00
}
public bool check ( String storageID , string key )
{
2020-07-08 21:52:52 +00:00
using ( MySqlCommand _mysqlCommand = m_mySQLClient . CreateCommand ( ) )
{
2020-07-08 22:27:41 +00:00
_mysqlCommand . CommandText = "Select StorageID, StorageKey FROM StorageData WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey" ;
2020-07-08 21:52:52 +00:00
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorage" , storageID ) ;
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorageKey" , key ) ;
using ( IDataReader _mysqlReader = _mysqlCommand . ExecuteReader ( ) )
{
2020-07-08 22:38:09 +00:00
if ( ! _mysqlReader . Read ( ) )
return false ;
2020-07-08 22:32:17 +00:00
2020-07-08 21:52:52 +00:00
if ( _mysqlReader [ "StorageKey" ] ! = null )
return true ;
return false ;
}
}
2020-07-07 22:25:02 +00:00
}
public string get ( String storageID , string key )
{
2020-07-08 21:52:52 +00:00
using ( MySqlCommand _mysqlCommand = m_mySQLClient . CreateCommand ( ) )
{
2020-07-08 22:27:41 +00:00
_mysqlCommand . CommandText = "Select StorageID, StorageKey, StorageData FROM StorageData WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey" ;
2020-07-08 21:52:52 +00:00
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorage" , storageID ) ;
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorageKey" , key ) ;
using ( IDataReader _mysqlReader = _mysqlCommand . ExecuteReader ( ) )
{
2020-07-08 22:38:09 +00:00
if ( ! _mysqlReader . Read ( ) )
return null ;
2020-07-08 22:32:17 +00:00
2020-07-08 21:52:52 +00:00
if ( _mysqlReader [ "StorageData" ] ! = null )
return _mysqlReader [ "StorageData" ] . ToString ( ) ;
return null ;
}
}
2020-07-07 22:25:02 +00:00
}
2020-07-07 23:26:12 +00:00
public void remove ( string storageID , string key )
2020-07-07 22:25:02 +00:00
{
2020-07-08 21:52:52 +00:00
using ( MySqlCommand _mysqlCommand = m_mySQLClient . CreateCommand ( ) )
{
2020-07-08 22:27:41 +00:00
_mysqlCommand . CommandText = "DELETE FROM StorageData WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey" ;
2020-07-08 21:52:52 +00:00
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorage" , storageID ) ;
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorageKey" , key ) ;
_mysqlCommand . ExecuteNonQuery ( ) ;
}
2020-07-07 22:25:02 +00:00
}
public void save ( String storageID , string key , string data )
{
2020-07-08 21:52:52 +00:00
using ( MySqlCommand _mysqlCommand = m_mySQLClient . CreateCommand ( ) )
{
2020-07-08 22:27:41 +00:00
_mysqlCommand . CommandText = "REPLACE INTO StorageData (StorageID, StorageKey, StorageData) VALUES (?mysqlStorage, ?mysqlStorageKey, ?mysqlStorageData)" ;
2020-07-08 21:52:52 +00:00
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorage" , storageID ) ;
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorageKey" , key ) ;
_mysqlCommand . Parameters . AddWithValue ( "?mysqlStorageData" , data ) ;
_mysqlCommand . ExecuteNonQuery ( ) ;
}
}
private void createEmptyTable ( )
{
using ( MySqlCommand _mysqlCommand = m_mySQLClient . CreateCommand ( ) )
{
2020-07-08 23:09:56 +00:00
_mysqlCommand . CommandText = "CREATE TABLE IF NOT EXISTS `StorageData` (`StorageID` VARCHAR(36) NOT NULL, `StorageKey` VARCHAR(512) NOT NULL, `StorageData` TEXT NOT NULL DEFAULT '', PRIMARY KEY(`StorageID`, `StorageKey`)) COLLATE = 'utf8_general_ci'; " ;
2020-07-08 21:52:52 +00:00
_mysqlCommand . ExecuteNonQuery ( ) ;
}
2020-07-07 22:25:02 +00:00
}
}
}