-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathInsertSelect.cs
76 lines (67 loc) · 1.69 KB
/
InsertSelect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
namespace NHibernate.SqlCommand
{
public class InsertSelect : ISqlStringBuilder
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(InsertSelect));
private string tableName;
private string comment;
private readonly List<string> columnNames = new List<string>();
private SqlSelectBuilder select;
public virtual InsertSelect SetTableName(string tableName)
{
this.tableName = tableName;
return this;
}
public virtual InsertSelect SetComment(string comment)
{
this.comment = comment;
return this;
}
public virtual InsertSelect AddColumn(string columnName)
{
columnNames.Add(columnName);
return this;
}
public virtual InsertSelect AddColumns(string[] columnNames)
{
this.columnNames.AddRange(columnNames);
return this;
}
public virtual InsertSelect SetSelect(SqlSelectBuilder select)
{
this.select = select;
return this;
}
public SqlString ToSqlString()
{
if (tableName == null)
throw new HibernateException("no table name defined for insert-select");
if (select == null)
throw new HibernateException("no select defined for insert-select");
var buf = new SqlStringBuilder(columnNames.Count + 4);
if (comment != null)
{
buf.Add("/* " + comment + " */ ");
}
buf.Add("insert into ").Add(tableName);
if (!(columnNames.Count == 0))
{
buf.Add(" (");
bool commaNeeded= false;
foreach (var columnName in columnNames)
{
if(commaNeeded)
{
buf.Add(", ");
}
buf.Add(columnName);
commaNeeded = true;
}
buf.Add(")");
}
buf.Add(" ").Add(select.ToStatementString());
return buf.ToSqlString();
}
}
}