Skip to content

Commit

Permalink
Moving Some DatabaseEntity Code to GenericDataEntity
Browse files Browse the repository at this point in the history
Signed-off-by: Saranga Athukorale <[email protected]>
  • Loading branch information
sathukorale committed Jan 30, 2016
1 parent cadb6ad commit c4da58d
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 163 deletions.
123 changes: 117 additions & 6 deletions libDatabaseHelper/classes/generic/GenericDatabaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,142 @@ public GenericDatabaseEntity()
ResetValues();
}

public virtual bool Add()
public bool Add()
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
ExistCondition condition;
if (((condition = Exist(false)) & ExistCondition.None) != ExistCondition.None)
{
throw new DatabaseException((DatabaseException.ErrorType)condition, condition);
}

var result = GetColumns(true);
if (result == null || !result.GetOtherColumns().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoColumnsFound);
}

var connection = GenericConnectionManager.GetConnectionManager(_supportedDatabase).GetConnection(_entityType);
var command = connection.CreateCommand();

if (OnAdd(command) == false)
{
return false;
}

if ((this is AuditEntry) == false && (this is GenericConnectionDetails) == false)
{
AuditEntry.AddAuditEntry(this, "Added ");
_OnDatabaseEntityUpdated(this, GetType(), UpdateEventType.Add);
}

return true;
}

public virtual bool Update()
public bool Update()
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
ExistCondition condition;
if ((condition = Exist()) != ExistCondition.RecordExists)
{
throw new DatabaseException((DatabaseException.ErrorType)condition, condition);
}

var result = GetColumns(true);
if (result == null || !result.GetOtherColumns().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoColumnsFound);
}
if (!result.GetPrimaryKeys().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound);
}

var connection = GenericConnectionManager.GetConnectionManager(_supportedDatabase).GetConnection(_entityType);
var command = connection.CreateCommand();

if (OnUpdate(command) == false)
{
return false;
}

if ((this is AuditEntry) == false && (this is GenericConnectionDetails) == false)
{
AuditEntry.AddAuditEntry(this, "Updated ");
_OnDatabaseEntityUpdated(this, GetType(), UpdateEventType.Update);
}

return true;
}

public virtual bool Remove()
public bool Remove()
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
ExistCondition condition;
if (((condition = Exist()) & ExistCondition.RecordExists) == 0)
{
throw new DatabaseException(DatabaseException.ErrorType.NonExistingRecord, condition);
}
if (Relationship.CheckReferences(this))
{
throw new DatabaseException(DatabaseException.ErrorType.ReferenceKeyViolation, condition);
}
var result = GetColumns();
if (result == null || !result.GetOtherColumns().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoColumnsFound);
}
if (!result.GetPrimaryKeys().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound);
}

if (IsReferenced())
{
throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound);
}

var connection = GenericConnectionManager.GetConnectionManager(_supportedDatabase).GetConnection(_entityType);
var command = connection.CreateCommand();

if (OnRemove(command) == false)
{
return false;
}

if ((this is AuditEntry) == false && (this is GenericConnectionDetails) == false)
{
AuditEntry.AddAuditEntry(this, "Removed ");
_OnDatabaseEntityUpdated(this, GetType(), UpdateEventType.Remove);
}

return true;
}

public ExistCondition Exist()
{
return Exist(true);
}

#region "Methods to Override"
public virtual ExistCondition Exist(bool onlyCheck)
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
}

protected virtual bool OnAdd(DbCommand command)
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
}

protected virtual bool OnUpdate(DbCommand command)
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
}

protected virtual bool OnRemove(DbCommand command)
{
throw new NotImplementedException("The class of type GenericDatabaseEntity is not intended for direct use and should be extended by a fellow DatabaseEntity");
}
#endregion

