using Strata.SqlTools.SqlBreakdown.Extensions; namespace Strata.SqlTools.SqlBreakdown.Classes; /// /// Represents a SQL FROM clause with optional JOIN clauses. /// public class SqlFrom { private readonly SqlTable _firstTable; private readonly List _joins; /// /// Initializes a new instance of the class. /// /// The primary table expression. /// The primary table alias. public SqlFrom(string tableExpression, string tableAlias) { _firstTable = new SqlTable(tableExpression, tableAlias); _joins = []; } /// /// Adds a JOIN to the FROM clause. /// /// The table expression to join. /// The alias for the joined table. /// The column from the primary table. /// The column from the joined table. public void Join(string table2Expression, string table2Alias, string table1Column, string table2Column) { var theJoin = new SqlJoin(table2Expression, table2Alias, table1Column, table2Column); Join(theJoin); } /// /// Adds a pre-constructed JOIN to the FROM clause. /// /// The join to add. public void Join(SqlJoin join) { _joins.Add(join); } /// /// Returns the SQL FROM clause as a string. /// /// The FROM clause with all JOINs. public override string ToString() { var output = new StringBuilderEx(); output.AppendFormat($"\t{_firstTable.TableExpression} {_firstTable.TableAlias}\n"); foreach (var join in _joins) { output.AppendFormat($"\tINNER JOIN {join.TableExpression} {join.TableAlias} ON {join.GetJoinOn(_firstTable.TableAlias)}\n"); } return output.ToString(); } }