git_bsmd/AIS/bsmd.AISService/DB/Hotposition.cs

84 lines
2.6 KiB
C#

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
{
private int id;
private int mmsi;
private int pid;
public int MMSI
{
get { return this.mmsi; }
}
public int 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<Hotposition> results = new List<Hotposition>();
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.GetInt32(0);
hp.mmsi = mmsi;
if (!reader.IsDBNull(1))
hp.pid = reader.GetInt32(1);
results.Add(hp);
}
reader.Close();
}
if (results.Count == 0)
{
// neuen Eintrag erzeugen
Hotposition hp = new Hotposition();
string insertQuery = string.Format("INSERT INTO hotposition SET mmsi={0}", mmsi);
con.ExecuteNonQuery(insertQuery);
object ob = con.ExecuteScalar("SELECT LAST_INSERT_ID()");
hp.id = Convert.ToInt32(ob);
hp.mmsi = mmsi;
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];
}
}
}
}