public bool IsReferenced()
{
var databaseManager = GenericDatabaseManager.GetDatabaseManager(_supportedDatabase);
Expand Down
86 changes: 86 additions & 0 deletions libDatabaseHelper/classes/generic/Relationship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace libDatabaseHelper.classes.generic
{
public struct Relationship
{
private readonly List<Type> _referencingClasses;
private readonly Type _referencedClass;

private static Dictionary<Type, Relationship> _relationships = new Dictionary<Type, Relationship>();

private Relationship(Type referencedClass)
{
_referencedClass = referencedClass;
_referencingClasses = new List<Type>();
}

private void AddReference(Type referencingClass)
{
if (referencingClass != null & !_referencingClasses.Contains(referencingClass))
{
_referencingClasses.Add(referencingClass);
}
}

public static void Add(Type referencedType, Type referencingType)
{
if (referencedType == null || referencingType == null)
return;

if (!_relationships.ContainsKey(referencedType))
{
var relationship = new Relationship(referencedType);
relationship.AddReference(referencingType);

_relationships.Add(referencedType, relationship);
}
else
{
_relationships[referencedType].AddReference(referencingType);
}
}

public static bool CheckReferences(GenericDatabaseEntity entity)
{
if (entity == null) return false;

var referencedType = entity.GetType();
if (!_relationships.ContainsKey(referencedType)) return false;

var references = _relationships[referencedType]._referencingClasses;

foreach (var reference in references.Where(reference => reference != null))
{
var fieldReferencedBy = reference.GetFields().Where(i => i.GetCustomAttributes(typeof(TableColumn), true).Any()).ToList();
var filters = new List<Selector>();

foreach (var fieldInfo in fieldReferencedBy)
{
var attr = (TableColumn)fieldInfo.GetCustomAttributes(typeof(TableColumn), true)[0];
if (attr != null && attr.ReferencedClass == referencedType)
{
filters.Add(new Selector(fieldInfo.Name, referencedType.GetField(attr.ReferencedField).GetValue(entity)));
}
}

if (filters.Count <= 0)
continue;

var command = GenericConnectionManager.GetConnectionManager(entity.GetSupportedDatabaseType()).GetConnection(entity.GetType()).CreateCommand();
var selectQuery = "SELECT COUNT(*) FROM " + reference.Name + " WHERE " + filters.Aggregate("", (currentStr, item) => currentStr + (currentStr == "" ? "" : " OR ") + item.SetToCommand(ref command));
command.CommandText = selectQuery;

int result;
if (Int32.TryParse(command.ExecuteScalar().ToString(), out result) && result > 0)
{
return true;
}
}
return false;
}
}
}
86 changes: 9 additions & 77 deletions libDatabaseHelper/classes/sqlce/DatabaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using libDatabaseHelper.classes.generic;
using libDatabaseHelper.classes.sqlce;
using libDatabaseHelper.classes.sqlce.entities;
using System.Data.Common;

namespace libDatabaseHelper.classes.sqlce
{
Expand All @@ -27,24 +28,13 @@ protected override DatabaseType FetchDatbaseType()
return DatabaseType.SqlCE;
}

public override bool Add()
protected override bool OnAdd(DbCommand command)
{
ExistCondition condition;
if (((condition = Exist(false)) & ExistCondition.None) != ExistCondition.None)
{
throw new DatabaseException((DatabaseException.ErrorType) condition, condition);
}
var result = GetColumns(true);
if (result == null || !result.GetOtherColumns().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoColumnsFound);
}

var connection = GenericConnectionManager.GetConnectionManager(_supportedDatabase).GetConnection(_entityType);
var command = connection.CreateCommand();
var paramString = "";
var valueString = "";
FieldInfo autogenField = null;
var result = GetColumns(true);

foreach (var field in result.GetOtherColumns().Where(i => ! ((TableColumn)i.GetCustomAttributes(typeof(TableColumn), true)[0]).IsAutogenerated))
{
valueString += (valueString == "" ? "" : ", ") + field.Name;
Expand All @@ -70,36 +60,15 @@ public override bool Add()
if (autogenField != null)
autogenField.SetValue(this, GenericFieldTools.ConvertToType(autogenField.FieldType, valReturned));

if ((this is AuditEntry) == false && (this is GenericConnectionDetails) == false)
{
AuditEntry.AddAuditEntry(this, "Added ");
base._OnDatabaseEntityUpdated(this, GetType(), UpdateEventType.Add);
}

return true;
}

public override bool Update()
protected override bool OnUpdate(DbCommand command)
{
ExistCondition condition;
if ((condition = Exist()) != ExistCondition.RecordExists)
{
throw new DatabaseException((DatabaseException.ErrorType) condition, condition);
}
var result = GetColumns(true);
if (result == null || !result.GetOtherColumns().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoColumnsFound);
}
if (!result.GetPrimaryKeys().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound);
}

var connection = GenericConnectionManager.GetConnectionManager(_supportedDatabase).GetConnection(_entityType);
var command = connection.CreateCommand();
var valueParamString = "";
var primaryValueParamString = "";
var result = GetColumns(true);

foreach (var field in result.GetOtherColumns())
{
if (result.GetPrimaryKeys().Contains(field))
Expand Down Expand Up @@ -133,44 +102,13 @@ public override bool Update()
if (command.ExecuteNonQuery() <= 0)
return false;

if ((this is AuditEntry) == false && (this is GenericConnectionDetails) == false)
{
AuditEntry.AddAuditEntry(this, "Updated ");
base._OnDatabaseEntityUpdated(this, GetType(), UpdateEventType.Update);
}

return true;
}

public override bool Remove()
protected override bool OnRemove(DbCommand command)
{
ExistCondition condition;
if (((condition = Exist()) & ExistCondition.RecordExists) == 0)
{
throw new DatabaseException(DatabaseException.ErrorType.NonExistingRecord, condition);
}
if (Relationship.CheckReferences(this))
{
throw new DatabaseException(DatabaseException.ErrorType.ReferenceKeyViolation, condition);
}
var result = GetColumns();
if (result == null || !result.GetOtherColumns().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoColumnsFound);
}
if (!result.GetPrimaryKeys().Any())
{
throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound);
}

if (IsReferenced())
{
throw new DatabaseException(DatabaseException.ErrorType.NoPrimaryKeyColumnsFound);
}

var connection = GenericConnectionManager.GetConnectionManager(_supportedDatabase).GetConnection(_entityType);
var command = connection.CreateCommand();
var primaryValueParamString = "";
var result = GetColumns();

foreach (var field in result.GetPrimaryKeys())
{
Expand All @@ -188,12 +126,6 @@ public override bool Remove()
if (command.ExecuteNonQuery() <= 0)
return false;

if ((this is AuditEntry) == false && (this is GenericConnectionDetails) == false)
{
AuditEntry.AddAuditEntry(this, "Removed ");
base._OnDatabaseEntityUpdated(this, GetType(), UpdateEventType.Remove);
}

return true;
}

Expand Down
Loading

0 comments on commit c4da58d

Please sign in to comment.