public ISQLJoin LeftJoin <TJoin, TEntityOther>(Expression <Func <TJoin, TEntityOther, bool> > join) { SQLJoin sqlJoin = new SQLJoin(Sql, join, JoinType.LeftJoin); sqlJoin.Type = typeof(TJoin); sqlJoin.Mapper = EntityMapper.GetMapper <TJoin>(); Sql.Joins = new List <ISQLJoin>() { sqlJoin }; return(sqlJoin); }
public TEntity GetSingle <TEntity>(object keyValue) { var sql = new SQL(); var p = Expression.Parameter(typeof(TEntity), "p"); var mi = typeof(TEntity).GetMembers().Where(r => r.Name == EntityMapper.GetMapper <TEntity>().GetPrimaryKeys()[0]).FirstOrDefault(); var left = Expression.MakeMemberAccess(p, mi); var right = Expression.Constant(keyValue); var equal = Expression.Equal(left, right); var lambda = Expression.Lambda <Func <TEntity, bool> >(equal, p); sql = sql.Select <TEntity>().Where <TEntity>(lambda as Expression <Func <TEntity, bool> >).Sql; return(Connection.QuerySingle <TEntity>(sql)); }
public SQLSelect(SQL sql, Type type) { Sql = sql; Type = type; Mapper = EntityMapper.GetMapper(type); }
protected override Expression VisitMember(MemberExpression node) { object value = null; Expression expression = searchMemberExpressionTree(node, node.Expression, out value); if (expression.NodeType == ExpressionType.Parameter) { var param = node.Expression as ParameterExpression; var mapper = EntityMapper.GetMapper(expression.Type); string alias = param.Name; if (_selectorCompilerStr != null) { // 编译select语句 if (!string.IsNullOrEmpty(Sql.Selector.Alias)) { // 被As强制定义了别名 if (!string.IsNullOrEmpty(alias) && alias != Sql.Selector.Alias) { throw new Exception($"SQLSelect.ConfirmAlias Error: 别名\"{ Sql.Selector.Alias}\"不可用,因为当前的Select中已经定义了别名\"{alias}\",请确保Select表达式\"{alias}=>\"中的名称与As(\"{Sql.Selector.Alias}\")中的名称一致"); } } else { // 没有被As强制定义别名 Sql.Selector.Alias = alias; } Sql.AliasCollection.Update(alias, Sql.Selector.Type); _selectorCompilerStr?.Append($"{Sql.Selector.ComposeSelectKey(mapper.GetColumnName(node.Member.Name))} AS {node.Member.Name},"); } if (_whereCompilerStr != null) { // 编译where语句 if (Sql.AliasCollection.Count == 0) { // 没有Alias,不显示 _whereCompilerStr.Append($"{mapper.GetColumnName(node.Member.Name)}"); } else { if (Sql.AliasCollection.ContainsKey(alias)) { _whereCompilerStr.Append($"{alias}.{mapper.GetColumnName(node.Member.Name)}"); } else { throw new Exception($"SQLCompiler.VisitMember Error: 你正在有意使用别名\"{alias}\",但在别名集合中没找到。尝试检查在Expression中使用的参数名是否在Select中使用过,或者是否在方法As()中定义过。'"); } } } if (_orderCompilerStr != null) { // 编译order语句 if (Sql.AliasCollection.Count == 0) { // 没有Alias,不显示 _orderCompilerStr.Append($"{mapper.GetColumnName(node.Member.Name)}{(_compilingOrder.By == OrderBy.DESC ? " DESC" : "")},"); } else { if (Sql.AliasCollection.ContainsKey(alias)) { _orderCompilerStr.Append($"{alias}.{mapper.GetColumnName(node.Member.Name)}{(_compilingOrder.By == OrderBy.DESC ? " DESC" : "")},"); } else { throw new Exception($"SQLCompiler.VisitMember Error: 你正在有意使用别名\"{alias}\",但在别名集合中没找到。尝试检查在Expression中使用的参数名是否在Select中使用过,或者是否在方法As()中定义过。'"); } } } if (_currentJoinCompilerStr != null) { // 编译Join语句 _currentJoinCompilerStr.Append($"{alias}.{mapper.GetColumnName(node.Member.Name)}"); } } else if (expression.NodeType == ExpressionType.Constant) { if (Sql.Parameters == null) { Sql.Parameters = new List <SQLParameter>(); } var param = Sql.Parameters.AddParameter(value); _whereCompilerStr?.Append(param.ParameterName); _currentJoinCompilerStr?.Append(param.ParameterName); } return(node); }