using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using bsmd.AISService.AIS; namespace bsmd.AISService.DB { internal class Hotposition : AISBaseEntity { private int mmsi; private Guid pid; public int MMSI { get { return this.mmsi; } } public Guid PosReportId { get { return this.pid; } set { this.pid = value; } } public void Save(DBConnector con) { string query = string.Format("UPDATE hotposition SET mmsi={0}, pid='{1}' WHERE id='{2}'", this.mmsi, this.pid, this.id); con.ExecuteNonQuery(query); } public static Hotposition LoadForMMSI(int mmsi, DBConnector con) { List results = new List(); string query = string.Format("SELECT id, pid FROM hotposition WHERE mmsi={0}", mmsi); SqlDataReader reader = con.ExecuteQuery(query); if (reader != null) { while (reader.Read()) { Hotposition hp = new Hotposition(); hp.id = reader.GetGuid(0); hp.mmsi = mmsi; if (!reader.IsDBNull(1)) hp.pid = reader.GetGuid(1); results.Add(hp); } reader.Close(); } if (results.Count == 0) { // neuen Eintrag erzeugen Hotposition hp = new Hotposition(); hp.Id = Guid.NewGuid(); hp.mmsi = mmsi; hp.IsNew = false; string insertQuery = string.Format("INSERT INTO hotposition (id, mmsi) VALUES ('{0}', {1})", hp.Id, mmsi); con.ExecuteNonQuery(insertQuery); return hp; } else if (results.Count == 1) { return results[0]; } else { // überschüssige HP's löschen (jeweils nur eins pro MMSI) for (int i = 1; i < results.Count; i++) { string delQuery = string.Format("DELETE FROM hotposition WHERE id={0}", results[i].id); con.ExecuteNonQuery(delQuery); } return results[0]; } } } }