add mysql code

master
Christopher 2020-07-08 23:52:52 +02:00
parent e0ff4ffbdf
commit f60094505b
3 changed files with 119 additions and 36 deletions

View File

@ -12,6 +12,7 @@
<ReferencePath>../../../bin</ReferencePath>
<Reference name="System" localCopy="false"/>
<Reference name="System.Data"/>
<Reference name="System.Xml"/>
<Reference name="log4net.dll" path="../../../bin"/>
<Reference name="Nini.dll" path="../../../bin"/>
@ -25,6 +26,7 @@
<Reference name="OpenSim.Services.Interfaces" path="../../../bin"/>
<Reference name="OpenSim.Server.Base" path="../../../bin"/>
<Reference name="OpenSim.Data" path="../../../bin"/>
<Reference name="MySQL.Data" path="../../../bin"/>
<Files>
<Match pattern="*.cs" recurse="true"/>
</Files>

View File

@ -98,6 +98,8 @@ namespace OpenSim.Modules.DataValue
if (m_storage == null)
m_storage = new Memory();
m_log.Info("[" + Name + "] Using '" + m_storageTyp + "' as Storage.");
m_scriptModule = m_scene.RequestModuleInterface<IScriptModuleComms>();
if (m_scriptModule != null)
{
@ -122,16 +124,16 @@ namespace OpenSim.Modules.DataValue
{
if(m_storage != null)
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
if (_element != null)
return _element.get();
checkRateLimit();
try
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
if (_element != null)
return _element.get();
checkRateLimit();
String _data = m_storage.get(_host.GroupID.ToString(), key);
if (_data == null)
@ -155,19 +157,19 @@ namespace OpenSim.Modules.DataValue
{
if (m_storage != null)
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
if (_element != null)
{
_element.save(value);
return;
}
checkRateLimit();
try
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
if (_element != null)
{
_element.save(value);
return;
}
checkRateLimit();
m_cache.Add(new StorageElement(_host.GroupID.ToString(), key, value, m_storage));
m_storage.save(_host.GroupID.ToString(), key, value);
return;
@ -183,15 +185,15 @@ namespace OpenSim.Modules.DataValue
[ScriptInvocation]
public void osDeleteDataValue(UUID hostID, UUID scriptID, string key, string value)
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
checkRateLimit();
if (m_storage != null)
{
try
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
checkRateLimit();
if (_element != null)
m_cache.Remove(_element);
@ -212,16 +214,16 @@ namespace OpenSim.Modules.DataValue
{
if (m_storage != null)
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
if (_element != null)
return 1;
checkRateLimit();
try
{
SceneObjectGroup _host = m_scene.GetSceneObjectGroup(hostID);
StorageElement _element = m_cache.Find(X => X.Group == _host.GroupID.ToString() && X.Index == key);
if (_element != null)
return 1;
checkRateLimit();
if (m_storage.check(_host.GroupID.ToString(), key))
return 1;

View File

@ -1,12 +1,15 @@
using log4net;
using MySql.Data.MySqlClient;
using Nini.Config;
using OpenSim.Region.Framework.Scenes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace OpenSim.Modules.DataValue.Storage
{
@ -15,30 +18,106 @@ namespace OpenSim.Modules.DataValue.Storage
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private Scene m_scene = null;
private Timer m_timer = null;
private String m_connectionString = null;
private MySqlConnection m_mySQLClient = null;
public MySQL(Scene scene, IConfig config)
{
m_scene = scene;
m_connectionString = config.GetString("DataValueConnectionString", String.Empty).ToUpper().Trim();
if (m_connectionString == String.Empty)
return;
m_timer = new Timer();
m_timer.Interval = 10000;
m_timer.Elapsed += mysqlping;
m_timer.Start();
m_mySQLClient = new MySqlConnection(m_connectionString);
m_mySQLClient.Open();
createEmptyTable();
}
private void mysqlping(object sender, ElapsedEventArgs e)
{
if (!m_mySQLClient.Ping())
m_mySQLClient.Open();
}
public bool check(String storageID, string key)
{
throw new NotImplementedException();
using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "Select StorageID, StorageKey FROM ?mysqlTable WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
using (IDataReader _mysqlReader = _mysqlCommand.ExecuteReader())
{
if (_mysqlReader["StorageKey"] != null)
return true;
return false;
}
}
}
public string get(String storageID, string key)
{
throw new NotImplementedException();
using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "Select StorageID, StorageKey, StorageData FROM ?mysqlTable WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
using (IDataReader _mysqlReader = _mysqlCommand.ExecuteReader())
{
if (_mysqlReader["StorageData"] != null)
return _mysqlReader["StorageData"].ToString();
return null;
}
}
}
public void remove(string storageID, string key)
{
throw new NotImplementedException();
using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "DELETE FROM ?mysqlTable WHERE StorageID = ?mysqlStorage AND StorageKey = ?mysqlStorageKey";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_mysqlCommand.Parameters.AddWithValue("?mysqlStorage", storageID);
_mysqlCommand.Parameters.AddWithValue("?mysqlStorageKey", key);
_mysqlCommand.ExecuteNonQuery();
}
}
public void save(String storageID, string key, string data)
{
throw new NotImplementedException();
using (MySqlCommand _mysqlCommand = m_mySQLClient.CreateCommand())
{
_mysqlCommand.CommandText = "REPLACE INTO ?mysqlTable (StorageID, StorageKey, StorageData) VALUES (?mysqlStorage, ?mysqlStorageKey, ?mysqlStorageData)";
_mysqlCommand.Parameters.AddWithValue("?mysqlTable", "StorageData");
_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())
{
_mysqlCommand.CommandText = "CREATE TABLE IF NOT EXISTS `StorageData` (`StorageID` VARCHAR(36) NOT NULL, `StorageKey` VARCHAR(256) NOT NULL, `StorageData` TEXT NOT NULL DEFAULT '', PRIMARY KEY(`StorageID`, `StorageKey`)) COLLATE = 'utf8_general_ci'; ";
_mysqlCommand.ExecuteNonQuery();
}
}
}
}