示例#1
0
        private ICollection <EntityFieldValue> GetModifiedFieldValues(IReadOnlyEntity entity, Type type)
        {
            EntityInfo                     entityInfo      = CacheManager.GetEntityInfo(type);
            ICollection <IColumn>          typeColumns     = entityInfo.Columns;
            ITypeFieldValueList            currentValues   = OperationUtils.ExtractEntityTypeFieldValues(entity, type);
            ICollection <EntityFieldValue> modifiedColumns = new  List <EntityFieldValue>();

            foreach (IColumn typeColumn in typeColumns)
            {
                if (typeColumn.Key)
                {
                    continue;
                }

                EntityFieldValue classFieldValue    = currentValues.GetFieldValue(typeColumn.AttributeName);
                EntityFieldValue originalFieldValue = entity.Context != null?entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName) : null;

                bool matches = originalFieldValue != null && classFieldValue != null && classFieldValue.Value == originalFieldValue.Value ||
                               (originalFieldValue != null && classFieldValue != null && classFieldValue.Value != null && classFieldValue.Value.Equals(originalFieldValue.Value));
                if (!matches)
                {
                    modifiedColumns.Add(classFieldValue);
                }
            }
            return(modifiedColumns);
        }
示例#2
0
        private static void SetParentRelationFieldsForNonIdentifyingRelations(IEntity parentEntity
                                                                              , IEnumerable <IEntity> childObjects, RelationColumnMapping mapping)
        {
            IReadOnlyEntity       firstObject     = null;
            IEnumerator <IEntity> childEnumerator = childObjects.GetEnumerator();

            if (childEnumerator.MoveNext())
            {
                firstObject = childEnumerator.Current;
            }
            if (firstObject == null)
            {
                return;
            }
            EntityInfo parentInfo = CacheManager.GetEntityInfo(parentEntity);
            EntityInfo childInfo  = CacheManager.GetEntityInfo(firstObject);

            PropertyInfo setter    = null;
            bool         foundOnce = false;

            while (parentInfo != null)
            {
                IColumn parentMatchedColumn = parentInfo.FindColumnByAttribute(mapping.FromField);
                if (parentMatchedColumn != null)
                {
                    foundOnce = true;
                    setter    = parentInfo.GetProperty(parentMatchedColumn);
                }
                parentInfo = parentInfo.SuperEntityInfo;
            }
            if (!foundOnce)
            {
                string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, firstObject.GetType().FullName);
                throw new NoMatchingColumnFoundException(message);
            }

            foundOnce = false;
            while (childInfo != null)
            {
                IColumn childMatchedColumn = childInfo.FindColumnByAttribute(mapping.ToField);

                if (childMatchedColumn != null)
                {
                    foundOnce = true;
                    foreach (IReadOnlyEntity dbObject in childObjects)
                    {
                        ITypeFieldValueList fieldValueList  = OperationUtils.ExtractEntityTypeFieldValues(dbObject, childInfo.EntityType);
                        EntityFieldValue    childFieldValue = fieldValueList.GetFieldValue(childMatchedColumn.AttributeName);
                        setter.SetValue(parentEntity, childFieldValue.Value, null);
                    }
                }
                childInfo = childInfo.SuperEntityInfo;
            }
            if (!foundOnce)
            {
                string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, firstObject.GetType().FullName);
                throw new NoMatchingColumnFoundException(message);
            }
        }
