/// <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); } }
/// <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); } }
/// <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; } }
/// <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); } }
/// <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); } }