/// <summary>
        /// Gets the number of pages based on the specified
        /// </summary>
        /// <param name="PageSize">Page size</param>
        /// <param name="Parameters">Parameters to search by</param>
        /// <typeparam name="ObjectType">Object type to get the page count of</typeparam>
        /// <returns>The number of pages that the table contains for the specified page size</returns>
        public int PageCount <ObjectType>(int PageSize = 25, params IParameter[] Parameters)
            where ObjectType : class
        {
            Parameters = Parameters.Check(new IParameter[] { });
            string KeyName = typeof(ObjectType).GetName() + "_PageCount_" + PageSize.ToString(CultureInfo.InvariantCulture) + "_" + Parameters.ToString(x => x.ToString(), "_");

            Parameters.ForEach(x => { KeyName = x.AddParameter(KeyName); });
            if (Cache.ContainsKey(KeyName))
            {
                return((int)Cache[KeyName]);
            }
            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
                if (Mapping != null)
                {
                    int Count = QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType))
                                .PageCount(PageSize, Parameters)
                                .Execute()[0]
                                .FirstOrDefault()
                                .Total;
                    if (Count > 0)
                    {
                        int ReturnValue = (Count / PageSize) + (Count % PageSize > 0 ? 1 : 0);
                        Cache.Add(KeyName, ReturnValue, new string[] { typeof(ObjectType).GetName() });
                        return(ReturnValue);
                    }
                }
            }
            return(0);
        }
