Example #1
0
        /// <summary>
        /// Deletes this item
        /// </summary>
        /// <returns></returns>
        /// <exception cref="AttributeNotFoundException">Class attribute parameter 'Mutable' must be seted to 'true'</exception>
        public virtual bool Delete()
        {
            ActiveRecordAttribute attribute = null;

            CheckTypeAtrribute(this.GetType(), ref attribute);

            if (!attribute.Mutable)
            {
                throw new AttributeNotFoundException("Class attribute parameter 'Mutable' must be seted to 'true'");
            }

            if (!ActiveRecordMaster.Delete(this.GetType(), this))
            {
                return(false);
            }

            //Cascade Delete
            if (ActiveRecordMaster.HasJoinedBase(this.GetType()) != null)
            {
                if (ActiveRecordMaster.GetAttribute(this.GetType().BaseType) != null)
                {
                    if (!ActiveRecordMaster.Delete(this.GetType().BaseType, this))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Finds an item by primary key.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static ActiveRecordBase Find(Type type, object id)
        {
            CheckTypeAtrribute(type);

            try
            {
                PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(type);

                if (pk == null)
                {
                    return(null);
                }

                Criteria criteria = Criteria.For(type);
                criteria.SQLString = ActiveRecordMaster.MakePrimaryKeySelect(type);

                criteria.AddParameter("@CODE", pk.CurrentPropertyInfo.Name, id);

                List <ActiveRecordBase> list = ActiveRecordMaster.ExecuteQueryCriteria(type, criteria);

                if (list != null && list.Count > 0)
                {
                    return(list[0]);
                }

                return(null);
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindAll(type: [{0}])", type), ex);
                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// Lazy load a property (HasMany).
        /// </summary>
        /// <typeparam name="TPropertyClass">The type of the property class.</typeparam>
        /// <param name="attr">The attr.</param>
        /// <returns></returns>
        /// <exception cref="AttributeNotFoundException"></exception>
        private List <TPropertyClass> LazyLoadMany <TPropertyClass>(HasManyAttribute attr)
            where TPropertyClass : ActiveRecordBase
        {
            ActiveRecordAttribute attribute = ActiveRecordMaster.GetAttribute(this.GetType());

            if (attribute == null)
            {
                throw new AttributeNotFoundException();
            }

            try
            {
                PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(this.GetType());

                if (pk == null)
                {
                    return(null);
                }

                Criteria criteria = Criteria.For <TPropertyClass>();
                criteria.SQLString = ActiveRecordMaster.MakeHasManySelect(this.GetType(), attr.CurrentPropertyInfo.Name);

                criteria.AddParameter("@CODE", pk.CurrentPropertyInfo.Name, pk.CurrentPropertyInfo.GetValue(this, null));

                return(ActiveRecordMaster.ExecuteQueryCriteria <TPropertyClass>(criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindByForeignKey(attr: [{0}])", attr), ex);
                return(null);
            }
        }
Example #4
0
        /// <summary>
        /// Checks the type atrribute.
        /// </summary>
        /// <param name="type">The type.</param>
        public static void CheckTypeAtrribute(Type type)
        {
            ActiveRecordAttribute attribute = ActiveRecordMaster.GetAttribute(type);
            PrimaryKeyAttribute   pk        = ActiveRecordMaster.GetPrimaryKey(type);

            CheckTypeAtrribute(type, ref attribute, ref pk);
        }
Example #5
0
 /// <summary>
 /// Removes the data.
 /// </summary>
 static void RemoveData()
 {
     Output("Removing data");
     ActiveRecordMaster.ExecuteNonQuery("DELETE FROM montyTDocument");
     ActiveRecordMaster.ExecuteNonQuery("DELETE FROM montyTPerson");
     ActiveRecordMaster.ExecuteNonQuery("DELETE FROM montyTJob");
     Output("Data removed");
 }
Example #6
0
        /// <summary>
        /// Creates a person via SQL.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="birthday">The birthday.</param>
        /// <param name="currentJob">The current job.</param>
        /// <returns></returns>
        static Person CreatePersonSQL(string name, DateTime?birthday, Job currentJob)
        {
            string sql = string.Format("INSERT INTO montyTPerson (Name, Birthday, CurrentJob) VALUES ('{0}', null, {1});SELECT last_insert_id() AS 'Identity';", name, currentJob.Id);

            ActiveRecordMaster.ExecuteNonQuery(sql);

            return(null);
        }
Example #7
0
        /// <summary>
        /// Lazy load a property (HasMany).
        /// </summary>
        /// <typeparam name="TPropertyClass">The type of the property class.</typeparam>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        protected List <TPropertyClass> LazyLoadMany <TPropertyClass>(string property)
            where TPropertyClass : ActiveRecordBase
        {
            HasManyAttribute attr = ActiveRecordMaster.GetHasMany(this.GetType(), property);

            List <TPropertyClass> items = this.LazyLoadMany <TPropertyClass>(attr);

            return(items);
        }
Example #8
0
        /// <summary>
        /// Gets the id.
        /// </summary>
        /// <returns></returns>
        public object GetId()
        {
            PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(this.GetType());

            if (pk == null)
            {
                return(null);
            }

            return(pk.GetValue(this));
        }
Example #9
0
        /// <summary>
        /// Sets the id.
        /// </summary>
        /// <param name="id">The id.</param>
        public void SetId(object id)
        {
            PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(this.GetType());

            if (pk == null)
            {
                return;
            }

            pk.SetValue(this, id);
        }
Example #10
0
        /// <summary>
        /// Check if the criteria return results.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static bool Exists(Type type, object id)
        {
            PrimaryKeyAttribute pk = ActiveRecordMaster.GetPrimaryKey(type);

            Criteria criteria = Criteria.For(type);

            criteria
            .AddFilter(criteria.Eq(pk.CurrentPropertyInfo.Name, id));

            return(ActiveRecordBase.Exists(type, criteria));
        }
Example #11
0
        /// <summary>
        /// Customs the non query criteria.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        /// <exception cref="AttributeNotFoundException"></exception>
        public static bool CustomNonQueryCriteria(Type type, Criteria criteria)
        {
            CheckTypeAtrribute(type);

            try
            {
                //Commit Filters
                criteria.CommitFilters();

                return(ActiveRecordMaster.ExecuteNonQueryCriteria(type, criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.CustomNonQueryCriteria(type: [{0}], criteria: [{1}])", type, criteria), ex);
                return(false);
            }
        }
Example #12
0
        /// <summary>
        /// Customs the find all.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="criteria">The criteria.</param>
        /// <param name="customSQL">The custom SQL.</param>
        /// <returns></returns>
        /// <exception cref="AttributeNotFoundException"></exception>
        public static List <ActiveRecordBase> CustomFindAll(Type type, Criteria criteria, string customSQL)
        {
            CheckTypeAtrribute(type);

            try
            {
                criteria.SQLString = customSQL;

                //Commit Filters
                criteria.CommitFilters();

                return(ActiveRecordMaster.ExecuteQueryCriteria(type, criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.CustomFindAll(type: [{0}], customSQL: [{1}])", type, customSQL), ex);
                return(null);
            }
        }
Example #13
0
        /// <summary>
        /// Finds all.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public static List <ActiveRecordBase> FindAll(Type type, Criteria criteria)
        {
            CheckTypeAtrribute(type);

            try
            {
                criteria.SQLString = ActiveRecordMaster.MakeFullSelect(type);

                //Commit Filters
                criteria.CommitFilters();

                return(ActiveRecordMaster.ExecuteQueryCriteria(type, criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindAll(type: [{0}])", type), ex);
                throw ex;
            }
        }
Example #14
0
        /// <summary>
        /// Returns only a selected field of all itens on criteria
        /// </summary>
        /// <typeparam name="TProjectionType">The type of the projection type.</typeparam>
        /// <param name="type">The type.</param>
        /// <param name="criteria">The criteria.</param>
        /// <param name="field">The field.</param>
        /// <returns></returns>
        public static List <TProjectionType> ProjectionFindAll <TProjectionType>(Type type, Criteria criteria, string field)
        {
            CheckTypeAtrribute(type);

            try
            {
                criteria.SQLString = ActiveRecordMaster.MakeProjectionFullSelect(type, field);

                //Commit Filters
                criteria.CommitFilters();

                return(ActiveRecordMaster.ExecuteProjectionQueryCriteria <TProjectionType>(criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.FindAll(type: [{0}])", type), ex);
                return(null);
            }
        }
Example #15
0
        /// <summary>
        /// Return a portion of the itens on the criteria.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="criteria">The criteria.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numberOfPages">The number of pages.</param>
        /// <param name="numberOfItems">The number of items.</param>
        /// <param name="findNumberOfPages">if set to <c>true</c> [find number of pages].</param>
        /// <returns></returns>
        /// <exception cref="AttributeNotFoundException"></exception>
        public static List <ActiveRecordBase> SlicedFindAll(Type type, Criteria criteria, int currentPage, int pageSize, out int numberOfPages, out int numberOfItems, bool findNumberOfPages)
        {
            CheckTypeAtrribute(type);

            try
            {
                if (findNumberOfPages)
                {
                    //Count
                    float count = ActiveRecordBase.Count(type, criteria);
                    numberOfItems = (int)count;

                    count         = ((float)count / pageSize);
                    numberOfPages = (count) != ((int)count) ? ((int)count) + 1 : ((int)count);
                }
                else
                {
                    numberOfItems = -1;
                    numberOfPages = -1;
                }
                //Query
                criteria.IncludeOrderBy = true;
                criteria.SQLString      = ActiveRecordMaster.MakeFullSelect(type);
                criteria.CommitFilters();
                //.CommitSlicedFilters((currentPage - 1) * pageSize, pageSize);
                //MakePaginator

                criteria.MakePaginator(currentPage, pageSize);

                return(ActiveRecordMaster.ExecuteQueryCriteria(type, criteria));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.SlicedFindAll(type: [{0}])", type), ex);

                numberOfItems = 0;
                numberOfPages = 0;

                return(null);
            }
        }
Example #16
0
        /// <summary>
        /// Return the number of items
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public static int Count(Type type, Criteria criteria)
        {
            CheckTypeAtrribute(type);

            try
            {
                criteria.SQLString = ActiveRecordMaster.MakeCountSelect(type);

                //Commit Filters
                criteria.IncludeOrderBy = false;
                criteria.CommitFilters();

                object count = ActiveRecordMaster.ExecuteScalarQueryCriteria(type, criteria);
                return(int.Parse(count.ToString()));
            }
            catch (Exception ex)
            {
                LogManager.Log("Monty.ActiveRecord", LogCategory.Error, String.Format("Error when tries to execute ActiveRecordBase.Count(type: [{0}])", type), ex);
                return(0);
            }
        }
Example #17
0
        /// <summary>
        /// Lazy load a property (BelongsTo).
        /// </summary>
        /// <typeparam name="TPropertyClass">The type of the property class.</typeparam>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        protected TPropertyClass LazyLoad <TPropertyClass>(string property)
            where TPropertyClass : ActiveRecordBase
        {
            if (!this.LazyObjects.ContainsKey(property))
            {
                return(null);
            }

            BelongsToAttribute attr = ActiveRecordMaster.GetBelongsTo(this.GetType(), property);

            if (this.LazyObjects[property] == null || String.IsNullOrEmpty(this.LazyObjects[property].ToString()))
            {
                return(null);
            }

            TPropertyClass item = ActiveRecordBase <TPropertyClass> .Find(this.LazyObjects[property]);

            if (attr.NotFoundBehaviour == NotFoundBehaviour.Exception && item == null)
            {
                throw new ObjectNotFoundException(typeof(TPropertyClass).Name);
            }

            return(item);
        }