Пример #1
0
        private static void GuessPrimaryKey(EntityConfiguration ec, List <string> primaryKeys)
        {
            //check name = ID or GUID column first
            foreach (PropertyConfiguration pc in ec.Properties)
            {
                if (pc.MappingName.ToUpper() == "ID" || pc.MappingName.ToUpper() == "GUID")
                {
                    primaryKeys.Add(pc.MappingName);
                    return;
                }
            }

            //check the first ends with ID or Guid column
            foreach (PropertyConfiguration pc in ec.Properties)
            {
                if (pc.MappingName.ToUpper().EndsWith("ID") || pc.MappingName.ToUpper().EndsWith("GUID"))
                {
                    primaryKeys.Add(pc.MappingName);
                    return;
                }
            }

            //or threat the first column as DEFAULT_KEY column
            primaryKeys.Add(ec.Properties[0].MappingName);
        }
Пример #2
0
        /// <summary>
        /// Gets the modified properties of this entity, not including query properties and properties of base entity's.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The modified properties</returns>
        public Dictionary <string, object> GetModifiedProperties(Type type)
        {
            EntityConfiguration ec = MetaDataManager.GetEntityConfiguration(type.ToString());

            if (ec.BaseEntity == null && type == this.GetType())
            {
                return(changedProperties);
            }

            Check.Require(typeof(Entity).IsAssignableFrom(type), "type must be an Entity");

            List <string> toRemoveItems = new List <string>();

            foreach (string item in changedProperties.Keys)
            {
                PropertyConfiguration pc = ec.GetPropertyConfiguration(item);
                if (pc == null || pc.IsInherited || pc.IsPrimaryKey)
                {
                    toRemoveItems.Add(item);
                }
            }
            Dictionary <string, object> retProperties = new Dictionary <string, object>();

            foreach (string item in changedProperties.Keys)
            {
                if (!toRemoveItems.Contains(item))
                {
                    retProperties.Add(item, changedProperties[item]);
                }
            }
            return(retProperties);
        }
Пример #3
0
        /// <summary>
        /// Gets the create property mapping column names.
        /// </summary>
        /// <param name="ec">The ec.</param>
        /// <returns>Column names</returns>
        public static string[] GetCreatePropertyMappingColumnNames(EntityConfiguration ec)
        {
            List <string> insertColumnNames = new List <string>();

            foreach (PropertyConfiguration pc in ec.Properties)
            {
                if ((!(pc.IsReadOnly && ec.BaseEntity == null)) && ((!pc.IsQueryProperty) || (pc.QueryType == "FkReverseQuery")) && ((!pc.IsInherited) || pc.IsPrimaryKey))
                {
                    insertColumnNames.Add(pc.MappingName);
                }
            }

            return(insertColumnNames.ToArray());
        }
Пример #4
0
        /// <summary>
        /// Gets the property mapping column names.
        /// </summary>
        /// <param name="ec">The ec.</param>
        /// <returns>Column names</returns>
        public static string[] GetPropertyMappingColumnNames(EntityConfiguration ec)
        {
            List <string> columnNames = new List <string>();

            foreach (PropertyConfiguration pc in ec.Properties)
            {
                if ((!pc.IsQueryProperty) || (pc.QueryType == "FkReverseQuery"))
                {
                    columnNames.Add(pc.MappingName);
                }
            }

            return(columnNames.ToArray());
        }
