92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using MySql.Data;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace Matomat
|
|
{
|
|
class DBDriverMysqli : Database
|
|
{
|
|
MySqlConnection conn;
|
|
private Exception err;
|
|
private bool data_b;
|
|
private MySqlDataReader data;
|
|
public DBDriverMysqli()
|
|
{
|
|
|
|
}
|
|
public override bool connect(string server, string dbs, string user, string pw, int port, string driver)
|
|
{
|
|
try
|
|
{
|
|
this.conn = new MySqlConnection("server=" + server + ";user=" + user + ";database=" + dbs + ";port=" + port + ";password=" + pw + ";");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.err = e;
|
|
return false;
|
|
}
|
|
new MySqlCommand("SET NAMES 'UTF8'", this.conn).ExecuteNonQuery();
|
|
new MySqlCommand("SET CHARACTER SET 'UTF8'", this.conn).ExecuteNonQuery();
|
|
return true;
|
|
}
|
|
public override void query(string sql)
|
|
{
|
|
this.err = null;
|
|
if (sql.ToUpper().Substring(0, 6) == "SELECT")
|
|
this.select(sql);
|
|
else if (sql.ToUpper().Substring(0, 6) == "INSERT")
|
|
this.insert(sql);
|
|
else if (sql.ToUpper().Substring(0, 6) == "UPDATE")
|
|
this.update(sql);
|
|
else if (sql.ToUpper().Substring(0, 6) == "DELETE")
|
|
this.delete(sql);
|
|
else if (sql.ToUpper().Substring(0, 8) == "TRUNCATE")
|
|
this.truncate(sql);
|
|
else if (sql.ToUpper().Substring(0, 6) == "CREATE")
|
|
this.create(sql);
|
|
else if (sql.ToUpper().Substring(0, 3) == "SET")
|
|
this.set(sql);
|
|
else
|
|
this.err = new Exception("Keine unterstütze MySQL Abfrage '" + sql + "'...");
|
|
}
|
|
|
|
private void select(string sql)
|
|
{
|
|
this.data_b = true;
|
|
try
|
|
{
|
|
MySqlCommand cmd = new MySqlCommand(sql, this.conn);
|
|
this.data = cmd.ExecuteReader();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.err = e;
|
|
this.data_b = false;
|
|
this.data = null;
|
|
}
|
|
}
|
|
public override List<object> getResultList()
|
|
{
|
|
if (!data_b || this.data == null)
|
|
{
|
|
return null;
|
|
}
|
|
List<object> ret = new List<object>();
|
|
while (this.data.Read())
|
|
{
|
|
object[] row = new object[this.data.FieldCount];
|
|
for (int i = 0; i < this.data.FieldCount; i++)
|
|
{
|
|
row[i] = this.data[i];
|
|
}
|
|
ret.Add(row);
|
|
}
|
|
this.data.Close();
|
|
return ret;
|
|
}
|
|
}
|
|
}
|