-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.cs
141 lines (112 loc) · 4.51 KB
/
Table.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace SatelliteDataScripter
{
public class Table
{
public Schema Schema { get; private set; }
public string Name { get; set; }
protected string FullName
{
get { return string.Format("{0}.{1}", Schema.Name, Name); }
}
protected string EscapedFullName
{
get { return string.Format("[{0}].[{1}]", Schema.Name, Name); }
}
public IEnumerable<Column> Columns { get; private set; }
public IEnumerable<Column> NonIdColumns
{
get { return Columns.Where(g => !g.IsIdColumn); }
}
public IEnumerable<Column> GeneratedColumns
{
get { return Columns.Where(g => g.Generate); }
}
public Table(Schema schema, TablesDataSet.TableRow row)
{
Schema = schema;
Name = row.Table_Name;
Columns = LoadColumns();
}
public override string ToString()
{
return Name;
}
public string ScriptData()
{
if (Columns.Count(c => c.IsIdColumn) == 0)
{
throw new Exception("The table does not contain a column with name Id which is required.");
}
var sb = new StringBuilder();
sb.Append("-- ");
sb.AppendLine(FullName);
foreach (DataRow record in GetTableData().Rows)
{
sb.AppendFormat("IF NOT EXISTS(SELECT 1 FROM {0} WHERE Id = {1})", EscapedFullName, GetIdValue(record));
sb.AppendLine();
GenerateInsertStatement(sb, record);
if (Schema.Connection.Model.GenerateUpdateStatements)
{
GenerateUpdateStatement(sb, record);
}
}
return sb.ToString().Trim();
}
private void GenerateInsertStatement(StringBuilder sb, DataRow record)
{
sb.Append(" INSERT INTO ");
sb.Append(EscapedFullName);
sb.Append(" (");
sb.Append(string.Join(", ", GeneratedColumns.Select(c => c.EscapedName).ToArray()));
sb.Append(") VALUES (");
sb.Append(string.Join(", ", GeneratedColumns.Select(c => c.GetValue(record)).ToArray()));
sb.AppendLine(")");
}
private void GenerateUpdateStatement(StringBuilder sb, DataRow record)
{
sb.Append(" ELSE UPDATE ");
sb.Append(EscapedFullName);
sb.Append(" SET ");
sb.Append(string.Join(", ", GeneratedColumns.Where(c => !c.IsIdColumn).Select(c => string.Format("{0} = {1}", c.EscapedName, c.GetValue(record))).ToArray()));
sb.Append(" WHERE Id = ");
sb.Append(GetIdValue(record));
if (Schema.Connection.Model.UpdateOnlyIfDataIsChanged)
{
sb.Append(" AND (");
sb.Append(string.Join(" OR ", GeneratedColumns.Where(c => !c.IsIdColumn).Select(c => string.Format("{0} {1} {2}", c.EscapedName, c.GetValue(record) == "NULL" ? "IS NOT" : "<>", c.GetValue(record))).ToArray()));
sb.Append(")");
}
sb.AppendLine();
}
private string GetIdValue(DataRow record)
{
return Columns.Single(c => c.IsIdColumn).GetValue(record);
}
private DataTable GetTableData()
{
using (var adapter = new SqlDataAdapter("select * from " + EscapedFullName + " order by Id", Schema.Connection.ConnectionString))
{
var dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
}
private IEnumerable<Column> LoadColumns()
{
using (var adapter = new SqlDataAdapter("select COLUMN_NAME, DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = @schema and TABLE_NAME = @table order by ORDINAL_POSITION", Schema.Connection.ConnectionString))
{
adapter.SelectCommand.Parameters.AddWithValue("@schema", Schema.Name);
adapter.SelectCommand.Parameters.AddWithValue("@table", Name);
var dataTable = new TablesDataSet.ColumnDataTable();
adapter.Fill(dataTable);
return dataTable.Rows.Cast<TablesDataSet.ColumnRow>().Select(r => new Column(r)).ToList();
}
}
}
}