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

213 lines
6.6 KiB
C#

// Copyright (c) 2008-2018 schick Informatik
// Description: Database connector
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using bsmd.AISService.AIS;
namespace bsmd.AISService.DB
{
public class DBConnector
{
private string connectionString;
private SqlConnection dbCon = null;
private List<AISWatchkeeper> watchkeeperShips = null;
private List<AISStaticData> dbShips = null;
private Dictionary<string, AISStation> updateStations = null;
public DBConnector() { }
#region Properties
public string ConnectionString
{
get { return this.connectionString; }
set { this.connectionString = value; }
}
public List<AISStaticData> DBShips
{
get
{
if (this.dbShips == null)
{
lock (this.dbCon)
{
this.dbShips = AISStaticData.LoadDBShips(this);
}
}
return this.dbShips;
}
set { this.dbShips = value; }
}
public List<AISWatchkeeper> WatchkeeperShips
{
get
{
if (this.watchkeeperShips == null) this.watchkeeperShips = AISWatchkeeper.GetWatchkeeperShips(this);
return this.watchkeeperShips;
}
}
#endregion
#region public methods
public SqlDataReader ExecuteQuery(string query)
{
if (!this.CheckConnection()) return null;
SqlCommand cmd = new SqlCommand(query, this.dbCon);
return cmd.ExecuteReader();
}
public int ExecuteNonQuery(string query)
{
if (!this.CheckConnection()) return 0;
SqlCommand cmd = new SqlCommand(query, this.dbCon);
return cmd.ExecuteNonQuery();
}
public object ExecuteScalar(string query)
{
if (!this.CheckConnection()) return 0;
SqlCommand cmd = new SqlCommand(query, this.dbCon);
return cmd.ExecuteScalar();
}
public bool Open()
{
if (this.dbCon != null && this.dbCon.State == System.Data.ConnectionState.Open) return true;
try
{
this.dbCon = new SqlConnection(this.connectionString);
this.dbCon.Open();
if (this.dbCon.State == System.Data.ConnectionState.Open)
return true;
}
catch (SqlException anException)
{
Trace.WriteLine(string.Format("cannot open SQL DB connection: {0}", anException.Message));
}
return false;
}
public void Close()
{
try
{
if (this.dbCon != null && this.dbCon.State == System.Data.ConnectionState.Open)
this.dbCon.Close();
}
catch (Exception) { } // egal
}
public void Update(AIS_Target target)
{
if (this.dbCon.State != System.Data.ConnectionState.Open) // reopen
{
this.dbCon.Close();
this.dbCon.Open();
}
if (this.updateStations == null)
{
this.updateStations = new Dictionary<string, AISStation>();
Trace.WriteLine("loading stations..");
List<AISStation> stations = AISStation.LoadStations(this);
Trace.WriteLine(string.Format("{0} stations loaded", stations.Count));
foreach (AISStation station in stations)
if (!updateStations.ContainsKey(station.Name))
updateStations.Add(station.Name, station);
}
if (target.LastPosReport != null)
{
Hotposition hotposition = Hotposition.LoadForMMSI(target.MMSI, this);
int? pid = AISPosReport.Save(target, this, updateStations.ContainsKey(target.Station) ? updateStations[target.Station] : null);
if (pid.HasValue)
{
hotposition.PosReportId = pid.Value;
hotposition.Save(this);
}
}
else
{
Trace.WriteLine(string.Format("last pos report is null for target {0}", target.MMSI));
}
if (target.LastStaticData != null)
{
AISStaticData.Save(target, this, updateStations.ContainsKey(target.Station) ? updateStations[target.Station] : null);
}
if ((target.Name == null || target.LastDBName == null) && (target.MMSI > 0))
{
// preload values from DB
AISStaticData.PreloadTarget(target, this);
}
target.UpdateDB = false; // reset update flag
// Watchkeeper check
if (this.watchkeeperShips == null)
this.watchkeeperShips = AISWatchkeeper.GetWatchkeeperShips(this);
if (!target.IsWatchkeeperShip.HasValue && this.watchkeeperShips != null)
{
for (int i = 0; i < this.watchkeeperShips.Count; i++)
{
if (this.watchkeeperShips[i].MMSI == target.MMSI) // found it
target.IsWatchkeeperShip = true;
}
if (!target.IsWatchkeeperShip.HasValue) // didn't find it
target.IsWatchkeeperShip = false;
}
}
public void ResetWatchkeeperList()
{
this.watchkeeperShips = null;
}
public void SaveStation(AISStation station)
{
station.Save(this);
}
#endregion
#region private methods
private bool CheckConnection()
{
// if connection has been closed, re-open the connection
if (this.dbCon.State != System.Data.ConnectionState.Open)
{
try
{
this.dbCon.Close();
this.Open();
}
catch (SqlException ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
}
}
return this.dbCon.State == System.Data.ConnectionState.Open;
}
#endregion
}
}