示例#3
0
        protected IDbCommand CreateRetrievalPreparedStatement(ITypeFieldValueList keyValueList, ITransaction tx)
        {
            Type       targetType = keyValueList.Type;
            EntityInfo entityInfo = CacheManager.GetEntityInfo(targetType);
            string     query      = entityInfo.GetLoadQuery(DbLayer);

            IDbCommand cmd;

            try
            {
                cmd             = tx.CreateCommand();
                cmd.CommandText = query;
            }
            catch (Exception ex)
            {
                string message = String.Format("SQL Exception while trying create command for sql {0}", query);
                throw new CommandCreationException(message, ex);
            }

            ICollection <IColumn> keys = entityInfo.GetKeys();

            StringBuilder logSb     = new StringBuilder();
            bool          showQuery = Config.ShowQueries;

            if (showQuery)
            {
                logSb.Append(query);
            }
            int i = 0;

            foreach (IColumn key in keys)
            {
                Object fieldValue = keyValueList.GetFieldValue(key.AttributeName).Value;
                if (showQuery)
                {
                    logSb.Append(" ,").Append(key.ColumnName).Append("=").Append(fieldValue);
                }
                DbLayer.DataManipulate().SetToPreparedStatement(cmd, fieldValue, ++i, key);
            }
            if (showQuery)
            {
                Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString());
            }
            if (Config.EnableStatistics)
            {
                Statistics.RegisterSelect(targetType);
            }
            return(cmd);
        }
示例#4
0
        private bool VersionValidated(IReadOnlyEntity entity, Type type, ITransaction tx)
        {
            EntityInfo            entityInfo  = CacheManager.GetEntityInfo(type);
            ICollection <IColumn> typeColumns = entityInfo.Columns;

            foreach (IColumn typeColumn in typeColumns)
            {
                if (typeColumn.ColumnType == ColumnType.Version)
                {
                    Object           classValue         = ExtractCurrentVersionValue(entity, typeColumn, type, tx);
                    EntityFieldValue originalFieldValue = entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName);
                    return(originalFieldValue != null && classValue == originalFieldValue.Value ||
                           (originalFieldValue != null && classValue != null && classValue.Equals(originalFieldValue.Value)));
                }
            }

            if (entityInfo.TableInfo.UpdateStrategy == UpdateStrategy.ChangedColumns)
            {
                ICollection <EntityFieldValue> modified = GetModifiedFieldValues(entity, type);
                typeColumns = new List <IColumn>();
                foreach (EntityFieldValue fieldValue in modified)
                {
                    typeColumns.Add(fieldValue.Column);
                }
            }

            ITypeFieldValueList fieldValueList = ExtractCurrentRowValues(entity, type, tx);

            if (fieldValueList == null)
            {
                return(false);
            }
            foreach (IColumn typeColumn in typeColumns)
            {
                EntityFieldValue classFieldValue    = fieldValueList.GetFieldValue(typeColumn.AttributeName);
                EntityFieldValue originalFieldValue = entity.Context != null?entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName) : null;

                bool matches = originalFieldValue != null && classFieldValue != null && classFieldValue.Value == originalFieldValue.Value ||
                               (originalFieldValue != null && classFieldValue != null && classFieldValue.Value != null && classFieldValue.Value.Equals(originalFieldValue.Value));
                if (!matches)
                {
                    return(false);
                }
            }
            return(true);
        }
示例#5
0
        private static void SetRelationObjectKeyValues(ITypeFieldValueList valueTypeList, Type entityType, Type childEntityType
                                                       , IEnumerable <IEntity> childObjects, IRelation relation)
        {
            EntityInfo            entityInfo = CacheManager.GetEntityInfo(entityType);
            ICollection <IColumn> columns    = entityInfo.Columns;

            foreach (RelationColumnMapping mapping in relation.TableColumnMappings)
            {
                IColumn          matchColumn = entityInfo.FindColumnByAttribute(mapping.FromField);
                EntityFieldValue fieldValue  = valueTypeList.GetFieldValue(matchColumn.AttributeName);

                if (fieldValue != null)
                {
                    SetChildPrimaryKeys(fieldValue, childEntityType, childObjects, mapping);
                }
                else
                {
                    string message = String.Format("The column {0} does not have a matching column in the object {1}", matchColumn.ColumnName, valueTypeList.Type.FullName);
                    throw new NoMatchingColumnFoundException(message);
                }
            }
        }