// 创建 DELETE 命令 protected override Command ParseDeleteCommand <T>(DbQueryableInfo_Delete <T> dQuery, ParserToken token) { TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); ISqlBuilder builder = this.CreateSqlBuilder(token); bool useKey = false; builder.Append("DELETE FROM "); builder.AppendMember(typeRuntime.TableName, !typeRuntime.IsTemporary); builder.Append(" t0 "); if (dQuery.Entity != null) { object entity = dQuery.Entity; builder.AppendNewLine(); builder.Append("WHERE "); foreach (var kv in typeRuntime.Invokers) { MemberInvokerBase invoker = kv.Value; var column = invoker.Column; if (column != null && column.IsKey) { useKey = true; var value = invoker.Invoke(entity); var seg = builder.GetSqlValue(value, column); builder.AppendMember("t0", invoker.Member.Name); builder.Append(" = "); builder.Append(seg); builder.Append(" AND "); } ; } builder.Length -= 5; if (!useKey) { throw new XFrameworkException("Delete<T>(T value) require T must have key column."); } } else if (dQuery.SelectInfo != null) { TableAliasCache aliases = this.PrepareAlias <T>(dQuery.SelectInfo, token); var cmd2 = new NpgSelectInfoCommand(this, aliases, NpgCommandType.DELETE, token); cmd2.HasManyNavigation = dQuery.SelectInfo.HasManyNavigation; var visitor0 = new NpgExistsExpressionVisitor(this, aliases, dQuery.SelectInfo.Joins, NpgCommandType.DELETE); visitor0.Write(cmd2); var visitor1 = new NpgWhereExpressionVisitor(this, aliases, dQuery.SelectInfo.WhereExpression); visitor1.Write(cmd2.WhereFragment); cmd2.AddNavMembers(visitor1.NavMembers); builder.Append(cmd2.CommandText); } builder.Append(';'); return(new Command(builder.ToString(), builder.Token != null ? builder.Token.Parameters : null, System.Data.CommandType.Text)); }
/// <summary> /// 创建 DELETE 命令 /// </summary> /// <param name="tree">查询语义</param> /// <param name="context">解析SQL命令上下文</param> /// <returns></returns> protected override DbRawCommand TranslateDeleteCommand <T>(DbQueryDeleteTree tree, ITranslateContext context) { ISqlBuilder builder = this.CreateSqlBuilder(context); var typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); builder.Append("DELETE FROM "); builder.AppendTable(typeRuntime.TableSchema, typeRuntime.TableName, typeRuntime.IsTemporary); builder.Append(" t0 "); if (tree.Entity != null) { if (typeRuntime.KeyMembers == null || typeRuntime.KeyMembers.Count == 0) { throw new XFrameworkException("Delete<T>(T value) require entity must have key column."); } object entity = tree.Entity; builder.AppendNewLine(); builder.Append("WHERE "); foreach (FieldAccessorBase m in typeRuntime.KeyMembers) { var value = m.Invoke(entity); var sqlExpression = this.Constor.GetSqlValue(value, context, m.Column); builder.AppendMember("t0", m.Member, typeRuntime.Type); builder.Append(" = "); builder.Append(sqlExpression); builder.Append(" AND "); } builder.Length -= 5; } else if (tree.Select != null) { AliasGenerator ag = this.PrepareTableAlias(tree.Select, context.AliasPrefix); var cmd = new NpgDbSelectCommand(context, ag, DbExpressionType.Delete, tree.Select.SelectHasMany); if (tree.Select.Joins != null) { var visitor = new NpgJoinExpressionVisitor(ag, cmd.JoinFragment, DbExpressionType.Delete, cmd); visitor.Visit(tree.Select.Joins); } if (tree.Select.Wheres != null) { var visitor = new NpgWhereExpressionVisitor(ag, cmd.WhereFragment); visitor.Visit(tree.Select.Wheres); cmd.AddNavMembers(visitor.NavMembers); } builder.Append(cmd.CommandText); } builder.Append(';'); return(new DbRawCommand(builder.ToString(), builder.TranslateContext != null ? builder.TranslateContext.Parameters : null, System.Data.CommandType.Text)); }
/// <summary> /// 创建 DELETE 命令 /// </summary> /// <param name="dbQuery">查询语义</param> /// <param name="token">解析上下文</param> /// <returns></returns> protected override RawCommand ResolveDeleteCommand <T>(IDbQueryableInfo_Delete dbQuery, ResolveToken token) { ISqlBuilder builder = this.CreateSqlBuilder(token); var typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); builder.Append("DELETE FROM "); builder.AppendMember(typeRuntime.TableName, !typeRuntime.IsTemporary); builder.Append(" t0 "); if (dbQuery.Entity != null) { if (typeRuntime.KeyMembers == null || typeRuntime.KeyMembers.Count == 0) { throw new XFrameworkException("Delete<T>(T value) require entity must have key column."); } object entity = dbQuery.Entity; builder.AppendNewLine(); builder.Append("WHERE "); foreach (var m in typeRuntime.KeyMembers) { var column = m.Column; var value = m.Invoke(entity); var seg = this.DbValue.GetSqlValue(value, token, column); builder.AppendMember("t0", m.Member.Name); builder.Append(" = "); builder.Append(seg); builder.Append(" AND "); } builder.Length -= 5; } else if (dbQuery.Query != null) { TableAlias aliases = this.PrepareTableAlias(dbQuery.Query, token); var cmd = new NpgMappingDbCommand(this, aliases, DbExpressionType.Delete, token) { HasMany = dbQuery.Query.HasMany }; var visitor = new NpgJoinExpressionVisitor(this, aliases, dbQuery.Query.Joins, DbExpressionType.Delete); visitor.Write(cmd); var visitor_ = new NpgWhereExpressionVisitor(this, aliases, dbQuery.Query.Where); visitor_.Write(cmd.WhereFragment); cmd.AddNavMembers(visitor_.NavMembers); builder.Append(cmd.CommandText); } builder.Append(';'); return(new RawCommand(builder.ToString(), builder.Token != null ? builder.Token.Parameters : null, System.Data.CommandType.Text)); }
// 创建 DELETE 命令 protected override Command ParseDeleteCommand <T>(DbQueryableInfo_Delete <T> dQueryInfo, ResolveToken token) { TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); ISqlBuilder builder = this.CreateSqlBuilder(token); builder.Append("DELETE FROM "); builder.AppendMember(typeRuntime.TableName, !typeRuntime.IsTemporary); builder.Append(" t0 "); if (dQueryInfo.Entity != null) { if (typeRuntime.KeyInvokers == null || typeRuntime.KeyInvokers.Count == 0) { throw new XFrameworkException("Delete<T>(T value) require entity must have key column."); } object entity = dQueryInfo.Entity; builder.AppendNewLine(); builder.Append("WHERE "); foreach (var invoker in typeRuntime.KeyInvokers) { var column = invoker.Column; var value = invoker.Invoke(entity); var seg = this.DbValue.GetSqlValue(value, token, column); builder.AppendMember("t0", invoker.Member.Name); builder.Append(" = "); builder.Append(seg); builder.Append(" AND "); } builder.Length -= 5; } else if (dQueryInfo.SelectInfo != null) { TableAliasCache aliases = this.PrepareAlias <T>(dQueryInfo.SelectInfo, token); var cmd2 = new NpgNavigationCommand(this, aliases, DbExpressionType.Delete, token) { HasMany = dQueryInfo.SelectInfo.HasMany }; var visitor0 = new NpgJoinExpressionVisitor(this, aliases, dQueryInfo.SelectInfo.Joins, DbExpressionType.Delete); visitor0.Write(cmd2); var visitor1 = new NpgWhereExpressionVisitor(this, aliases, dQueryInfo.SelectInfo.WhereExpression); visitor1.Write(cmd2.WhereFragment); cmd2.AddNavMembers(visitor1.NavMembers); builder.Append(cmd2.CommandText); } builder.Append(';'); return(new Command(builder.ToString(), builder.Token != null ? builder.Token.Parameters : null, System.Data.CommandType.Text)); }
/// <summary> /// 创建 UPDATE 命令 /// </summary> /// <param name="dbQuery">查询语义</param> /// <param name="token">解析上下文</param> /// <returns></returns> protected override RawCommand ResolveUpdateCommand <T>(IDbQueryableInfo_Update dbQuery, ResolveToken token) { ISqlBuilder builder = this.CreateSqlBuilder(token); var typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); builder.Append("UPDATE "); builder.AppendMember(typeRuntime.TableName, !typeRuntime.IsTemporary); builder.Append(" t0 SET"); builder.AppendNewLine(); if (dbQuery.Entity != null) { object entity = dbQuery.Entity; ISqlBuilder seg_Where = this.CreateSqlBuilder(token); bool useKey = false; int length = 0; foreach (var m in typeRuntime.Members) { var column = m.Column; if (column != null && column.IsIdentity) { goto gotoLabel; // Fix issue# 自增列同时又是主键 } if (column != null && column.NoMapped) { continue; } if (m.ForeignKey != null) { continue; } if (m.Member.MemberType == System.Reflection.MemberTypes.Method) { continue; } builder.AppendMember(m.Member.Name); builder.Append(" = "); gotoLabel: var value = m.Invoke(entity); var seg = this.DbValue.GetSqlValueWidthDefault(value, token, column); if (column == null || !column.IsIdentity) { builder.Append(seg); length = builder.Length; builder.Append(','); builder.AppendNewLine(); } if (column != null && column.IsKey) { useKey = true; seg_Where.AppendMember(m.Member.Name); seg_Where.Append(" = "); seg_Where.Append(seg); seg_Where.Append(" AND "); } } if (!useKey) { throw new XFrameworkException("Update<T>(T value) require T must have key column."); } builder.Length = length; seg_Where.Length -= 5; builder.AppendNewLine(); builder.Append("WHERE "); builder.Append(seg_Where); } else if (dbQuery.Expression != null) { TableAlias aliases = this.PrepareTableAlias(dbQuery.Query, token); var visitor = new NpgUpdateExpressionVisitor(this, aliases, dbQuery.Expression); visitor.Write(builder); var cmd = new NpgMappingDbCommand(this, aliases, DbExpressionType.Update, token) { HasMany = dbQuery.Query.HasMany }; var visitor_ = new NpgJoinExpressionVisitor(this, aliases, dbQuery.Query.Joins, DbExpressionType.Update); visitor_.Write(cmd); var visitor__ = new NpgWhereExpressionVisitor(this, aliases, dbQuery.Query.Where); visitor__.Write(cmd.WhereFragment); cmd.AddNavMembers(visitor__.NavMembers); builder.Append(cmd.CommandText); } builder.Append(';'); return(new RawCommand(builder.ToString(), builder.Token != null ? builder.Token.Parameters : null, System.Data.CommandType.Text)); }
// 创建 UPDATE 命令 protected override Command ParseUpdateCommand <T>(DbQueryableInfo_Update <T> uQuery, ParserToken token) { ISqlBuilder builder = this.CreateSqlBuilder(token); var typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); builder.Append("UPDATE "); builder.AppendMember(typeRuntime.TableName, !typeRuntime.IsTemporary); builder.Append(" t0 SET"); builder.AppendNewLine(); if (uQuery.Entity != null) { object entity = uQuery.Entity; ISqlBuilder whereBuilder = this.CreateSqlBuilder(token); bool useKey = false; int length = 0; foreach (var kv in typeRuntime.Invokers) { MemberInvokerBase invoker = kv.Value; var column = invoker.Column; if (column != null && column.IsIdentity) { goto gotoLabel; // fix issue# 自增列同时又是主键 } if (column != null && column.NoMapped) { continue; } if (invoker.ForeignKey != null) { continue; } if (invoker.Member.MemberType == System.Reflection.MemberTypes.Method) { continue; } builder.AppendMember(invoker.Member.Name); builder.Append(" = "); gotoLabel: var value = invoker.Invoke(entity); var seg = builder.GetSqlValueWidthDefault(value, column); if (column == null || !column.IsIdentity) { builder.Append(seg); length = builder.Length; builder.Append(','); builder.AppendNewLine(); } if (column != null && column.IsKey) { useKey = true; whereBuilder.AppendMember(invoker.Member.Name); whereBuilder.Append(" = "); whereBuilder.Append(seg); whereBuilder.Append(" AND "); } } if (!useKey) { throw new XFrameworkException("Update<T>(T value) require T must have key column."); } builder.Length = length; whereBuilder.Length -= 5; builder.AppendNewLine(); builder.Append("WHERE "); builder.Append(whereBuilder); } else if (uQuery.Expression != null) { TableAliasCache aliases = this.PrepareAlias <T>(uQuery.SelectInfo, token); ExpressionVisitorBase visitor = null; visitor = new NpgUpdateExpressionVisitor(this, aliases, uQuery.Expression); visitor.Write(builder); var cmd2 = new NpgSelectInfoCommand(this, aliases, NpgCommandType.UPDATE, token); cmd2.HasManyNavigation = uQuery.SelectInfo.HasManyNavigation; var visitor0 = new NpgExistsExpressionVisitor(this, aliases, uQuery.SelectInfo.Joins, NpgCommandType.UPDATE); visitor0.Write(cmd2); var visitor1 = new NpgWhereExpressionVisitor(this, aliases, uQuery.SelectInfo.WhereExpression); visitor1.Write(cmd2.WhereFragment); cmd2.AddNavMembers(visitor1.NavMembers); builder.Append(cmd2.CommandText); } builder.Append(';'); return(new Command(builder.ToString(), builder.Token != null ? builder.Token.Parameters : null, System.Data.CommandType.Text)); }
/// <summary> /// 创建 UPDATE 命令 /// </summary> /// <param name="tree">查询语义</param> /// <param name="context">解析SQL命令上下文</param> /// <returns></returns> protected override DbRawCommand TranslateUpdateCommand <T>(DbQueryUpdateTree tree, ITranslateContext context) { ISqlBuilder builder = this.CreateSqlBuilder(context); var typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>(); builder.Append("UPDATE "); builder.AppendTable(typeRuntime.TableSchema, typeRuntime.TableName, typeRuntime.IsTemporary); builder.Append(" t0 SET"); builder.AppendNewLine(); if (tree.Entity != null) { object entity = tree.Entity; ISqlBuilder whereBuilder = this.CreateSqlBuilder(context); bool useKey = false; int length = 0; foreach (var item in typeRuntime.Members) { var m = item as FieldAccessorBase; if (m == null || !m.IsDbField) { continue; } if (m.Column != null && m.Column.IsIdentity) { goto LABEL; // Fix issue# 自增列同时又是主键 } builder.AppendMember(null, m.Member, typeRuntime.Type); builder.Append(" = "); LABEL: var value = m.Invoke(entity); var sqlExpression = this.Constor.GetSqlValueWidthDefault(value, context, m.Column); if (m.Column == null || !m.Column.IsIdentity) { builder.Append(sqlExpression); length = builder.Length; builder.Append(','); builder.AppendNewLine(); } if (m.Column != null && m.Column.IsKey) { useKey = true; whereBuilder.AppendMember(null, m.Member, typeRuntime.Type); whereBuilder.Append(" = "); whereBuilder.Append(sqlExpression); whereBuilder.Append(" AND "); } } if (!useKey) { throw new XFrameworkException("Update<T>(T value) require T must have key column."); } builder.Length = length; whereBuilder.Length -= 5; builder.AppendNewLine(); builder.Append("WHERE "); builder.Append(whereBuilder); } else if (tree.Expression != null) { AliasGenerator ag = this.PrepareTableAlias(tree.Select, context.AliasPrefix); DbExpressionVisitor visitor = null; visitor = new NpgUpdateExpressionVisitor(ag, builder); visitor.Visit(tree.Expression); var cmd = new NpgDbSelectCommand(context, ag, DbExpressionType.Update, tree.Select.SelectHasMany); if (tree.Select.Joins != null) { visitor = new NpgJoinExpressionVisitor(ag, cmd.JoinFragment, DbExpressionType.Update, cmd); visitor.Visit(tree.Select.Joins); } if (tree.Select.Wheres != null) { visitor = new NpgWhereExpressionVisitor(ag, cmd.WhereFragment); visitor.Visit(tree.Select.Wheres); cmd.AddNavMembers(visitor.NavMembers); } builder.Append(cmd.CommandText); } builder.Append(';'); return(new DbRawCommand(builder.ToString(), builder.TranslateContext != null ? builder.TranslateContext.Parameters : null, System.Data.CommandType.Text)); }