/// <inheritdoc /> public IQuery <T> QueryAll(ISession session, OrderCritia <T> orderBy) { Expect.IsNotNull(session, nameof(session)); var fieldNames = string.Join(", ", Mapper.FieldMappers.Select(x => x.FieldName)); var sql = string.Format("select {0} from {1}", fieldNames, Mapper.TableName); sql = AppendOrder(sql, orderBy); return(session.CreateQuery(sql, Mapper)); }
/// <summary> /// Get field from order critia /// </summary> /// <param name="critia"></param> /// <returns></returns> protected IFieldMapper <T> GetOrderField(OrderCritia <T> critia) { Expect.IsNotNull(critia, nameof(critia)); var orderProp = ExpressionBuilder.GetProperty(critia.Selector); var field = Mapper.FieldMappers.FirstOrDefault(x => x.PropertyName == orderProp.Name); Expect.That(field != null, string.Format("Could not found order property {1} in entity {0}", typeof(T).Name, orderProp.Name)); return(field); }
/// <inheritdoc /> public PagedCollection <TEntity> QueryPaged(string baseSql, OrderCritia <TEntity> orderField, QueryDto qdata) { Expect.IsNotBlank(baseSql, nameof(baseSql)); Expect.IsNotNull(orderField, nameof(orderField)); Expect.IsNotNull(qdata, nameof(qdata)); var result = new PagedCollection <TEntity>(); result.SetPagination(qdata); var queryPair = GetQueryBuilder().CreatePagedQueryTuple(Session, baseSql, orderField, qdata); result.Total = queryPair.Item1.ExecInt(); result.Data = queryPair.Item2.GetAll(); return(result); }
/// <inheritdoc /> public IQuery <T> GetAll(ISession session, OrderCritia <T> orderField) { var fieldNames = string.Join(", ", Mapper.FieldMappers.Select(x => x.FieldName)); string sql = string.Format("select {0} from {1}", string.Join(", ", fieldNames), Mapper.TableName); if (orderField != null) { var field = GetOrderField(orderField); sql += string.Format(" order by {0} {1}", field.FieldName, orderField.Ascending ? "asc" : "desc"); } var query = session.CreateQuery <T>(sql); return(query); }
/// <summary> /// Append order by clause if exist /// </summary> /// <param name="baseSql"></param> /// <param name="orderBy"></param> /// <returns></returns> private string AppendOrder(string baseSql, OrderCritia <T> orderBy) { Expect.IsNotBlank(baseSql, nameof(baseSql)); string result = baseSql; if (orderBy != null) { var prop = ExpressionBuilder.GetProperty(orderBy.Selector); Expect.IsNotNull(prop, nameof(prop)); var fieldMapper = Mapper.FieldMappers.FirstOrDefault(x => x.PropertyName == prop.Name); Expect.IsNotNull(fieldMapper, nameof(fieldMapper)); result += string.Format(" order by {0} {1}", fieldMapper.FieldName, orderBy.Ascending ? "asc" : "desc"); } return(result); }
/// <inheritdoc /> public override Tuple <IQuery <T>, IQuery <T> > CreatePagedQueryTuple(ISession session, string baseSql, OrderCritia <T> orderCritia, QueryDto qdata) { var re = new Regex(@"(select\s+)([\s\S]+)(\s+from[\s\S]+)", RegexOptions.IgnoreCase); var match = re.Match(baseSql); if (!match.Success) { throw new ArgumentException("Unsupport sql: " + baseSql); } var field = GetOrderField(orderCritia); string countSql = string.Format("{0}count(*){1}", match.Groups[1].Value, match.Groups[3].Value); var countQuery = session.CreateQuery <T>(countSql); qdata.SetQuery(countQuery); string dataSql = string.Format("{0} order by {1} {2} offset @offset row fetch next @limit rows only", baseSql, field.FieldName, orderCritia.Ascending ? "asc" : "desc"); var dataQuery = session.CreateQuery <T>(dataSql); qdata.SetQuery(dataQuery); return(new Tuple <IQuery <T>, IQuery <T> >(countQuery, dataQuery)); }
/// <inheritdoc /> public TEntity[] GetAll(OrderCritia <TEntity> orderField = null) { return(GetQueryBuilder().GetAll(Session, orderField) .GetAll()); }
/// <inheritdoc /> public abstract Tuple <IQuery <T>, IQuery <T> > CreatePagedQueryTuple(ISession session, string baseSql, OrderCritia <T> orderCritia, QueryDto qdata);