/// <summary> /// 生成“Select Paged Query SQL” /// </summary> /// <typeparam name="TEntity">类型参数</typeparam> /// <param name="connection">DbConnection</param> /// <param name="predicate">条件表达式目录树</param> /// <param name="pageNumber">当前页</param> /// <param name="pageSize">每页显示数</param> /// <returns>string</returns> private static string BuildSelectPagedQuery <TEntity>(IDbConnection connection, Expression <Func <TEntity, bool> > predicate, int pageNumber, int pageSize, out DynamicParameters parameters) { string sql = BuildSelectSql <TEntity>(predicate, out parameters); var keyColumns = ""; try { keyColumns = (Resolvers.KeyProperty(typeof(TEntity))).Name; } catch (Exception ex) { throw new Exception("不可存在多个键属性“[Key()]”\r\n" + ex.Message); } //排序 var orderBy = "order by " + string.Join(", ", keyColumns); sql += new CustomSqlServerSqlBuilder().BuildPaging(orderBy, pageNumber, pageSize); return(sql); }
/// <summary> /// 根据“Type”生成Insert SQL /// </summary> /// <param name="connection">DbConnection</param> /// <param name="type">Type</param> /// <returns>string</returns> private static string BuildInsertQuery(IDbConnection connection, Type type) { string insertSQlSentence = ""; if (!insertQueryCache.TryGetValue(type, out insertSQlSentence)) { string tableName = Resolvers.Table(type); bool flag = false; PropertyInfo keyProperty = Resolvers.KeyProperty(type, out flag); List <PropertyInfo> source = new List <PropertyInfo>(); foreach (PropertyInfo propertyInfo in Resolvers.Properties(type)) { if (((propertyInfo != keyProperty) || !flag) && (propertyInfo.GetSetMethod() != null)) { source.Add(propertyInfo); } } string[] columnNames = source.Select <PropertyInfo, string>(new Func <PropertyInfo, string>(Resolvers.Column)).ToArray <string>(); string[] paramNames = (from p in source select "@" + p.Name).ToArray <string>(); insertSQlSentence = new CustomSqlServerSqlBuilder().BuildInsert(tableName, columnNames, paramNames, keyProperty); insertQueryCache.TryAdd(type, insertSQlSentence); } return(insertSQlSentence); }