Пример #1
0
        protected IEnumerable <string> GenerateDeleteScripts(EntityCacheEntry cacheEntry)
        {
            ExtensibleEntity entity = cacheEntry.Entity as ExtensibleEntity;

            return(GenerateDeleteScripts(entity, entity.Metadata));
            //TODO: delete relationships
        }
Пример #2
0
        internal void DetachCacheFromEntity()
        {
            ExtensibleEntity entity = _entity as ExtensibleEntity;

            if (entity != null)
            {
                entity.DetachCache();
            }
        }
Пример #3
0
        internal void AttachCacheToEntity()
        {
            ExtensibleEntity entity = _entity as ExtensibleEntity;

            if (entity != null)
            {
                entity.AttachCache(this, EntityCacheEntry.HandleEntityChanged);
            }
        }
        public void AddObject(IExtensibleEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            AssertNotDisposed();
            this.Cache.Add(entity);
            ExtensibleEntity entityObject = entity as ExtensibleEntity;

            if (entityObject != null)
            {
                //TODO: Add context to relationships
            }
        }
Пример #5
0
        private static IExtensibleEntity CreateEntity(Type entityType, IDataRecord dbReader, ExtensibleObjectContextBase context, bool withExtensionValues)
        {
            ExtensibleEntity entity         = Activator.CreateInstance(entityType) as ExtensibleEntity;
            EntityMetadata   entityMetaData = ExtensibleEntity.GetEntityMetadata(entityType);

            FieldMetadata[] fields = withExtensionValues?entityMetaData.Fields:entityMetaData.PreDefinedFields;
            for (int i = 0; i < fields.Length; i++)
            {
                object value = dbReader[fields[i].Name]; //note: use .Name rather than .ColumnName
                if (value is DBNull)
                {
                    value = null;
                }
                entity.SetValueWithFlag(fields[i].Name, value, true); //don't set the dirty flag
            }
            return(entity);
        }
Пример #6
0
        private static EntityKey CreateEntityKey(Type entityType, IDataRecord dbReader)
        {
            EntityMetadata entityMetadata = ExtensibleEntity.GetEntityMetadata(entityType);

            FieldMetadata[] keyFields = entityMetadata.KeyFields;
            KeyValuePair <string, object>[] keyValuePairs = new KeyValuePair <string, object> [keyFields.Length];
            for (int i = 0; i < keyFields.Length; i++)
            {
                object value = dbReader[keyFields[i].Name];
                if (value is DBNull)
                {
                    value = null;
                }
                keyValuePairs[i] = new KeyValuePair <string, object>(keyFields[i].Name, value);
            }
            return(new EntityKey(entityMetadata, keyValuePairs));
        }
Пример #7
0
        private static string BuildSqlString(Type entityType, string queryClause)
        {
            EntityMetadata entityMetaData = ExtensibleEntity.GetEntityMetadata(entityType);
            StringBuilder  sb             = new StringBuilder();

            sb.Append("SELECT ");
            FieldMetadata[] fields = entityMetaData.PreDefinedFields;
            for (int i = 0; i < fields.Length; i++)
            {
                sb.Append(string.Format("P.{0} AS {1}", fields[i].ColumnName, fields[i].Name));
                if (i != fields.Length - 1)
                {
                    sb.Append(",");
                }
            }
            fields = entityMetaData.ExtensionFields;
            if (fields.Length > 0)
            {
                sb.Append(",");
            }
            for (int i = 0; i < fields.Length; i++)
            {
                sb.Append(string.Format("E{0}.Value AS {1}", i, fields[i].Name));
                if (i != fields.Length - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append(string.Format(" FROM {0} AS P", entityMetaData.SourceName));
            string extensionTableName = string.IsNullOrEmpty(entityMetaData.ExtensionTable) ? Consts.DefExtensionTable : entityMetaData.ExtensionTable;

            if (fields.Length > 0)
            {
                for (int i = 0; i < fields.Length; i++)
                {
                    sb.Append(string.Format(" LEFT OUTER JOIN {0} AS E{1}", extensionTableName, i));
                    sb.Append(string.Format(" ON P.{0} = E{1}.RecordId AND E{1}.FieldId = {2}",
                                            entityMetaData.Key, i, SqlFormat.ToSqlValueString(fields[i].Id)));
                }
            }
            if (!string.IsNullOrEmpty(queryClause))
            {
                sb.Append(" WHERE ").Append(queryClause);
            }
            return(sb.ToString());
        }
Пример #8
0
        public IEnumerable <string> GenerateUpdateScripts(EntityCacheEntry cacheEntry)
        {
            switch (cacheEntry.State)
            {
            case EntityRowState.Added:
                return(GenerateInsertScripts(cacheEntry));

            case EntityRowState.Deleted:
                return(GenerateDeleteScripts(cacheEntry));

            case EntityRowState.Modified:
            {
                ExtensibleEntity entity = cacheEntry.Entity as ExtensibleEntity;
                return(GenerateUpdateScripts(entity, entity.Metadata, entity.GetModifiedFields()));
            }

            default:
                return(null);
            }
        }
Пример #9
0
 public static EntityMetadata GetEntityMetadata(Type entityType)
 {
     return(DataModelWorkspace.Current.GetEntityMetadata(ExtensibleEntity.GetEntityClassName(entityType)));
 }