243 lines
7.5 KiB
C#
243 lines
7.5 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;
|
|
using log4net;
|
|
|
|
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;
|
|
private static ILog _log = LogManager.GetLogger(typeof(DBConnector));
|
|
|
|
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);
|
|
SqlDataReader result = null;
|
|
try
|
|
{
|
|
result = cmd.ExecuteReader();
|
|
}
|
|
catch(SqlException ex)
|
|
{
|
|
_log.ErrorFormat("ExecuteQuery: {0}", ex.Message);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public int ExecuteNonQuery(string query)
|
|
{
|
|
if (!this.CheckConnection()) return 0;
|
|
SqlCommand cmd = new SqlCommand(query, this.dbCon);
|
|
int result = -1;
|
|
try
|
|
{
|
|
result = cmd.ExecuteNonQuery();
|
|
}
|
|
catch(SqlException ex)
|
|
{
|
|
_log.ErrorFormat("ExecuteNonQuery: [{0}] {1} ", query, ex.Message);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public object ExecuteScalar(string query)
|
|
{
|
|
if (!this.CheckConnection()) return 0;
|
|
SqlCommand cmd = new SqlCommand(query, this.dbCon);
|
|
object result = null;
|
|
try
|
|
{
|
|
result = cmd.ExecuteScalar();
|
|
}
|
|
catch(SqlException ex)
|
|
{
|
|
_log.ErrorFormat("ExecuteScalar: [{0}] {1} ", query, ex.Message);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
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)
|
|
{
|
|
_log.ErrorFormat("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>();
|
|
_log.Info("loading stations..");
|
|
List<AISStation> stations = AISStation.LoadStations(this);
|
|
_log.InfoFormat("{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);
|
|
Guid? pid = AISPosReport.Save(target, this, updateStations.ContainsKey(target.Station) ? updateStations[target.Station] : null);
|
|
if (pid.HasValue)
|
|
{
|
|
hotposition.PosReportId = pid.Value;
|
|
hotposition.Save(this);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_log.InfoFormat("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)
|
|
{
|
|
_log.Error(ex.ToString());
|
|
}
|
|
}
|
|
return this.dbCon.State == System.Data.ConnectionState.Open;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|