Bulk-Insert Interface und implementierung für CREW und PAS (IBulkSaver)

This commit is contained in:
Daniel Schick 2022-09-29 09:16:30 +02:00
parent 3845313bb3
commit df32cf6fc6
6 changed files with 178 additions and 16 deletions

View File

@ -333,7 +333,7 @@ namespace ENI2
// Bearbeitungsinformationen für bestehende ID-Beantragung beibehalten, falls bereits vorhanden // Bearbeitungsinformationen für bestehende ID-Beantragung beibehalten, falls bereits vorhanden
if(existingCore.IsTransit && if(existingCore.IsTransit &&
(existingMessage.MessageNotificationClass == Message.NotificationClass.TRANSIT) && (existingMessage.MessageNotificationClass == Message.NotificationClass.TRANSIT) &&
(existingMessage.InternalStatus == Message.BSMDStatus.CONFIRMED)) (existingMessage.InternalStatus == Message.BSMDStatus.CONFIRMED))
{ {
@ -392,7 +392,7 @@ namespace ENI2
newMessage.MessageCoreId = newCore.Id; newMessage.MessageCoreId = newCore.Id;
DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Save(newMessage); DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Save(newMessage);
newMessage.SaveElements(); newMessage.SaveElements();
} }
this.OnOpenNewCoreRequested(newCore); this.OnOpenNewCoreRequested(newCore);
} }

View File

@ -14,7 +14,7 @@ using System.Collections.Generic;
namespace bsmd.database namespace bsmd.database
{ {
public class CREW : DatabaseEntity, ISublistElement public class CREW : DatabaseEntity, ISublistElement, IBulkSaver
{ {
public CREW() public CREW()
@ -272,6 +272,62 @@ namespace bsmd.database
#endregion #endregion
#region IBulkSaver implementation
public DataTable PrepareBulkInsert(List<DatabaseEntity> databaseEntities)
{
DataTable result = new DataTable();
result.Columns.Add(new DataColumn { ColumnName = "MessageHeaderId", DataType = MessageHeader.Id.GetType() });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberLastName", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberFirstName", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberPlaceOfBirth", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberDateOfBirth", DataType = typeof(DateTime), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberGender", DataType = typeof(byte), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberNationality", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberIdentityDocumentType", DataType = typeof(byte), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberIdentityDocumentId", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberVisaNumber", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberDuty", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "Identifier", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "IsDeparture", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberIdentityDocumentIssuingState", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberIdentityDocumentExpiryDate", DataType = typeof(DateTime), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "NotificationSchengen", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "NotificationPAX", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "CrewMemberCountryOfBirth", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "Effects", DataType = typeof(string), AllowDBNull = true });
foreach (CREW crew in databaseEntities)
{
DataRow row = result.NewRow();
row[0] = crew.MessageHeader.Id;
row[1] = crew.CrewMemberLastName ?? (object)DBNull.Value;
row[2] = crew.CrewMemberFirstName ?? (object)DBNull.Value;
row[3] = crew.CrewMemberPlaceOfBirth ?? (object)DBNull.Value;
row[4] = crew.CrewMemberDateOfBirth ?? (object)DBNull.Value;
row[5] = crew.CrewMemberGender ?? (object)DBNull.Value;
row[6] = crew.CrewMemberNationality ?? (object)DBNull.Value;
row[7] = crew.CrewMemberIdentityDocumentType ?? (object)DBNull.Value;
row[8] = crew.CrewMemberIdentityDocumentId ?? (object)DBNull.Value;
row[9] = crew.CrewMemberVisaNumber ?? (object)DBNull.Value;
row[10] = crew.CrewMemberDuty ?? (object)DBNull.Value;
row[11] = crew.Identifier ?? (object)DBNull.Value;
row[12] = crew.IsDeparture;
row[13] = crew.CrewMemberIdentityDocumentIssuingState ?? (object)DBNull.Value;
row[14] = crew.CrewMemberIdentityDocumentExpiryDate ?? (object)DBNull.Value;
row[15] = crew.NotificationSchengen ?? (object)DBNull.Value;
row[16] = crew.NotificationPAX ?? (object)DBNull.Value;
row[17] = crew.CrewMemberCountryOfBirth ?? (object)DBNull.Value;
row[18] = crew.Effects ?? (object)DBNull.Value;
result.Rows.Add(row);
}
return result;
}
#endregion
} }
#region CREWD #region CREWD

View File

@ -1,8 +1,5 @@
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bsmd.database namespace bsmd.database
{ {
@ -50,4 +47,9 @@ namespace bsmd.database
string Identifier { get; set; } string Identifier { get; set; }
} }
public interface IBulkSaver
{
DataTable PrepareBulkInsert(List<DatabaseEntity> databaseEntities);
}
} }

View File

