private String ConditionConvert(ColumnRelevanceMapper column, bool isNull = false) { StringBuilder sb = new StringBuilder(); //如果 null == item.id 的写法, 会解析成 //column.ColumnName == null && column.SqlOperatorEnum == SqlOperatorEnum.None // null,那么就直接返回"", 接下来的 == item.id 会传递false,然后解析 item.id is null if (column.ColumnName == null && column.SqlOperatorEnum == SqlOperatorEnum.None) { return(""); } //如果是 item.id == null 中写法 那么 == null 则返回 is null if (isNull || column.ColumnName == null) { column.SqlOperatorEnum = SqlEnumConvert(column.SqlOperatorEnum); } if (column.SqlOperatorEnum == SqlOperatorEnum.EqualNotNull || column.SqlOperatorEnum == SqlOperatorEnum.EqualNull) { String str = ""; if (!String.IsNullOrEmpty(column.ColumnName)) { Type t = column.TableName; String tableName = EntityTableMapper.GetTableName(t); String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName); str = $"{tableName}.{columnName}"; } return($"{str} {column.SqlOperatorEnum.GetOperator()}"); } if (column.SqlOperatorEnum != SqlOperatorEnum.None) { sb.Append(column.SqlOperatorEnum.GetOperator()); } //如果是 直接传递字符串 如 id in (select id from xxx)中,则 括号中则直接拼接,而不用@数字 //如果是 item.id == 1 中的 == 1,那么只 保存 = @ColumnName if (column.TableName == null) { if (column.ColumnName.IndexOf("(") != -1) { sb.Append($" {column.ColumnName} "); } else { sb.Append($"@{column.ColumnName}"); } } //如果是 item.id == 1 中的 item.id ,那么就保存 tablename.id else { Type t = column.TableName; String tableName = EntityTableMapper.GetTableName(t); String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName); sb.Append($" [{tableName}].[{columnName}] "); } return(sb.ToString()); }
private String ConditionConvert(ColumnRelevanceMapper column, bool isNull = false) { StringBuilder sb = new StringBuilder(); //如果 null == item.id 中写法 null,那么返回"", == item.id 返回item.id is null //如果是 item.id == null 中写法 那么 == null 则返回 is null if (column.ColumnName == null && column.SqlOperatorEnum == SqlOperatorEnum.None) { return(""); } if (isNull || column.ColumnName == null) { column.SqlOperatorEnum = SqlEnumConvert(column.SqlOperatorEnum); } if (column.SqlOperatorEnum == SqlOperatorEnum.EqualNotNull || column.SqlOperatorEnum == SqlOperatorEnum.EqualNull) { String str = ""; if (!String.IsNullOrEmpty(column.ColumnName)) { Type t = column.TableName; String tableName = EntityTableMapper.GetTableName(t); String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName); str = $"{tableName}.{columnName}"; } return($"{str} {column.SqlOperatorEnum.GetOperator()}"); } if (column.SqlOperatorEnum != SqlOperatorEnum.None) { sb.Append(column.SqlOperatorEnum.GetOperator()); } //如果是 item.id = 1 中的 = 1,那么只 保存 = @ColumnName if (column.TableName == null) { sb.Append($"@{column.ColumnName}"); } else { Type t = column.TableName; String tableName = EntityTableMapper.GetTableName(t); String columnName = EntityTableMapper.GetColoumName(t, column.ColumnName); sb.Append($" {tableName}.{columnName} "); } return(sb.ToString()); }
public override IBatch End() { IBatch batch = new SqlServerBatch(); Tuple <List <String>, List <String> > tuple = ConvertHeader(Reduce.GetHeader()); Type t = typeof(T); StringBuilder sb = new StringBuilder(); TableRelevanceMapper table = Reduce.GetTable(); List <ColumnRelevanceMapper> whereColumn = Reduce.GetWhere(); sb.Append(table.TableOperatorEnum.GetOperator()); sb.Append($" {EntityTableMapper.GetTableName(t)} "); switch (table.TableOperatorEnum) { case TableOperatorEnum.Insert: sb.Append(GetInsertSql(tuple)); break; case TableOperatorEnum.Update: sb.Append(" set "); sb.Append(GetUpdateSql(tuple)); if (whereColumn.Count == 0) { SetNoneWhere(t); } break; case TableOperatorEnum.Delete: if (whereColumn.Count == 0) { SetNoneWhere(t); } break; default: throw new Exception("请先选择操作种类"); } String where = ConvertToWhere(whereColumn); if (!String.IsNullOrEmpty(where)) { sb.Append($" where {where}"); } batch.SqlBuilder = sb.ToString(); batch.DynamicParameters = Reduce.Parameters; return(batch); }
private String ConvertToQuery(List <ColumnRelevanceMapper> query) { StringBuilder sb = new StringBuilder(256); foreach (var column in query) { if (sb.Length != 0) { sb.Append(","); } if (column.TableName == null) { sb.Append($" {column.ColumnName} "); continue; } String tablename = EntityTableMapper.GetTableName(column.TableName); String columnname = EntityTableMapper.GetColoumName(column.TableName, column.ColumnName); sb.Append($"{tablename}.{columnname}"); } return(sb.ToString()); }
private String ConvertToTable(List <TableRelevanceMapper> tables) { StringBuilder sb = new StringBuilder(); foreach (var table in tables) { String tableName = EntityTableMapper.GetTableName(table.TableName); if (table.TableOperatorEnum == TableOperatorEnum.None) { sb.Insert(0, tableName); } else { String tableOperator = table.TableOperatorEnum.GetOperator(); sb.Append($" {tableOperator} {tableName} on "); foreach (var on in table.ColumnOperator) { sb.Append($" {ConditionConvert(on)} "); } } } return(sb.ToString()); }