1 //---------------------------------------------------------------------
2 // <copyright file="SqlBuilder.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Collections.Generic;
12 using System.Diagnostics;
15 using System.Data.SqlClient;
16 using System.Data.Metadata.Edm;
17 using System.Data.Common.CommandTrees;
19 namespace System.Data.SqlClient.SqlGen
22 /// This class is like StringBuilder. While traversing the tree for the first time,
23 /// we do not know all the strings that need to be appended e.g. things that need to be
24 /// renamed, nested select statements etc. So, we use a builder that can collect
25 /// all kinds of sql fragments.
27 internal class SqlBuilder : ISqlFragment
29 private List<object> _sqlFragments;
30 private List<object> sqlFragments
34 if (null == _sqlFragments)
36 _sqlFragments = new List<object>();
44 /// Add an object to the list - we do not verify that it is a proper sql fragment
45 /// since this is an internal method.
47 /// <param name="s"></param>
48 public void Append(object s)
50 Debug.Assert(s != null);
55 /// This is to pretty print the SQL. The writer <see cref="SqlWriter.Write"/>
56 /// needs to know about new lines so that it can add the right amount of
57 /// indentation at the beginning of lines.
59 public void AppendLine()
61 sqlFragments.Add("\r\n");
65 /// Whether the builder is empty. This is used by the <see cref="SqlGenerator.Visit(DbProjectExpression)"/>
66 /// to determine whether a sql statement can be reused.
68 public virtual bool IsEmpty
70 get { return ((null == _sqlFragments) || (0 == _sqlFragments.Count)); }
73 #region ISqlFragment Members
76 /// We delegate the writing of the fragment to the appropriate type.
78 /// <param name="writer"></param>
79 /// <param name="sqlGenerator"></param>
80 public virtual void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
82 if (null != _sqlFragments)
84 foreach (object o in _sqlFragments)
86 string str = (o as String);
93 ISqlFragment sqlFragment = (o as ISqlFragment);
94 if (null != sqlFragment)
96 sqlFragment.WriteSql(writer, sqlGenerator);
100 throw new InvalidOperationException();