Пример #5
0
        /// <summary>
        /// Called when removed item from query property.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="item">The item.</param>
        protected void OnQueryPropertyItemRemove(string propertyName, object item)
        {
            Check.Require(item != null, "item to remove could not be null.");

            bool isLoadedBefore = IsQueryPropertyLoaded(propertyName);

            SetPropertyLoaded(propertyName);

            if (!isLoadedBefore)
            {
                return;
            }

            EntityConfiguration   ec = GetEntityConfiguration();
            PropertyConfiguration pc = ec.GetPropertyConfiguration(propertyName);

            if ((!(pc.IsContained || pc.QueryType == "ManyToManyQuery")))
            {
                return;
            }

            bool isInSaveList = false;

            lock (toSaveRelatedPropertyObjects)
            {
                if (toSaveRelatedPropertyObjects.ContainsKey(propertyName) && toSaveRelatedPropertyObjects[propertyName].Contains(item))
                {
                    toSaveRelatedPropertyObjects[propertyName].Remove(item);
                    isInSaveList = true;
                }
            }

            if (!isInSaveList)
            {
                lock (toDeleteRelatedPropertyObjects)
                {
                    if (!toDeleteRelatedPropertyObjects.ContainsKey(propertyName))
                    {
                        toDeleteRelatedPropertyObjects.Add(propertyName, new List <object>());
                    }

                    if (!toDeleteRelatedPropertyObjects[propertyName].Contains(item))
                    {
                        toDeleteRelatedPropertyObjects[propertyName].Add(item);
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Queries array of the specified return entity type.
        /// </summary>
        /// <param name="returnEntityType">Type of the return entity.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="baseEntity">The base entity.</param>
        /// <returns>The query result.</returns>
        protected object Query(Type returnEntityType, string propertyName, Entity baseEntity)
        {
            if (onQuery != null)
            {
                EntityConfiguration   ec = baseEntity.GetEntityConfiguration();
                PropertyConfiguration pc = ec.GetPropertyConfiguration(propertyName);

                try
                {
                    return(onQuery(returnEntityType, propertyName, pc.QueryWhere, pc.QueryOrderBy, baseEntity));
                }
                catch
                {
                    onQuery = null;
                }
            }
            return(null);
        }
Пример #7
0
        /// <summary>
        /// Gets the primary key mapping column names.
        /// </summary>
        /// <param name="ec">The ec.</param>
        /// <returns></returns>
        public static string[] GetPrimaryKeyMappingColumnNames(EntityConfiguration ec)
        {
            List <string> primaryKeys = new List <string>();

            foreach (PropertyConfiguration pc in ec.Properties)
            {
                if (pc.IsPrimaryKey)
                {
                    primaryKeys.Add(pc.MappingName);
                }
            }

            if (primaryKeys.Count == 0)
            {
                //take the most possible column as a single primary DEFAULT_KEY
                GuessPrimaryKey(ec, primaryKeys);
            }

            return(primaryKeys.ToArray());
        }
Пример #8
0
        /// <summary>
        /// Called when query property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="oldValues">The old values.</param>
        /// <param name="newValues">The new values.</param>
        protected void OnQueryPropertyChanged(string propertyName, object oldValues, object newValues)
        {
            bool isLoadedBefore = IsQueryPropertyLoaded(propertyName);

            SetPropertyLoaded(propertyName);

            if (newValues != null)
            {
                BindArrayListEventHandlers(propertyName, newValues);
            }

            if (!isLoadedBefore)
            {
                return;
            }

            EntityConfiguration   ec = GetEntityConfiguration();
            PropertyConfiguration pc = ec.GetPropertyConfiguration(propertyName);

            if ((!(pc.IsContained || pc.QueryType == "ManyToManyQuery")) || oldValues == newValues)
            {
                return;
            }

            if (oldValues != null)
            {
                //lock (toSaveRelatedPropertyObjects)
                //{
                //    if (toSaveRelatedPropertyObjects.ContainsKey(propertyName))
                //    {
                //        foreach (object oldValue in (IEnumerable)oldValues)
                //        {
                //            if (toSaveRelatedPropertyObjects[propertyName].Contains(oldValue))
                //            {
                //                toSaveRelatedPropertyObjects[propertyName].Remove(oldValue);
                //            }
                //        }
                //    }
                //}
                //lock (toDeleteRelatedPropertyObjects)
                //{
                //    if (!toDeleteRelatedPropertyObjects.ContainsKey(propertyName))
                //    {
                //        toDeleteRelatedPropertyObjects.Add(propertyName, new List<object>());
                //    }

                //    foreach (object oldValue in (IEnumerable)oldValues)
                //    {
                //        if (!toDeleteRelatedPropertyObjects[propertyName].Contains(oldValue))
                //        {
                //            toDeleteRelatedPropertyObjects[propertyName].Add(oldValue);
                //        }
                //    }
                //}

                foreach (object oldValue in (IEnumerable)oldValues)
                {
                    OnQueryPropertyItemRemove(propertyName, oldValue);
                }
            }

            if (newValues != null)
            {
                //lock (toDeleteRelatedPropertyObjects)
                //{
                //    if (toDeleteRelatedPropertyObjects.ContainsKey(propertyName))
                //    {
                //        foreach (object newValue in (IEnumerable)newValues)
                //        {
                //            if (toDeleteRelatedPropertyObjects[propertyName].Contains(newValue))
                //            {
                //                toDeleteRelatedPropertyObjects[propertyName].Remove(newValue);
                //            }
                //        }
                //    }
                //}
                //lock (toSaveRelatedPropertyObjects)
                //{
                //    if (!toSaveRelatedPropertyObjects.ContainsKey(propertyName))
                //    {
                //        toSaveRelatedPropertyObjects.Add(propertyName, new List<object>());
                //    }

                //    foreach (object newValue in (IEnumerable)newValues)
                //    {
                //        if (!toSaveRelatedPropertyObjects[propertyName].Contains(newValue))
                //        {
                //            toSaveRelatedPropertyObjects[propertyName].Add(newValue);
                //        }
                //    }
                //}

                foreach (object newValue in (IEnumerable)newValues)
                {
                    OnQueryPropertyItemAdd(propertyName, newValue);
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Called when query one property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="oldValue">The old value.</param>
        /// <param name="newValue">The new value.</param>
        protected void OnQueryOnePropertyChanged(string propertyName, object oldValue, object newValue)
        {
            bool isLoadedBefore = IsQueryPropertyLoaded(propertyName);

            SetPropertyLoaded(propertyName);

            if (!isLoadedBefore)
            {
                return;
            }

            if (oldValue == newValue)
            {
                return;
            }

            EntityConfiguration   ec = GetEntityConfiguration();
            PropertyConfiguration pc = ec.GetPropertyConfiguration(propertyName);

            if ((!(pc.IsContained || pc.QueryType == "ManyToManyQuery")))
            {
                return;
            }

            if (oldValue != null)
            {
                //lock (toSaveRelatedPropertyObjects)
                //{
                //    if (toSaveRelatedPropertyObjects.ContainsKey(propertyName) && toSaveRelatedPropertyObjects[propertyName].Contains(oldValue))
                //    {
                //        toSaveRelatedPropertyObjects[propertyName].Remove(oldValue);
                //    }
                //}
                //lock (toDeleteRelatedPropertyObjects)
                //{
                //    if (!toDeleteRelatedPropertyObjects.ContainsKey(propertyName))
                //    {
                //        toDeleteRelatedPropertyObjects.Add(propertyName, new List<object>());
                //    }
                //    if (!toDeleteRelatedPropertyObjects[propertyName].Contains(oldValue))
                //    {
                //        toDeleteRelatedPropertyObjects[propertyName].Add(oldValue);
                //    }
                //}

                OnQueryPropertyItemRemove(propertyName, oldValue);
            }

            if (newValue != null)
            {
                //lock (toDeleteRelatedPropertyObjects)
                //{
                //    if (toDeleteRelatedPropertyObjects.ContainsKey(propertyName) && toDeleteRelatedPropertyObjects[propertyName].Contains(newValue))
                //    {
                //        toDeleteRelatedPropertyObjects[propertyName].Remove(newValue);
                //    }
                //}
                //lock (toSaveRelatedPropertyObjects)
                //{
                //    if (!toSaveRelatedPropertyObjects.ContainsKey(propertyName))
                //    {
                //        toSaveRelatedPropertyObjects.Add(propertyName, new List<object>());
                //    }
                //    if (!toSaveRelatedPropertyObjects[propertyName].Contains(newValue))
                //    {
                //        toSaveRelatedPropertyObjects[propertyName].Add(newValue);
                //    }
                //}

                OnQueryPropertyItemAdd(propertyName, newValue);
            }
        }