@ -125,7 +125,7 @@ namespace bsmd.database
// create table columns // create table columns
foreach (PropertyInfo p in properties) foreach (PropertyInfo p in properties)
{ {
if (!(p.CanRead && p.CanWrite)) continue; if (!(p.CanRead && p.CanWrite)) continue;
Type propType = p.PropertyType; Type propType = p.PropertyType;
DataColumn dc = new DataColumn(); DataColumn dc = new DataColumn();
dc.ColumnName = p.Name; dc.ColumnName = p.Name;

View File

@ -692,12 +692,19 @@ namespace bsmd.database
public void SaveElements() public void SaveElements()
{ {
foreach (DatabaseEntity dbEntity in this.Elements) if (CanDoBulkSave())
{ {
DBManager.Instance.Save(dbEntity); this.BulkSaveElements();
if (dbEntity is ISublistContainer sublistContainer) }
else
{
foreach (DatabaseEntity dbEntity in this.Elements)
{ {
(sublistContainer).SaveElements(); DBManager.Instance.Save(dbEntity);
if (dbEntity is ISublistContainer sublistContainer)
{
sublistContainer.SaveElements();
}
} }
} }
} }
@ -857,7 +864,7 @@ namespace bsmd.database
} }
#endregion #endregion
#region IComparable implementation #region IComparable implementation
/// <summary> /// <summary>
@ -903,6 +910,39 @@ namespace bsmd.database
else this.Flags &= (int)~flag; else this.Flags &= (int)~flag;
} }
/// <summary>
/// Bulk save is actually bulk insert and can only be done for certain
/// message classes where bulk save is implemented
/// </summary>
private bool CanDoBulkSave()
{
if(this.MessageNotificationClass == NotificationClass.CREW ||
this.MessageNotificationClass == NotificationClass.CREWD ||
this.MessageNotificationClass == NotificationClass.PAS ||
this.MessageNotificationClass == NotificationClass.PASD)
{
foreach (DatabaseEntity subEntity in this.Elements)
if (!subEntity.IsNew)
return false;
return true;
}
return false;
}
private void BulkSaveElements()
{
if (this.Elements.Count == 0) return; // NOP
if(this.Elements[0] is IBulkSaver ibs)
{
DataTable dt = ibs.PrepareBulkInsert(new List<DatabaseEntity>(this.Elements));
DBManager.Instance.PerformBulkInsert(dt);
}
else
{
throw new InvalidOperationException("bulk save called on classes that do not support it");
}
}
#endregion #endregion
} }
} }

View File

@ -14,7 +14,7 @@ using System.Collections.Generic;
namespace bsmd.database namespace bsmd.database
{ {
public class PAS : DatabaseEntity, ISublistElement public class PAS : DatabaseEntity, ISublistElement, IBulkSaver
{ {
public PAS() public PAS()
@ -331,6 +331,70 @@ namespace bsmd.database
#endregion #endregion
#region IBulkSaver implementation
public DataTable PrepareBulkInsert(List<DatabaseEntity> databaseEntities)
{
DataTable result = new DataTable();
result.Columns.Add(new DataColumn { ColumnName = "MessageHeaderId", DataType = MessageHeader.Id.GetType() });
result.Columns.Add(new DataColumn { ColumnName = "PassengerLastName", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerFirstName", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerPlaceOfBirth", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerDateOfBirth", DataType = typeof(DateTime), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerGender", DataType = typeof(byte), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerNationality", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerIdentityDocumentType", DataType = typeof(byte), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerIdentityDocumentId", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerVisaNumber", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerPortOfEmbarkation", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerPortOfDisembarkation", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerInTransit", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "Identifier", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "IsDeparture", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerIdentityDocumentIssuingState", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerIdentityDocumentExpiryDate", DataType = typeof(DateTime), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "NotificationSchengen", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "NotificationPAX", DataType = typeof(bool), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "EmergencyCare", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "EmergencyContactNumber", DataType = typeof(string), AllowDBNull = true });
result.Columns.Add(new DataColumn { ColumnName = "PassengerCountryOfBirth", DataType = typeof(string), AllowDBNull = true });
foreach (PAS pas in databaseEntities)
{
DataRow row = result.NewRow();
row[0] = pas.MessageHeader.Id;
row[1] = pas.PassengerLastName ?? (object)DBNull.Value;
row[2] = pas.PassengerFirstName ?? (object)DBNull.Value;
row[3] = pas.PassengerPlaceOfBirth ?? (object)DBNull.Value;
row[4] = pas.PassengerDateOfBirth ?? (object)DBNull.Value;
row[5] = pas.PassengerGender ?? (object)DBNull.Value;
row[6] = pas.PassengerNationality ?? (object)DBNull.Value;
row[7] = pas.PassengerIdentityDocumentType ?? (object)DBNull.Value;
row[8] = pas.PassengerIdentityDocumentId ?? (object)DBNull.Value;
row[9] = pas.PassengerVisaNumber ?? (object)DBNull.Value;
row[10] = pas.PassengerPortOfEmbarkation ?? (object)DBNull.Value;
row[11] = pas.PassengerPortOfDisembarkation ?? (object)DBNull.Value;
row[12] = pas.PassengerInTransit ?? (object)DBNull.Value;
row[13] = pas.Identifier ?? (object)DBNull.Value;
row[14] = pas.IsDeparture;
row[15] = pas.PassengerIdentityDocumentIssuingState ?? (object)DBNull.Value;
row[16] = pas.PassengerIdentityDocumentExpiryDate ?? (object)DBNull.Value;
row[17] = pas.NotificationSchengen ?? (object)DBNull.Value;
row[18] = pas.NotificationPAX ?? (object)DBNull.Value;
row[19] = pas.EmergencyCare ?? (object)DBNull.Value;
row[20] = pas.EmergencyContactNumber ?? (object)DBNull.Value;
row[21] = pas.PassengerCountryOfBirth ?? (object)DBNull.Value;
result.Rows.Add(row);
}
return result;
}
#endregion
} }
#region class PASD #region class PASD