Example #2
0
 private void SortMappings()
 {
     foreach (IDatabase Database in Mappings.Keys.OrderBy(x => x.Order))
     {
         var Order = 0;
         var Graph = MapperProvider.GetStructure(Database.GetType());
         foreach (var Mapping in KahnSort(Graph.Copy()).Reverse())
         {
             Mapping.Order = Order;
             ++Order;
         }
     }
 }
        /// <summary>
        /// Returns all items that match the criteria
        /// </summary>
        /// <typeparam name="ObjectType">Type of the object</typeparam>
        /// <param name="Parameters">Parameters used in the where clause</param>
        /// <returns>All items that match the criteria</returns>
        public IEnumerable <ObjectType> All <ObjectType>(params IParameter[] Parameters)
            where ObjectType : class
        {
            Parameters = Parameters.Check(new IParameter[] { });
            var    ReturnValue = new List <Dynamo>();
            string KeyName     = typeof(ObjectType).GetName() + "_All_" + Parameters.ToString(x => x.ToString(), "_");

            Parameters.ForEach(x => { KeyName = x.AddParameter(KeyName); });
            if (Cache.ContainsKey(KeyName))
            {
                return(GetListCached <ObjectType>(ref ReturnValue, KeyName));
            }
            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
                if (Mapping != null)
                {
                    foreach (Dynamo Item in QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType)).All(Parameters).Execute()[0])
                    {
                        var IDProperty = Mapping.IDProperties.FirstOrDefault();
                        CopyOrAdd(ReturnValue, IDProperty, Item);
                    }
                }
            }
            Cache.Add(KeyName, ReturnValue, new string[] { typeof(ObjectType).GetName() });
            return(ConvertValues <ObjectType>(ReturnValue));
        }
 /// <summary>
 /// Saves an object to the database
 /// </summary>
 /// <typeparam name="ObjectType">Object type</typeparam>
 /// <typeparam name="PrimaryKeyType">Primary key type</typeparam>
 /// <param name="Object">Object to save</param>
 public void Save <ObjectType, PrimaryKeyType>(ObjectType Object)
     where ObjectType : class
 {
     Cache.RemoveByTag(typeof(ObjectType).GetName());
     foreach (ISourceInfo Source in SourceProvider.Where(x => x.Writable).OrderBy(x => x.Order))
     {
         IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
         if (Mapping != null)
         {
             var Generator = QueryProvider.Generate <ObjectType>(Source, MapperProvider[typeof(ObjectType), Source], MapperProvider.GetStructure(Mapping.DatabaseConfigType));
             var TempBatch = QueryProvider.Batch(Source);
             CascadeSave <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             TempBatch.Execute();
             TempBatch = QueryProvider.Batch(Source);
             TempBatch.AddCommand(Generator.Save <PrimaryKeyType>(Object));
             TempBatch.Execute();
             TempBatch = QueryProvider.Batch(Source);
             JoinsDelete <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             JoinsSave <ObjectType>(Object, Source, Mapping, TempBatch, new List <object>());
             TempBatch.RemoveDuplicateCommands().Execute();
         }
     }
 }
        /// <summary>
        /// Returns a paged list of items
        /// </summary>
        /// <typeparam name="ObjectType">Object type</typeparam>
        /// <param name="PageSize">Page size</param>
        /// <param name="CurrentPage">Current page (starting with 0)</param>
        /// <param name="OrderBy">The order by portion of the query</param>
        /// <param name="Parameters">Parameters used in the where clause</param>
        /// <returns>A paged list of items that match the criteria</returns>
        public IEnumerable <ObjectType> Paged <ObjectType>(int PageSize = 25, int CurrentPage = 0, string OrderBy = "", params IParameter[] Parameters)
            where ObjectType : class
        {
            Parameters = Parameters.Check(new IParameter[] { });
            string KeyName = typeof(ObjectType).GetName() + "_Paged_" + PageSize.ToString(CultureInfo.InvariantCulture) + "_" + CurrentPage.ToString(CultureInfo.InvariantCulture) + "_" + Parameters.ToString(x => x.ToString(), "_");

            Parameters.ForEach(x => { KeyName = x.AddParameter(KeyName); });
            var ReturnValue = new System.Collections.Generic.List <Dynamo>();

            if (Cache.ContainsKey(KeyName))
            {
                return(GetListCached <ObjectType>(ref ReturnValue, KeyName));
            }
            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
                if (Mapping != null)
                {
                    var IDProperty = Mapping.IDProperties.FirstOrDefault();
                    if (IDProperty != null)
                    {
                        foreach (Dynamo Item in QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType))
                                 .Paged(PageSize, CurrentPage, OrderBy, Parameters)
                                 .Execute()[0])
                        {
                            CopyOrAdd(ReturnValue, IDProperty, Item);
                        }
                    }
                }
            }
            Cache.Add(KeyName, ReturnValue, new string[] { typeof(ObjectType).GetName() });
            return(ConvertValues <ObjectType>(ReturnValue));
        }
        /// <summary>
        /// Loads a property (primarily used internally for lazy loading)
        /// </summary>
        /// <typeparam name="ObjectType">Object type</typeparam>
        /// <typeparam name="DataType">Data type</typeparam>
        /// <param name="Object">Object</param>
        /// <param name="PropertyName">Property name</param>
        /// <returns>The appropriate property value</returns>
        public IList <DataType> LoadProperties <ObjectType, DataType>(ObjectType Object, string PropertyName)
            where ObjectType : class
            where DataType : class
        {
            var ReturnValue = new System.Collections.Generic.List <Dynamo>();

            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
                if (Mapping != null)
                {
                    var Property = Mapping.Properties.FirstOrDefault(x => x.Name == PropertyName);
                    if (Property != null)
                    {
                        foreach (Dynamo Item in QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType))
                                 .LoadProperty <DataType>(Object, Property)
                                 .Execute()[0])
                        {
                            var IDProperty = Property.ForeignMapping.IDProperties.FirstOrDefault();
                            CopyOrAdd(ReturnValue, IDProperty, Item);
                        }
                    }
                }
            }
            if (ReturnValue.Count == 0)
            {
                return(new ObservableList <DataType>());
            }
            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping ObjectMapping = MapperProvider[typeof(ObjectType), Source];
                IMapping Mapping       = MapperProvider[typeof(DataType), Source];
                if (Mapping != null)
                {
                    IProperty ObjectProperty = ObjectMapping == null ? null : ObjectMapping.Properties.FirstOrDefault(x => x.Name == PropertyName);
                    if (ObjectProperty == null)
                    {
                        var        IDProperty = Mapping.IDProperties.FirstOrDefault();
                        IParameter Parameter  = null;
                        int        Counter    = 0;
                        foreach (Dynamo Item in ReturnValue)
                        {
                            if (IDProperty.GetParameter(Item) != null)
                            {
                                Parameter = Parameter == null ? (IParameter) new EqualParameter <object>(IDProperty.GetParameter(Item), Counter.ToString(CultureInfo.InvariantCulture), IDProperty.FieldName, Source.ParameterPrefix) : (IParameter) new OrParameter(Parameter, new EqualParameter <object>(IDProperty.GetParameter(Item), Counter.ToString(CultureInfo.InvariantCulture), IDProperty.FieldName, Source.ParameterPrefix));
                                ++Counter;
                            }
                        }
                        if (Parameter != null)
                        {
                            foreach (Dynamo Item in QueryProvider.Generate <DataType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType)).All(Parameter).Execute()[0])
                            {
                                CopyOrAdd(ReturnValue, IDProperty, Item);
                            }
                        }
                    }
                }
            }
            return(ConvertValues <DataType>(ReturnValue).ToObservableList(x => x));
        }
        /// <summary>
        /// Returns a single item matching the criteria specified
        /// </summary>
        /// <typeparam name="ObjectType">Type of the object</typeparam>
        /// <typeparam name="IDType">ID type for the object</typeparam>
        /// <param name="ID">ID of the object to load</param>
        /// <returns>A single object matching the ID</returns>
        public ObjectType Any <ObjectType, IDType>(IDType ID)
            where ObjectType : class
            where IDType : IComparable
        {
            Dynamo ReturnValue = null;
            string KeyName     = typeof(ObjectType).GetName() + "_Any_" + ID.ToString();

            if (Cache.ContainsKey(KeyName))
            {
                return(GetCached <ObjectType>(ref ReturnValue, KeyName));
            }
            var StringID = ID.ToString();

            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
                if (Mapping != null)
                {
                    var IDProperty = Mapping.IDProperties.FirstOrDefault();
                    if (IDProperty != null)
                    {
                        Dynamo Value = typeof(IDType) == typeof(string) ?
                                       QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType)).Any(new StringEqualParameter(StringID, IDProperty.FieldName, StringID.Length, IDProperty.FieldName, Source.ParameterPrefix)).Execute()[0].FirstOrDefault() :
                                       QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType)).Any(new EqualParameter <IDType>(ID, IDProperty.FieldName, IDProperty.FieldName, Source.ParameterPrefix)).Execute()[0].FirstOrDefault();
                        ReturnValue = CopyOrAssign(ReturnValue, Value);
                    }
                }
            }
            Cache.Add(KeyName, ReturnValue, new string[] { typeof(ObjectType).GetName() });
            return(ConvertValue <ObjectType>(ReturnValue));
        }
        /// <summary>
        /// Returns a single item matching the criteria
        /// </summary>
        /// <typeparam name="ObjectType">Type of the object</typeparam>
        /// <param name="Parameters">Parameters used in the where clause</param>
        /// <returns>A single object matching the criteria</returns>
        public ObjectType Any <ObjectType>(params IParameter[] Parameters)
            where ObjectType : class
        {
            Parameters = Parameters.Check(new IParameter[] { });
            Dynamo ReturnValue = null;
            string KeyName     = typeof(ObjectType).GetName() + "_Any_" + Parameters.ToString(x => x.ToString(), "_");

            Parameters.ForEach(x => { KeyName = x.AddParameter(KeyName); });
            if (Cache.ContainsKey(KeyName))
            {
                return(GetCached <ObjectType>(ref ReturnValue, KeyName));
            }
            foreach (ISourceInfo Source in SourceProvider.Where(x => x.Readable).OrderBy(x => x.Order))
            {
                IMapping Mapping = MapperProvider[typeof(ObjectType), Source];
                if (Mapping != null)
                {
                    Dynamo Value = QueryProvider.Generate <ObjectType>(Source, Mapping, MapperProvider.GetStructure(Mapping.DatabaseConfigType)).Any(Parameters).Execute()[0].FirstOrDefault();
                    ReturnValue = CopyOrAssign(ReturnValue, Value);
                }
            }
            Cache.Add(KeyName, ReturnValue, new string[] { typeof(ObjectType).GetName() });
            return(ConvertValue <ObjectType>(ReturnValue));
        }