forked from mono/moma-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbissue.cs
412 lines (346 loc) · 13.4 KB
/
dbissue.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using Npgsql;
using System.Globalization;
using MoMA.Analyzer;
using ICSharpCode.SharpZipLib.Zip;
namespace MoMAIssueAdd
{
class Program
{
static int miss_id;
static int niex_id;
static int todo_id;
static int GetIssueType(NpgsqlConnection conn, string lookup_name)
{
using (NpgsqlCommand command = new NpgsqlCommand("SELECT id, lookup_name FROM issue_type WHERE lookup_name = :ln;", conn))
{
command.Parameters.Add(new NpgsqlParameter("ln", DbType.String));
command.Parameters[0].Value = lookup_name;
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Should just have the one row with id and lookup_name columns returned
if (dr.FieldCount != 2)
{
throw new Exception("Got too many columns");
}
if (!dr.GetString(1).Equals(lookup_name))
{
throw new Exception(String.Format ("Got wrong name: {0}", dr[1]));
}
return (dr.GetInt32(0));
}
throw new Exception(String.Format("Couldn't find {0}", lookup_name));
}
}
}
/* Irritatingly, the ID is 32 bits when selecting it, but 64
* when inserting.
*/
static long GetDefinitionID(NpgsqlConnection conn, string lookup_name, DateTime date)
{
using (NpgsqlCommand command = new NpgsqlCommand("SELECT id, lookup_name FROM moma_definition WHERE lookup_name = :ln;", conn))
{
command.Parameters.Add(new NpgsqlParameter("ln", DbType.String));
command.Parameters[0].Value = lookup_name;
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Should just have the one row with id and lookup_name columns returned
if (dr.FieldCount != 2)
{
throw new Exception("Got too many columns");
}
if (!dr.GetString(1).Equals(lookup_name))
{
throw new Exception(String.Format ("Got wrong name: {0}", dr[1]));
}
/* Make sure the date is correct */
Console.WriteLine ("Setting {0} date to {1}", lookup_name, date.ToString ());
using (NpgsqlCommand date_command = new NpgsqlCommand("UPDATE moma_definition SET create_date = :create_date WHERE id = :def_id;", conn)) {
date_command.Parameters.Add(new NpgsqlParameter("create_date", DbType.DateTime));
date_command.Parameters[0].Value = date;
date_command.Parameters.Add(new NpgsqlParameter("def_id", DbType.Int32));
date_command.Parameters[1].Value = dr.GetInt32(0);
date_command.ExecuteNonQuery ();
}
return (dr.GetInt32(0));
}
}
}
/* Not found, so insert it */
using (NpgsqlCommand command = new NpgsqlCommand("INSERT INTO moma_definition (id, lookup_name, display_name, description, create_date) VALUES (nextval ('moma_definition_id_seq'), :lookup_name, :display_name, :description, :create_date); SELECT currval('moma_definition_id_seq');", conn))
{
command.Parameters.Add(new NpgsqlParameter("lookup_name", DbType.String));
command.Parameters[0].Value = lookup_name;
command.Parameters.Add(new NpgsqlParameter("display_name", DbType.String));
command.Parameters[1].Value = lookup_name;
command.Parameters.Add(new NpgsqlParameter("description", DbType.String));
command.Parameters[2].Value = lookup_name;
command.Parameters.Add(new NpgsqlParameter("create_date", DbType.DateTime));
command.Parameters[3].Value = date;
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Should just have the one row with the newly inserted id returned
if (dr.FieldCount != 1)
{
throw new Exception("Got too many columns");
}
return (dr.GetInt64(0));
}
throw new Exception("Error inserting definition");
}
}
}
static int GetLatestDefinitionID(NpgsqlConnection conn)
{
using (NpgsqlCommand command = new NpgsqlCommand("SELECT id, lookup_name FROM moma_definition ORDER BY create_date DESC LIMIT 1;", conn))
{
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Should just have the one row with id and lookup_name columns returned
if (dr.FieldCount != 2)
{
throw new Exception("Got too many columns");
}
Console.WriteLine ("Latest definitions: {0}", dr.GetString(1));
return (dr.GetInt32(0));
}
}
}
throw new Exception("Error finding latest definition");
}
static int GetIssueID(NpgsqlConnection conn, int issue_type_id, string method_return_type, string method_namespace, string method_class, string method_name, string method_library)
{
using (NpgsqlCommand command = new NpgsqlCommand("SELECT id FROM issue WHERE issue_type_id = :issue_type_id AND method_return_type = :method_return_type AND method_namespace = :method_namespace AND method_class = :method_class AND method_name = :method_name AND method_library = :method_library;", conn))
{
command.Parameters.Add(new NpgsqlParameter("issue_type_id", DbType.Int32));
command.Parameters[0].Value = issue_type_id;
command.Parameters.Add(new NpgsqlParameter("method_return_type", DbType.String));
command.Parameters[1].Value = method_return_type;
command.Parameters.Add(new NpgsqlParameter("method_namespace", DbType.String));
command.Parameters[2].Value = method_namespace;
command.Parameters.Add(new NpgsqlParameter("method_class", DbType.String));
command.Parameters[3].Value = method_class;
command.Parameters.Add(new NpgsqlParameter("method_name", DbType.String));
command.Parameters[4].Value = method_name;
command.Parameters.Add(new NpgsqlParameter("method_library", DbType.String));
command.Parameters[5].Value = method_library;
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Should just have the one row with id returned, or 0 rows at all
if (dr.FieldCount != 1)
{
throw new Exception("Got too many columns");
}
return (dr.GetInt32(0));
}
return (0);
}
}
}
static long InsertIssue(NpgsqlConnection conn, int issue_type_id, string method_return_type, string method_namespace, string method_class, string method_name, string method_library)
{
using (NpgsqlCommand command = new NpgsqlCommand("INSERT INTO issue (id, issue_type_id, method_return_type, method_namespace, method_class, method_name, method_library) VALUES (nextval ('issue_id_seq'), :issue_type_id, :method_return_type, :method_namespace, :method_class, :method_name, :method_library); SELECT currval('issue_id_seq');", conn))
{
command.Parameters.Add(new NpgsqlParameter("issue_type_id", DbType.Int32));
command.Parameters[0].Value = issue_type_id;
command.Parameters.Add(new NpgsqlParameter("method_return_type", DbType.String));
command.Parameters[1].Value = method_return_type;
command.Parameters.Add(new NpgsqlParameter("method_namespace", DbType.String));
command.Parameters[2].Value = method_namespace;
command.Parameters.Add(new NpgsqlParameter("method_class", DbType.String));
command.Parameters[3].Value = method_class;
command.Parameters.Add(new NpgsqlParameter("method_name", DbType.String));
command.Parameters[4].Value = method_name;
command.Parameters.Add(new NpgsqlParameter("method_library", DbType.String));
command.Parameters[5].Value = method_library;
using (NpgsqlDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Should just have the one row with the newly inserted id returned
if (dr.FieldCount != 1)
{
throw new Exception("Got too many columns");
}
return (dr.GetInt64(0));
}
throw new Exception("Error inserting issue");
}
}
}
static void InsertIssueDefinition(NpgsqlConnection conn, long issue_id, long def_id)
{
using (NpgsqlCommand command = new NpgsqlCommand("INSERT INTO issue_definition (issue_id, def_id) VALUES (:issue_id, :def_id);", conn))
{
command.Parameters.Add(new NpgsqlParameter("issue_id", DbType.Int64));
command.Parameters[0].Value = issue_id;
command.Parameters.Add(new NpgsqlParameter("def_id", DbType.Int64));
command.Parameters[1].Value = def_id;
command.ExecuteNonQuery();
}
}
static void MarkLatestDefinition (NpgsqlConnection conn)
{
NpgsqlTransaction trans = conn.BeginTransaction();
int def_id = GetLatestDefinitionID (conn);
using (NpgsqlCommand command = new NpgsqlCommand("UPDATE issue SET is_latest_definition = false;", conn)) {
/* This can take a while, so stop Npgsql
* 'canceling statement due to user request'
*/
command.CommandTimeout = 600;
command.ExecuteNonQuery ();
}
using (NpgsqlCommand command = new NpgsqlCommand("UPDATE issue SET is_latest_definition = true WHERE id IN (SELECT issue_id FROM issue_definition WHERE def_id = :def_id);", conn)) {
/* This can take a while, so stop Npgsql
* 'canceling statement due to user request'
*/
command.CommandTimeout = 600;
command.Parameters.Add(new NpgsqlParameter("def_id", DbType.Int32));
command.Parameters[0].Value = def_id;
int rows = command.ExecuteNonQuery ();
Console.WriteLine ("Marked {0} rows as belonging to latest definitions", rows);
}
trans.Commit();
}
static void Main(string[] args)
{
//Npgsql.NpgsqlEventLog.Level = Npgsql.LogLevel.Debug;
//Npgsql.NpgsqlEventLog.LogName = "/tmp/npgsql-debug-log";
string connstr = "Server=hagbard.apathetic.discordia.org.uk;Database=moma;User ID=dick;Password=test";
NpgsqlConnection conn = new NpgsqlConnection(connstr);
conn.Open();
miss_id = GetIssueType(conn, "MISS");
niex_id = GetIssueType(conn, "NIEX");
todo_id = GetIssueType(conn, "TODO");
foreach (string f in args) {
InsertDefinition (conn, f);
}
/* Mark the issues from the latest definition */
MarkLatestDefinition (conn);
conn.Close();
}
static long GetDefinitionIDFromFile (NpgsqlConnection conn, string file)
{
ZipInputStream zs = new ZipInputStream (File.OpenRead (file));
ZipEntry ze;
while ((ze = zs.GetNextEntry ()) != null) {
if (ze.Name == "version.txt") {
StreamReader sr = new StreamReader (zs);
string version = sr.ReadLine ();
string date = sr.ReadLine ();
DateTime date_parsed = new DateTime(int.Parse(date.Substring(6, 2)) + 2000, int.Parse(date.Substring(0, 2)), int.Parse(date.Substring(3, 2)));
long defid = GetDefinitionID(conn, version, date_parsed);
Console.WriteLine ("Defs {0} date {1}", version, date_parsed.ToString ());
sr.Close();
zs.Close();
return defid;
}
}
throw new Exception(String.Format("Couldn't find version info in {0}", file));
}
static void InsertDefinition (NpgsqlConnection conn, string file)
{
Console.WriteLine ("Loading defs from {0}", file);
long def_id = GetDefinitionIDFromFile (conn, file);
string line;
StreamReader sr;
Stream mt;
Stream ni;
Stream mi;
DefinitionHandler.GetDefinitionStreamsFromBundle (file, out mt, out ni, out mi);
NpgsqlTransaction trans = conn.BeginTransaction();
sr = new StreamReader (mt);
while ((line = sr.ReadLine ()) != null) {
int split = line.IndexOf ("-");
string method = line.Substring (0, split);
InsertIssueIfNew (conn, method, todo_id, def_id, file);
}
sr.Close ();
Console.WriteLine ("Done TODO");
sr = new StreamReader (ni);
while ((line = sr.ReadLine ()) != null) {
InsertIssueIfNew (conn, line, niex_id, def_id, file);
}
sr.Close ();
Console.WriteLine ("Done NIEX");
sr = new StreamReader (mi);
while ((line = sr.ReadLine ()) != null) {
InsertIssueIfNew (conn, line, miss_id, def_id, file);
}
sr.Close ();
Console.WriteLine ("Done MISS");
mt.Close ();
ni.Close ();
mi.Close ();
trans.Commit();
}
static void InsertIssueIfNew (NpgsqlConnection conn, string rest, int iss_type, long def_id, string file)
{
string ns = "";
string class_name = "";
string method_name = "";
string return_type = "";
if (rest.Contains (" modopt(")) {
int modopt_start = rest.IndexOf (" modopt(");
int modopt_end = rest.IndexOf (")", modopt_start);
rest = rest.Substring (0, modopt_start) + rest.Substring (modopt_end + 1);
}
while (rest.Contains (" modreq(")) {
int modreq_start = rest.IndexOf (" modreq(");
int modreq_end = rest.IndexOf (")", modreq_start);
rest = rest.Substring (0, modreq_start) + rest.Substring (modreq_end + 1);
}
try {
string[] method_parts = rest.Split(' ');
return_type = method_parts[0];
string details = method_parts[1];
int colons = details.IndexOf("::");
if (colons != -1)
{
/* "::" was found */
method_name = details.Substring(colons + 2);
string[] ns_parts = details.Substring(0, colons).Split('.');
class_name = ns_parts[ns_parts.Length - 1];
for (int i = 0; i < ns_parts.Length - 1; i++)
{
if (ns.Length > 0)
{
ns += ".";
}
ns += ns_parts[i];
}
}
else
{
/* "::" not found */
Console.WriteLine ("Error parsing [{0}] (no colons) in file {1}", rest, file);
return;
}
} catch {
Console.WriteLine ("Error parsing [{0}] in file {1}", rest, file);
return;
}
long iss_id = GetIssueID(conn, iss_type, return_type, ns, class_name, method_name, "");
if (iss_id == 0)
{
iss_id = InsertIssue(conn, iss_type, return_type, ns, class_name, method_name, "");
} else {
}
InsertIssueDefinition(conn, iss_id, def_id);
}
}
}