diff --git a/Matomat/DBDriverMysqli.cs b/Matomat/DBDriverMysqli.cs index bc29b23..1c3e550 100644 --- a/Matomat/DBDriverMysqli.cs +++ b/Matomat/DBDriverMysqli.cs @@ -13,28 +13,31 @@ namespace Matomat private Exception err; private bool data_b; private MySqlDataReader data; + private long id; 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 + ";"); + this.conn.Open(); } 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(); + this.query("SET NAMES 'UTF8'"); + this.query("SET CHARACTER SET 'UTF8'"); return true; } public override void query(string sql) { this.err = null; + this.data = null; + this.data_b = true; if (sql.ToUpper().Substring(0, 6) == "SELECT") this.select(sql); else if (sql.ToUpper().Substring(0, 6) == "INSERT") @@ -53,9 +56,94 @@ namespace Matomat this.err = new Exception("Keine unterstütze MySQL Abfrage '" + sql + "'..."); } + private void set(string sql) + { + try + { + MySqlCommand cmd = new MySqlCommand(sql, this.conn); + cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + this.err = e; + this.data_b = false; + } + } + + private void create(string sql) + { + try + { + MySqlCommand cmd = new MySqlCommand(sql, this.conn); + cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + this.err = e; + this.data_b = false; + } + } + + private void truncate(string sql) + { + try + { + MySqlCommand cmd = new MySqlCommand(sql, this.conn); + cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + this.err = e; + this.data_b = false; + } + } + + private void delete(string sql) + { + try + { + MySqlCommand cmd = new MySqlCommand(sql, this.conn); + cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + this.err = e; + this.data_b = false; + } + } + + private void update(string sql) + { + try + { + MySqlCommand cmd = new MySqlCommand(sql, this.conn); + cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + this.err = e; + this.data_b = false; + } + + } + + private void insert(string sql) + { + try + { + MySqlCommand cmd = new MySqlCommand(sql, this.conn); + cmd.ExecuteNonQuery(); + this.id = cmd.LastInsertedId; + } + catch (Exception e) + { + this.err = e; + this.data_b = false; + } + } + private void select(string sql) { - this.data_b = true; try { MySqlCommand cmd = new MySqlCommand(sql, this.conn); @@ -68,13 +156,13 @@ namespace Matomat this.data = null; } } - public override List getResultList() + public override List getResultList() { if (!data_b || this.data == null) { return null; } - List ret = new List(); + List ret = new List(); while (this.data.Read()) { object[] row = new object[this.data.FieldCount]; @@ -87,5 +175,41 @@ namespace Matomat this.data.Close(); return ret; } + public override object[] getResult() + { + if (!data_b || this.data == null) + { + return null; + } + if (this.data.HasRows) + { + this.data.Read(); + object[] row = new object[this.data.FieldCount]; + for (int i = 0; i < this.data.FieldCount; i++) + { + row[i] = this.data[i]; + } + return row; + } + return null; + } + public override Exception getError() + { + return this.err; + } + public override long getID() + { + return this.id; + } + public override string quote(string str) + { + return str; + } + ~DBDriverMysqli() + { + if (!this.data.IsClosed) + this.data.Close(); + this.conn.Close(); + } } } diff --git a/Matomat/Database.cs b/Matomat/Database.cs index 961bb8f..372c983 100644 --- a/Matomat/Database.cs +++ b/Matomat/Database.cs @@ -28,7 +28,8 @@ namespace Matomat if (i.signature == server + dbs + user + pw + port + driver) return i.iDatabase; } - Type t = Type.GetType("DBDriver" + char.ToUpper(driver[0]) + driver.Substring(1).ToLower()); + string object_driver = "Matomat.DBDriver" + char.ToUpper(driver[0]) + driver.Substring(1).ToLower(); + Type t = Type.GetType(object_driver, true); Database db = (Database)t.GetConstructor(Type.EmptyTypes).Invoke(new Object[0]); db.connect(server, dbs, user, pw, port, driver); if (db.getError() != null) @@ -39,31 +40,31 @@ namespace Matomat instances.Add(instance); return instance.iDatabase; } - public bool connect(string server, string dbs, string user, string pw, int port, string driver) + public virtual bool connect(string server, string dbs, string user, string pw, int port, string driver) { return false; } - public void query(string sql) + public virtual void query(string sql) { return; } - public object getResult() + public virtual object[] getResult() { return null; } - public List getResultList() + public virtual List getResultList() { return null; } - public Exception getError() + public virtual Exception getError() { return null; } - public int getID() + public virtual long getID() { return 0; } - public string quote(string str) + public virtual string quote(string str) { return str; } diff --git a/Matomat/Matomat.csproj b/Matomat/Matomat.csproj index 25cc1c9..4d8b3ad 100644 --- a/Matomat/Matomat.csproj +++ b/Matomat/Matomat.csproj @@ -34,6 +34,21 @@ 4 + + False + .\mysql.data.dll + + + False + .\mysql.data.entity.dll + + + .\mysql.visualstudio.dll + + + False + .\mysql.web.dll + False .\RfidClass.dll @@ -55,7 +70,6 @@ - @@ -67,16 +81,22 @@ - + PreserveNewest - + PreserveNewest + + + + + +