private void BuildUpdate(ToDBCommand command, StringBuilder sql) { if (command.UpdateClause != null) { sql.Append("update " + command.UpdateClause.Item + " "); } }
void BuidDelete(ToDBCommand command, StringBuilder sql) { if (command.DeleteClause != null) { sql.Append("delete " + command.DeleteClause.Item + " "); } }
private void BuildSets(ToDBCommand command, StringBuilder sql) { if (command.SetItems != null && command.SetItems.Any()) { sql.Append("set " + string.Join(",", command.SetItems.Select(x => x.Left + "=" + x.Right)) + " "); } }
public string ToSql(ToDBCommand command) { StringBuilder sql = new StringBuilder(); ToSql(command, sql); return(sql.ToString().TrimEnd()); }
void BuildOrderBy(ToDBCommand command, StringBuilder sql) { if (command.OrderBys.Any()) { sql.Append("order by " + string.Join(",", command.OrderBys.Select(x => x.Column + " " + (x.Desc ? "desc" : "asc"))) + " "); } }
void BuildFrom(ToDBCommand command, StringBuilder sql) { if (command.FromClause != null) { sql.Append("from " + command.FromClause.Item + " "); } }
void BuildUnions(ToDBCommand command, StringBuilder sql) { foreach (Union union in command.Unions) { string op = ""; switch (union.TypeOfUnion) { case Union.UnionType.Union: op = "union"; break; case Union.UnionType.UnionAll: op = "union all"; break; case Union.UnionType.Intersect: op = "intersect"; break; case Union.UnionType.Except: op = "except"; break; default: throw new NotImplementedException(); } sql.Append(op + " " + ToSql(union.Query)); } }
void BuildGroupBys(ToDBCommand command, StringBuilder sql) { if (command.GroupBys != null && command.GroupBys.Any()) { sql.Append("group by " + string.Join(",", command.GroupBys.Select(x => x.Column)) + " "); } }
void BuildWhere(ToDBCommand command, StringBuilder sql) { if (command.WhereClause.Items.Any()) { sql.Append("where "); sql.Append(RenderConditions(command.WhereClause)); } }
void BuildHavings(ToDBCommand command, StringBuilder sql) { if (command.HavingClause.Items.Any()) { sql.Append("having "); sql.Append(RenderConditions(command.HavingClause)); } }
void BuildInsert(ToDBCommand command, StringBuilder sql) { if (command.InsertClause != null) { sql.Append("insert " + command.InsertClause.Table.Item + " "); if (command.InsertClause.Columns.Any()) { sql.Append("(" + string.Join(",", command.InsertClause.Columns) + ") "); } } }
string RenderSubQuery(ToDBCommand command, string alias) { StringBuilder sql = new StringBuilder(); sql.Append("("); ToSql(command, sql); sql.Append(")"); if (!string.IsNullOrWhiteSpace(alias)) { sql.Append(" " + alias); } return(sql.ToString()); }
void BuildValues(ToDBCommand command, StringBuilder sql) { if (command.ValuesClause != null && command.ValuesClause.Any()) { sql.Append("values "); bool isFirst = true; foreach (var item in command.ValuesClause) { if (!isFirst) { sql.Append(","); } sql.Append("(" + string.Join(",", item.Parameters) + ")"); isFirst = false; } sql.Append(" "); } }
void ToSql(ToDBCommand command, StringBuilder sql) { BuidDelete(command, sql); BuildInsert(command, sql); BuildValues(command, sql); BuildUpdate(command, sql); BuildSets(command, sql); BuildSelect(command.SelectItems, sql); BuildFrom(command, sql); BuildJoins(command.Joins, sql); BuildWhere(command, sql); BuildUnions(command, sql); BuildGroupBys(command, sql); BuildHavings(command, sql); BuildOrderBy(command, sql); if (_options.RequireWhereOnUpdateAndDelete) { if (!command.WhereClause.Items.Any() && (command.DeleteClause != null || command.UpdateClause != null)) { throw new ToDBException("Update or Delete with no conditions. Set RequireWhereOnUpdateAndDelete to false in the options if this was intentional."); } } }
public static string ToSql(this ToDBCommand command, TSqlSerializerOptions options) { TSqlSerializer serializer = new TSqlSerializer(options); return(serializer.ToSql(command)); }
public static string ToSql(this ToDBCommand command) { TSqlSerializer serializer = new TSqlSerializer(); return(serializer.ToSql(command)); }