Skip to content

Commit

Permalink
Merge branch 'sqlite'
Browse files Browse the repository at this point in the history
  • Loading branch information
firefly2442 committed Aug 30, 2012
2 parents 09eb649 + 34ad24e commit 8d94c91
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 102 deletions.
10 changes: 5 additions & 5 deletions Arma2NETMySQLPlugin/Arma2NETMySQLPlugin.cs
Expand Up @@ -37,9 +37,9 @@ public override string Invoke(string args, int maxResultSize)

Logger.addMessage(Logger.LogType.Info, "Received - Database: " + database + " Procedure: " + procedure + " Parameters: " + parameters.ToString());

if (MySQL.dbs.SQLProviderExists(database))
if (SQL.dbs.SQLProviderExists(database))
{
IEnumerable<string[][]> returned = MySQL.dbs.getSQLProvider(database).RunProcedure(procedure, split.ToArray(), maxResultSize);
IEnumerable<string[][]> returned = SQL.dbs.getSQLProvider(database).RunProcedure(procedure, split.ToArray(), maxResultSize);
return Format.ObjectAsSqf(returned);
}
else
Expand All @@ -64,7 +64,7 @@ public override void Unload()
}

//the function name for the plugin (called from Arma side)
[AddIn("Arma2NETMySQLCommand", Version = "0.1.0.0", Publisher = "firefly2442", Description = "Runs raw MySQL commands")]
[AddIn("Arma2NETMySQLCommand", Version = "0.1.0.0", Publisher = "firefly2442", Description = "Runs raw MySQL/SQLite commands")]
public class Arma2NETMySQLPluginCommand : AddIn
{
//This method is called when callExtension is used from SQF:
Expand All @@ -82,9 +82,9 @@ public override string Invoke(string args, int maxResultSize)

Logger.addMessage(Logger.LogType.Info, "Received - Database: " + database + " MySQL Command: " + mysql_command.ToString());

if (MySQL.dbs.SQLProviderExists(database))
if (SQL.dbs.SQLProviderExists(database))
{
IEnumerable<string[][]> returned = MySQL.dbs.getSQLProvider(database).RunCommand(mysql_command, maxResultSize);
IEnumerable<string[][]> returned = SQL.dbs.getSQLProvider(database).RunCommand(mysql_command, maxResultSize);
return Format.ObjectAsSqf(returned);
}
else
Expand Down
8 changes: 7 additions & 1 deletion Arma2NETMySQLPlugin/Arma2NETMySQLPlugin.csproj
Expand Up @@ -40,6 +40,10 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite, Version=1.0.81.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\Program Files\System.Data.SQLite\2010\bin\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -48,11 +52,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Arma2NETMySQLPlugin.cs" />
<Compile Include="SQLite.cs" />
<Compile Include="MySQL.cs" />
<Compile Include="Startup.cs" />
<Compile Include="DatabaseObject.cs" />
<Compile Include="Databases.cs" />
<Compile Include="Logger.cs" />
<Compile Include="MySQL.cs" />
<Compile Include="SQL.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
37 changes: 27 additions & 10 deletions Arma2NETMySQLPlugin/DatabaseObject.cs
Expand Up @@ -3,32 +3,49 @@
using System.Linq;
using System.Text;


namespace Arma2NETMySQLPlugin
{
class DatabaseObject
{
public string type;
public string databasename;
public string ipaddress;
public string username;
public string password;
public string port;

public MySQL mysql_connection;
public SQL sql_connection;

public DatabaseObject(string name, string ip, string p, string user, string pass)
public DatabaseObject(params string[] values)
{
//Constructor
databasename = name;
ipaddress = ip;
port = p;
username = user;
password = pass;
type = values[0];
databasename = values[1];
if (values.Length == 6) {
ipaddress = values[2];
port = values[3];
username = values[4];
password = values[5];
}

mysql_connection = new MySQL();
mysql_connection.OpenConnection(getFormattedConnectionString());
if (type == "mysql")
{
sql_connection = new MySQL();
sql_connection.OpenConnection(getMySQLFormattedConnectionString());
}
else if (type == "sqlite")
{
sql_connection = new SQLite();
sql_connection.OpenConnection(databasename);
}
else
{
Logger.addMessage(Logger.LogType.Error, "Database type does not match one of the supported types.");
}
}

private string getFormattedConnectionString()
private string getMySQLFormattedConnectionString()
{
//With new (6.5.4) versions of the MySQL Connector, it was throwing an error when running the stored procedures:
//Unable to retrieve stored procedure metadata for routine. Either grant SELECT privilege to mysql.proc for this user or use "check parameters=false" with your connection string.
Expand Down
27 changes: 18 additions & 9 deletions Arma2NETMySQLPlugin/Databases.cs
Expand Up @@ -6,7 +6,7 @@

namespace Arma2NETMySQLPlugin
{
class Databases
public class Databases
{
private List<DatabaseObject> databaseList = new List<DatabaseObject>();

Expand All @@ -16,12 +16,13 @@ public Databases()

//Load the database names, ip, port, usernames, passwords, and so on from file
string line;
if (!File.Exists("Databases.txt"))
var databasesFileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Arma2MySQL/Databases.txt");
if (!File.Exists(databasesFileLocation))
{
Logger.addMessage(Logger.LogType.Error, "Unable to find the Databases.txt file, are you sure it's there?");
}

StreamReader sr = File.OpenText("Databases.txt");
StreamReader sr = File.OpenText(databasesFileLocation);
line = sr.ReadLine();
while (line != null)
{
Expand All @@ -30,10 +31,18 @@ public Databases()
{
//Separate out the information
string[] split = line.Split(',');
if (split.Length == 5)
if (split.Length == 6)
{
Logger.addMessage(Logger.LogType.Info, "Database: " + split[0] + " IPAddress: " + split[1] + " Port: " + split[2] + " Username: " + split[3] + " Password: NotShownForSecurityReasons");
DatabaseObject temp = new DatabaseObject(split[0], split[1], split[2], split[3], split[4]);
split[0] = split[0].ToLower();
Logger.addMessage(Logger.LogType.Info, "Type: " + split[0] + " Database: " + split[1] + " IPAddress: " + split[2] + " Port: " + split[3] + " Username: " + split[4] + " Password: NotShownForSecurityReasons");
DatabaseObject temp = new DatabaseObject(new string[6] {split[0], split[1], split[2], split[3], split[4], split[5]});
databaseList.Add(temp);
}
else if (split.Length == 2)
{
split[0] = split[0].ToLower();
Logger.addMessage(Logger.LogType.Info, "Type: " + split[0] + " Database: " + split[1]);
DatabaseObject temp = new DatabaseObject(new string[2] {split[0], split[1]});
databaseList.Add(temp);
}
else if (line.Contains(","))
Expand All @@ -55,7 +64,7 @@ public void shutdown()
{
for (int i = 0; i < databaseList.Count(); i++)
{
databaseList[i].mysql_connection.CloseConnection();
databaseList[i].sql_connection.CloseConnection();
}
}

Expand All @@ -71,13 +80,13 @@ public bool SQLProviderExists(string dbname)
return false;
}

public MySQL getSQLProvider(string dbname)
public SQL getSQLProvider(string dbname)
{
for (int i = 0; i < databaseList.Count(); i++)
{
if (databaseList[i].databasename == dbname)
{
return databaseList[i].mysql_connection;
return databaseList[i].sql_connection;
}
}
return null;
Expand Down
10 changes: 5 additions & 5 deletions Arma2NETMySQLPlugin/Logger.cs
Expand Up @@ -33,16 +33,16 @@ public Logger()
if (State == loggerState.Stopped)
{
//check to see if the logs folder exists, if not create it
if (!System.IO.Directory.Exists("logs"))
{
System.IO.Directory.CreateDirectory("logs");
var logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Arma2MySQL/logs/");
if (!System.IO.Directory.Exists(logDir)) {
System.IO.Directory.CreateDirectory(logDir);
}

//Setup file streams
DateTime dateValue = new DateTime();
dateValue = DateTime.Now;
string relativepath = Path.Combine("logs", dateValue.ToString("MM-dd-yyyy_HH-mm-ss") + ".log");
fs = new FileStream(relativepath, FileMode.Append);
string fullpath = Path.Combine(logDir, dateValue.ToString("MM-dd-yyyy_HH-mm-ss") + ".log");
fs = new FileStream(fullpath, FileMode.Append);
sw = new StreamWriter(fs);

state = loggerState.Started;
Expand Down
89 changes: 35 additions & 54 deletions Arma2NETMySQLPlugin/MySQL.cs
Expand Up @@ -2,25 +2,22 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using Arma2Net.AddInProxy;
using System.Threading;

namespace Arma2NETMySQLPlugin
{
class MySQL
public class MySQL : SQL
{
private object getCommandSync = new object();
private MySqlConnection connection;

public static Databases dbs = null;
private object getCommandSync = new object();

public MySQL()
{
//constructor
// Default constructor for the derived class
}

public void OpenConnection(string connectionString)
public override void OpenConnection(string connectionString)
{
// if there is no connection
if (connection == null)
Expand All @@ -43,17 +40,17 @@ public void OpenConnection(string connectionString)
}
catch (Exception ex)
{
Logger.addMessage(Logger.LogType.Info, "Unable to open connection to database, trying again in 10 seconds." + ex.ToString());
Logger.addMessage(Logger.LogType.Info, "Unable to open connection to MySQL database, trying again in 10 seconds." + ex.ToString());
Thread.Sleep(10000);
}
}
}

public void CloseConnection()
public override void CloseConnection()
{
if (connection != null)
{
while (connection.State == System.Data.ConnectionState.Executing || connection.State == System.Data.ConnectionState.Fetching) ;
while (connection.State == System.Data.ConnectionState.Executing || connection.State == System.Data.ConnectionState.Fetching);

if (connection.State != System.Data.ConnectionState.Closed)
{
Expand All @@ -63,7 +60,23 @@ public void CloseConnection()
}
}

public IEnumerable<string[][]> RunProcedure(string procedure, string[] parameters, int maxResultSize)
private MySqlCommand GetCommand(string procedureName)
{
MySqlCommand cmd = null;
// only one request at a time.
lock (getCommandSync)
{
// create the command
cmd = new MySqlCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = connection;
cmd.CommandText = procedureName;
cmd.Prepare();
}
return cmd;
}

public override IEnumerable<string[][]> RunProcedure(string procedure, string[] parameters, int maxResultSize)
{
//Logger.addMessage(Logger.LogType.Info, "Started RunProcedure");
if (connection != null && connection.State == System.Data.ConnectionState.Open && procedure != null)
Expand Down Expand Up @@ -92,7 +105,7 @@ public IEnumerable<string[][]> RunProcedure(string procedure, string[] parameter
yield break;
}

public IEnumerable<string[][]> RunCommand(string mysql_command, int maxResultSize)
public override IEnumerable<string[][]> RunCommand(string mysql_command, int maxResultSize)
{
//Logger.addMessage(Logger.LogType.Info, "Started RunCommand");
if (connection != null && connection.State == System.Data.ConnectionState.Open && mysql_command != null)
Expand Down Expand Up @@ -145,55 +158,23 @@ private string[][] RunOnDatabase(MySqlCommand command, int maxResultSize)

//convert into the array which we'll have to pass back
string[][] string_array = new string[max_array_rows][];
for (int i = 0; i < max_array_rows; i++) {
for (int i = 0; i < max_array_rows; i++)
{
string_array[i] = to_return[i].ToArray();
}

/*
* callExtension is the method that is used by Arma2NET to pass information between itself and Arma2
* callExtension has a size limit for the max amount of data that can be passed:
* http://community.bistudio.com/wiki/Extensions#A_few_technical_considerations
* The limit is 4 Kilobytes
* One character = one byte
* The Wiki notes that this size limit could change through future patches.
* https://dev-heaven.net/issues/25915
*
* Arma2NET has a long output addin method that does the following:
* "From version 1.5, Arma2NET supports plugins requiring the maximum result size as an argument to the Run method.
* You can use this to ensure that a plugin won't return a result that is too long for Arma 2 to handle."
*
* In Arma2NET, 4095 characters is the limit
* The last character is reserved for null
*
*/
string formatted = Format.ObjectAsSqf(string_array);
int size = Encoding.UTF8.GetByteCount(formatted.ToCharArray());
//Logger.addMessage(Logger.LogType.Info, "Size length: " + size);
if (size > maxResultSize) {
return new string[][] { new [] {"TooLong"} };
} else {
if (validLength(string_array, maxResultSize) == false)
{
return new string[][] { new[] { "TooLong" } };
}
else
{
return string_array;
}
}
}
//Logger.addMessage(Logger.LogType.Info, "Returning error from RunProcedure");
return new string[][] { new [] { "Error" } };
}

private MySqlCommand GetCommand(string procedureName)
{
MySqlCommand cmd = null;
// only one request at a time.
lock (getCommandSync)
{
// create the command
cmd = new MySqlCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = connection;
cmd.CommandText = procedureName;
cmd.Prepare();
}
return cmd;
return new string[][] { new[] { "Error" } };
}
}
}

0 comments on commit 8d94c91

Please sign in to comment.