-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScriptData.cs
297 lines (249 loc) · 10.8 KB
/
ScriptData.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace dbscript
{
class ScriptData
{
private string m_table;
private string m_database;
private int m_limit_results;
private bool m_tbl_has_identity;
private bool m_for_fixtures;
private string m_where_clause;
private const int ROW_LIMIT = 10000;
public ScriptData(Connection conn, string dbFilesPath, string dbName, string tblName)
:this(conn, dbFilesPath, dbName, tblName, -1, false, "")
{}
public ScriptData(Connection conn, string dbFilesPath, string dbName, string tblName, int limit, bool fixtures, string where)
{
Console.WriteLine(String.Format(@"Generating data insert script for: {0}.dbo.{1}", dbName, tblName));
// generate data insert script as <dbFilePath>\Data\<tblName>.insert.sql
string dbDataScriptsPath = getDataScriptsPath(dbFilesPath, dbName, fixtures);
string filename = String.Format(@"{0}\{1}.insert.sql", dbDataScriptsPath, tblName);
m_table = tblName;
m_database = dbName;
m_limit_results = limit;
m_for_fixtures = fixtures;
m_where_clause = where;
m_tbl_has_identity = hasIdentity(conn, dbName, tblName);
if (m_tbl_has_identity == false) Console.WriteLine("table has no identity column !!");
ArrayList cols = columns(conn);
Console.WriteLine("got " + cols.Count + " columns ");
List<Hashtable> data = tableData(cols, conn);
Console.WriteLine("got " + data.Count + " rows ");
// write file
generateDataScript(filename, cols, data);
Console.WriteLine("Done ... \n");
}
private void generateDataScript(string filename, ArrayList cols, List<Hashtable> data)
{
int fileCounter = 0;
string sqlfile = filename;
if (data.Count > ROW_LIMIT)
{
fileCounter++;
sqlfile = Regex.Replace(filename, @"insert\.sql", @"insert." + fileCounter.ToString() + ".sql");
}
TextWriter tw = getScriptFileForWriting(sqlfile, cols);
int rowcount = 0;
foreach (Hashtable row in data)
{
int colcount = 0;
string sql = "SELECT ";
foreach (string col in cols)
{
sql += formatForTsqlScript(row[col.ToString()]);
colcount++;
if (colcount < cols.Count) sql += ",";
}
tw.WriteLine(sql);
tw.Flush();
rowcount++;
if (rowcount % ROW_LIMIT == 0)
{
// large data sets need to be separated into multiple insert files
// larger insert scripts (millions of rows) throw memory exceptions
closeScriptFile(tw);
fileCounter++;
sqlfile = Regex.Replace(filename, @"insert\.sql", @"insert." + fileCounter.ToString() + ".sql");
tw = getScriptFileForWriting(sqlfile, cols, false);
}
else if (rowcount < data.Count)
{
tw.WriteLine("UNION ALL"); // if not at end of data set
}
}
closeScriptFile(tw);
}
private TextWriter getScriptFileForWriting(string filename, ArrayList cols)
{
bool trunc = true;
if (m_for_fixtures == true) trunc = false;
return getScriptFileForWriting(filename, cols, trunc);
}
private TextWriter getScriptFileForWriting(string filename, ArrayList cols, bool withTruncate)
{
Console.WriteLine("generating script file: " + filename);
// write file
TextWriter tw = new StreamWriter(filename);
if (withTruncate == true) tw.WriteLine("TRUNCATE TABLE [{0}]", m_table); // fixtures don't truncate tables
if (m_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] ON", m_table);
tw.WriteLine("INSERT INTO [{0}] (", m_table);
tw.WriteLine("[" + string.Join("],[", cols.ToArray(typeof(string)) as string[]) + "]");
tw.WriteLine(")\n");
return tw;
}
private void closeScriptFile(TextWriter tw)
{
if (m_tbl_has_identity == true) tw.WriteLine("SET IDENTITY_INSERT [{0}] OFF", m_table);
tw.Close();
}
private List<Hashtable> tableData(ArrayList cols, Connection conn)
{
// limiting results?
string limit = "";
string order_by = ""; // if negative number for LIMIT then order descending (by first column)
if (m_limit_results != 0)
{
// get top n
limit = String.Format(@"TOP {0} ", Math.Abs(m_limit_results).ToString());
// ordering
order_by = "ORDER BY " + cols[0];
if (m_limit_results < 0) order_by += " DESC";
}
string command = String.Format(@"SELECT {0}* FROM [{1}].[dbo].[{2}] WITH(NOLOCK) {3} {4}", limit, m_database, m_table, m_where_clause, order_by);
SqlConnection sqlconn = new SqlConnection(conn.connectionString());
sqlconn.Open();
SqlCommand cmd = new SqlCommand(command, sqlconn);
SqlDataReader rdr = cmd.ExecuteReader();
List<Hashtable> rows = new List<Hashtable>();
while (rdr.Read())
{
Hashtable row = new Hashtable();
foreach (string c in cols)
{
row.Add(c, rdr[c]);
}
rows.Add(row);
}
rdr.Close();
return rows;
}
private ArrayList columns(Connection conn)
{
ArrayList cols = new ArrayList();
SqlConnection sqlconn = new SqlConnection(conn.connectionString());
sqlconn.Open();
SqlCommand cmd = new SqlCommand(m_database+".dbo.sp_columns", sqlconn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@table_name", m_table));
// execute the command
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
cols.Add(rdr["COLUMN_NAME"]);
}
rdr.Close();
return cols;
}
private string dataScript(ArrayList cols, List<Hashtable> data)
{
string script = @"INSERT INTO [" + m_table + "] (";
script += string.Join(",", cols.ToArray(typeof(string)) as string[]);
script += ")\n";
int rowcount = 0;
foreach (Hashtable row in data)
{
int colcount = 0;
script += "SELECT ";
foreach (string col in cols)
{
script += formatForTsqlScript(row[col.ToString()]);
colcount++;
if (colcount < cols.Count) script += ",";
}
rowcount++;
// if not at end of data
if (rowcount < data.Count) script += "\nUNION ALL\n";
}
return script;
}
public static bool hasIdentity(Connection conn, string dbName, string tblName)
{
//var sql = String.Format(@"SELECT COUNT(*) FROM {0}.SYS.IDENTITY_COLUMNS WHERE OBJECT_NAME(OBJECT_ID) = '{1}'", dbName, tblName);
var sql = String.Format(@"USE {0} SELECT OBJECTPROPERTY(OBJECT_ID('{1}'), 'TableHasIdentity')", dbName, tblName);
SqlConnection sqlconn = new SqlConnection(conn.connectionString());
sqlconn.Open();
SqlCommand cmd = new SqlCommand(sql,sqlconn);
if ((int)cmd.ExecuteScalar() == (int)0)
return false;
else
return true;
}
public static string[] getTables(Connection conn, string dbName)
{
ArrayList tbls = new ArrayList();
SqlConnection sqlconn = new SqlConnection(conn.connectionString());
sqlconn.Open();
SqlCommand cmd = new SqlCommand(dbName + ".dbo.sp_tables", sqlconn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
if (
Convert.ToString(rdr["TABLE_TYPE"]) == "TABLE" &&
Convert.ToString(rdr["TABLE_OWNER"]) != "sys"
) tbls.Add(rdr["TABLE_NAME"]);
}
rdr.Close();
return tbls.ToArray(typeof(string)) as string[];
}
static string getDataScriptsPath(string dbFilesPath, string dbName, bool fixtures)
{
string dir = "Data";
if (fixtures == true) dir = "Fixtures";
var dbPath = String.Format(@"{0}\{1}", dbFilesPath, dbName);
var dataScriptsPath = String.Format(@"{0}\{1}", dbPath, dir);
if (!Directory.Exists(dbFilesPath)) Directory.CreateDirectory(dbFilesPath);
if (!Directory.Exists(dbPath)) Directory.CreateDirectory(dbPath);
if (!Directory.Exists(dataScriptsPath)) Directory.CreateDirectory(dataScriptsPath);
return dataScriptsPath;
}
static string formatForTsqlScript(object val)
{
Type t = val.GetType();
string ret = "";
if (t == typeof(System.Boolean))
{
if ((bool)val == true)
ret = "1";
else
ret = "0";
}
else if (t == typeof(System.Int32) || t == typeof(System.Int64))
{
ret = val.ToString();
}
else if (t == typeof(System.DBNull))
{
ret = "NULL";
}
else if (t == typeof(System.DateTime))
{
DateTime dt = (DateTime)val;
ret = "'" + dt.ToString("yyyyMMdd HH':'mm':'ss") + "'";
}
else // strings, etc...
{
ret = "'" + val.ToString().Replace("'", "''") + "'";
}
return ret;
}
}
}