-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathTsqlGenerator.cs
More file actions
173 lines (159 loc) · 6.38 KB
/
Copy pathTsqlGenerator.cs
File metadata and controls
173 lines (159 loc) · 6.38 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NSF2SQL
{
class TsqlGenerator : ISqlGenerator
{
public string CreateDatabase(string databaseName)
{
return string.Format(@"DROP DATABASE IF EXISTS {0};
GO
CREATE DATABASE {0};
GO
USE [{0}];", databaseName);
}
public string SetVariables()
{
return string.Empty;
}
public string CreateTable(Table table)
{
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.AppendFormat("CREATE TABLE [{0}] (\r\n", table.Name);
sqlBuilder.AppendLine("[id] INT NOT NULL,");
foreach (Column column in table.Columns.Values)
{
sqlBuilder.AppendFormat("[{0}] {1} NULL,\r\n", column.Name, column.Type);
}
sqlBuilder.Append("PRIMARY KEY ([id])");
if (table.LinkedTable != null)
{
sqlBuilder.Append(",\r\n");
sqlBuilder.AppendFormat("CONSTRAINT[{0}_constraint] FOREIGN KEY([{1}id]) REFERENCES [{1}]([id]) ON DELETE CASCADE ON UPDATE CASCADE);\r\n", table.Name, table.LinkedTable);
sqlBuilder.AppendFormat("CREATE INDEX[{0}_idx] ON [{0}]([{1}id]);\r\n", table.Name, table.LinkedTable);
}
else
{
sqlBuilder.Append(");\r\n");
}
return sqlBuilder.ToString();
}
public string BeginInsertTable(Table table)
{
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.AppendFormat("INSERT INTO [{0}] ([id],[", table.Name);
sqlBuilder.Append(string.Join("],[", table.Columns.Keys));
sqlBuilder.AppendLine("]) VALUES");
return sqlBuilder.ToString();
}
public List<string> InsertTableRowsToList(Table table)
{
List<string> rows = new List<string>(table.RowCount);
for (int i = 0; i < table.RowCount; i++)
{
List<string> columnValues = new List<string>(table.Columns.Count);
foreach (Column column in table.Columns.Values)
{
if (column.Values.ContainsKey(i + 1))
{
if (column.Values[i + 1] != null)
{
string value = column.Values[i + 1].ToString();
switch (column.Type)
{
case "decimal(20,10)":
if (value == "")
{
value = "NULL";
}
else if (value == "Infinity")
{
value = "9999999999.9999999999";
}
else
{
double temp;
if (!double.TryParse(value, out temp))
{
value = "NULL";
}
}
break;
case "datetime":
if (value == "")
{
value = "NULL";
}
else
{
DateTime temp;
if (DateTime.TryParse(value, out temp))
{
if (column.Values[i + 1] is DateTime)
{
value = ((DateTime)column.Values[i + 1]).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
value = temp.ToString("yyyy-MM-dd HH:mm:ss");
}
value = "'" + value + "'";
}
else
{
value = "NULL";
}
}
break;
default:
value = "'" + value.ToString().Replace("'", "''") + "'";
break;
}
columnValues.Add(value);
}
else
{
columnValues.Add("NULL");
}
}
else
{
columnValues.Add("NULL");
}
}
rows.Add("(" + (i + 1) + "," + String.Join(",", columnValues) + ")");
}
return rows;
}
public string InsertTableRowsToString(Table table)
{
StringBuilder sqlBuilder = new StringBuilder();
List<string> rows = InsertTableRowsToList(table);
int index = 0;
do
{
sqlBuilder.Append(string.Join(",\r\n", rows.GetRange(index, Math.Min(1000, rows.Count - index))));
sqlBuilder.AppendLine(";");
index += 1000;
if(index < rows.Count)
{
sqlBuilder.AppendLine();
sqlBuilder.Append(BeginInsertTable(table));
}
}
while (index < rows.Count);
return sqlBuilder.ToString();
}
public string EndInsertTable(Table table)
{
return string.Empty;
}
public string RestoreVariables()
{
return string.Empty;
}
}
}