Exemplo n.º 1
0
 public static AuditEntry AddAuditEntry(GenericDatabaseEntity entity, string message)
 {
     var entry = new AuditEntry
     {
         Message = message + " : " + entity.ToContentString(),
         TimeOfOccurence = DateTime.Now
     };
     try
     {
         entry.Add();
     }
     catch (Exception ex) { Console.WriteLine(ex.Message); }
     return entry;
 }
Exemplo n.º 2
0
        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;
        }