Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / SqlClient / SqlGen / SqlBuilder.cs
1 //---------------------------------------------------------------------
2 // <copyright file="SqlBuilder.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner  Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections.Generic;
12 using System.Diagnostics;
13 using System.IO;
14 using System.Text;
15 using System.Data.SqlClient;
16 using System.Data.Metadata.Edm;
17 using System.Data.Common.CommandTrees;
18
19 namespace System.Data.SqlClient.SqlGen
20 {
21     /// <summary>
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.
26     /// </summary>
27     internal class SqlBuilder : ISqlFragment
28     {
29         private List<object> _sqlFragments;
30         private List<object> sqlFragments
31         {
32             get
33             {
34                 if (null == _sqlFragments)
35                 {
36                     _sqlFragments = new List<object>();
37                 }
38                 return _sqlFragments;
39             }
40         }
41
42
43         /// <summary>
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.
46         /// </summary>
47         /// <param name="s"></param>
48         public void Append(object s)
49         {
50             Debug.Assert(s != null);
51             sqlFragments.Add(s);
52         }
53
54         /// <summary>
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.
58         /// </summary>
59         public void AppendLine()
60         {
61             sqlFragments.Add("\r\n");
62         }
63
64         /// <summary>
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.
67         /// </summary>
68         public virtual bool IsEmpty
69         {
70             get { return ((null == _sqlFragments) || (0 == _sqlFragments.Count)); }
71         }
72
73         #region ISqlFragment Members
74
75         /// <summary>
76         /// We delegate the writing of the fragment to the appropriate type.
77         /// </summary>
78         /// <param name="writer"></param>
79         /// <param name="sqlGenerator"></param>
80         public virtual void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator)
81         {
82             if (null != _sqlFragments)
83             {
84                 foreach (object o in _sqlFragments)
85                 {
86                     string str = (o as String);
87                     if (null != str)
88                     {
89                         writer.Write(str);
90                     }
91                     else
92                     {
93                         ISqlFragment sqlFragment = (o as ISqlFragment);
94                         if (null != sqlFragment)
95                         {
96                             sqlFragment.WriteSql(writer, sqlGenerator);
97                         }
98                         else
99                         {
100                             throw new InvalidOperationException();
101                         }
102                     }
103                 }
104             }
105         }
106
107         #endregion
108     }
109 }