Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Database handling tools using Npgsql directly

svn path=/trunk/moma-tool/; revision=118717
  • Loading branch information...
commit 9acf9171d41f0900279a1963ce26c79772d19919 1 parent ec5a9dc
@dickp dickp authored
Showing with 767 additions and 172 deletions.
  1. +9 −3 Makefile
  2. +346 −169 dbinsert.cs
  3. +412 −0 dbissue.cs
View
12 Makefile
@@ -11,10 +11,13 @@ TEST_SRCS=test.cs
TEST_ASSES=-r:$(ASS) $(ASS_ASSES)
TEST=test.exe
DBINS_SRCS=dbinsert.cs
-DBINS_ASSES=-r:$(ASS) $(ASS_ASSES)
+DBINS_ASSES=-r:Npgsql -r:System.Data -r:../moma/MoMA.Analyzer/bin/MoMA.Analyzer.dll -r:ICSharpCode.SharpZipLib
DBINS=dbinsert.exe
+DBISS_SRCS=dbissue.cs
+DBISS_ASSES=-r:Npgsql -r:System.Data -r:../moma/MoMA.Analyzer/bin/MoMA.Analyzer.dll -r:ICSharpCode.SharpZipLib
+DBISS=dbissue.exe
-all: $(ASS) $(TEST) $(DBINS)
+all: $(DBINS) $(DBISS)
$(ASS): $(ASS_SRCS)
gmcs -out:$(ASS) -target:library $(ASS_SRCS) $(ASS_ASSES)
@@ -22,5 +25,8 @@ $(ASS): $(ASS_SRCS)
$(TEST): $(TEST_SRCS) $(ASS)
gmcs -out:$(TEST) $(TEST_SRCS) $(TEST_ASSES)
-$(DBINS): $(DBINS_SRCS) $(ASS)
+$(DBINS): $(DBINS_SRCS)
gmcs -out:$(DBINS) $(DBINS_SRCS) $(DBINS_ASSES)
+
+$(DBISS): $(DBISS_SRCS)
+ gmcs -out:$(DBISS) $(DBISS_SRCS) $(DBISS_ASSES)
View
515 dbinsert.cs
@@ -1,189 +1,366 @@
-using System;
+using System;
+using System.Collections.Generic;
using System.IO;
+using System.Text;
+
+using System.Data;
+using Npgsql;
using System.Globalization;
-using System.Collections;
-using System.Collections.Generic;
-using System.Reflection;
-using Castle.ActiveRecord;
-using Castle.ActiveRecord.Framework.Config;
-using MomaTool.Database;
-namespace MomaTool
+namespace MoMADBAdd
{
- public class DBInsert
+ class Program
+ {
+ static int miss_id;
+ static int niex_id;
+ static int pinv_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));
+ }
+ }
+ }
+
+ static int GetDefinitionID(NpgsqlConnection conn, string lookup_name)
+ {
+ 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]));
+ }
+
+ return (dr.GetInt32(0));
+ }
+
+ throw new Exception(String.Format("Couldn't find {0}", lookup_name));
+ }
+ }
+ }
+
+ 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 InsertReportMetadata(NpgsqlConnection conn, long report_id)
+ {
+ using (NpgsqlCommand command = new NpgsqlCommand("INSERT INTO report_metadata (report_id) VALUES (:report_id);", conn))
+ {
+ command.Parameters.Add(new NpgsqlParameter("report_id", DbType.Int64));
+ command.Parameters[1].Value = report_id;
+
+ int rows = command.ExecuteNonQuery();
+ }
+ }
+
+ static long InsertReport(NpgsqlConnection conn, int def_id, string report_filename, DateTime report_date,
+ string reporter_ip, string reporter_name, string reporter_email,
+ string reporter_organization, string reporter_homepage,
+ string reporter_comments)
+ {
+ using (NpgsqlCommand command = new NpgsqlCommand("INSERT INTO report (id, moma_definition_id, report_filename, report_date, reporter_ip, reporter_name, reporter_email, reporter_organization, reporter_homepage, reporter_comments) VALUES (nextval ('report_id_seq'), :defid, :report_filename, :report_date, :reporter_ip, :reporter_name, :reporter_email, :reporter_organization, :reporter_homepage, :reporter_comments); SELECT currval('report_id_seq');", conn))
+ {
+ command.Parameters.Add(new NpgsqlParameter("defid", DbType.Int32));
+ command.Parameters[0].Value = def_id;
+ command.Parameters.Add(new NpgsqlParameter("report_filename", DbType.String));
+ command.Parameters[1].Value = report_filename;
+ command.Parameters.Add(new NpgsqlParameter("report_date", DbType.DateTime));
+ command.Parameters[2].Value = report_date;
+ command.Parameters.Add(new NpgsqlParameter("reporter_ip", DbType.String));
+ command.Parameters[3].Value = reporter_ip;
+ command.Parameters.Add(new NpgsqlParameter("reporter_name", DbType.String));
+ command.Parameters[4].Value = reporter_name;
+ command.Parameters.Add(new NpgsqlParameter("reporter_email", DbType.String));
+ command.Parameters[5].Value = reporter_email;
+ command.Parameters.Add(new NpgsqlParameter("reporter_organization", DbType.String));
+ command.Parameters[6].Value = reporter_organization;
+ command.Parameters.Add(new NpgsqlParameter("reporter_homepage", DbType.String));
+ command.Parameters[7].Value = reporter_homepage;
+ command.Parameters.Add(new NpgsqlParameter("reporter_comments", DbType.String));
+ command.Parameters[8].Value = reporter_comments;
+
+ 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");
+ }
+
+ long rep_id = dr.GetInt64(0);
+
+ InsertReportMetadata (conn, rep_id);
+
+ return (rep_id);
+ }
+
+ throw new Exception("Error inserting report");
+ }
+ }
+ }
+
+ static void InsertIssueReport(NpgsqlConnection conn, long issue_id, long report_id)
+ {
+ using (NpgsqlCommand command = new NpgsqlCommand("INSERT INTO issue_report (issue_id, report_id) VALUES (:issue_id, :report_id);", conn))
+ {
+ command.Parameters.Add(new NpgsqlParameter("issue_id", DbType.Int64));
+ command.Parameters[0].Value = issue_id;
+ command.Parameters.Add(new NpgsqlParameter("report_id", DbType.Int64));
+ command.Parameters[1].Value = report_id;
+
+ int rows = command.ExecuteNonQuery();
+ }
+ }
+
+ static void Main(string[] args)
+ {
+ //Npgsql.NpgsqlEventLog.Level = Npgsql.LogLevel.Debug;
+ //Npgsql.NpgsqlEventLog.LogName = "c:\\cygwin\\tmp\\npgsql-debug-log2";
+
+ 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");
+ pinv_id = GetIssueType(conn, "PINV");
+ todo_id = GetIssueType(conn, "TODO");
+
+ foreach (string f in args) {
+ ParseReport (conn, f);
+ }
+
+ conn.Close();
+ }
+
+ static void ParseReport (NpgsqlConnection conn, string file)
{
- static IssueType miss;
- static IssueType niex;
- static IssueType pinv;
- static IssueType todo;
-
- public static void Main (string[] args)
- {
- XmlConfigurationSource source = new XmlConfigurationSource ("test_castle_config.xml");
- Assembly ass = Assembly.Load ("MomaTool.Database");
-
- ActiveRecordStarter.Initialize (ass, source);
- //ActiveRecordStarter.CreateSchema ();
-
- miss = IssueType.FindByLookupName ("MISS");
- if(miss == null) {
- Console.WriteLine ("Can't find issue type: MISS");
- Environment.Exit (1);
- }
-
- niex = IssueType.FindByLookupName ("NIEX");
- if(niex == null) {
- Console.WriteLine ("Can't find issue type: NIEX");
- Environment.Exit (1);
- }
+ using (FileStream fs = File.OpenRead (file)) {
+ Dictionary<string, string> meta = new Dictionary<string, string>();
+ StreamReader stream = new StreamReader(fs);
+ CultureInfo cult = CultureInfo.CreateSpecificCulture("en-US");
+ string date = stream.ReadLine();
+ DateTime date_parsed = DateTime.Parse(date, cult);
+ string ip = stream.ReadLine();
+ string r = ReadMeta(stream, meta);
- pinv = IssueType.FindByLookupName ("PINV");
- if(pinv == null) {
- Console.WriteLine ("Can't find issue type: PINV");
- Environment.Exit (1);
- }
+ int defid = GetDefinitionID(conn, meta["@Definitions"]);
+
+ NpgsqlTransaction trans = conn.BeginTransaction();
- todo = IssueType.FindByLookupName ("TODO");
- if(todo == null) {
- Console.WriteLine ("Can't find issue type: TODO");
- Environment.Exit (1);
+ long rep_id = InsertReport(conn, defid, Path.GetFileName(file), date_parsed, ip, meta["@Name"], meta["@Email"], meta["@Organization"], meta["@HomePage"], meta["@Comments"]);
+
+ for (; r != null && r.Length > 6; r = stream.ReadLine())
+ {
+ int iss_type;
+
+ if (r[r.Length - 1] == '\r')
+ {
+ r = r.Substring(0, r.Length - 1);
}
- foreach (string f in args) {
- /*Report dbrep = */ParseReport (f);
+ switch (r.Substring(0, 6))
+ {
+ case "[TODO]":
+ iss_type = todo_id;
+ break;
+ case "[NIEX]":
+ iss_type = niex_id;
+ break;
+ case "[MISS]":
+ iss_type = miss_id;
+ break;
+ case "[PINV]":
+ iss_type = pinv_id;
+ break;
+ default:
+ continue;
}
- }
-
- static Report ParseReport (string file)
- {
- Report dbrep;
-
- using (FileStream fs = File.OpenRead (file)) {
- MomaDefinition dbdef;
- Dictionary<string,string> meta = new Dictionary<string,string>();
-
- StreamReader stream = new StreamReader (fs);
- CultureInfo cult = CultureInfo.CreateSpecificCulture ("en-US");
-
- string date = stream.ReadLine ();
- DateTime date_parsed = DateTime.Parse (date, cult);
- string ip = stream.ReadLine ();
- string r = ReadMeta (stream, meta);
-
- try {
- dbdef = MomaDefinition.GetOrCreate (meta["@Definitions"]);
- } catch {
- Console.WriteLine ("Exception when creating MomaDefinition: [{0}]", meta["@Definitions"]);
-
- throw;
- }
-
- try {
- dbrep = Report.CreateOrUpdate (Path.GetFileName (file), dbdef, date_parsed, ip, meta["@Name"], meta["@Email"], meta["@Organization"], meta["@HomePage"], meta["@Comments"]);
- } catch {
- Console.WriteLine ("Exception when creating Report: Date {0}, IP {1}, Name [{2}], Email [{3}], Organisation [{4}], Home page [{5}], Comments [{6}]", date_parsed.ToString (), ip, meta["@Name"], meta["@Email"], meta["@Organization"], meta["@HomePage"], meta["@Comments"]);
- throw;
- }
+ string rest = r.Substring(7);
-
- for(; r != null && r.Length > 6; r = stream.ReadLine ()) {
- IssueType type;
-
- if (r[r.Length - 1] == '\r') {
- r = r.Substring (0, r.Length - 1);
- }
-
- switch(r.Substring (0, 6)) {
- case "[TODO]":
- type = todo;
- break;
- case "[NIEX]":
- type = niex;
- break;
- case "[MISS]":
- type = miss;
- break;
- case "[PINV]":
- type = pinv;
- break;
- default:
- continue;
- }
-
- string rest = r.Substring (7);
-
- string ns = "";
- string class_name = "";
- string method_name = "";
- string lib = "";
- string return_type = "";
-
- 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 */
- method_parts = rest.Split ('-');
- method_name = method_parts[0].Split (new char[]{' '}, 2)[1];
- lib = method_parts[1];
- }
- } catch {
- Console.WriteLine ("Error parsing [{0}] in file {1}", r, file);
- /* Carry on parsing,
- * no need to error
- * out here
- */
- continue;
- }
+ string ns = "";
+ string class_name = "";
+ string method_name = "";
+ string lib = "";
+ string return_type = "";
- try {
- Issue.Create (dbrep, type, return_type, ns, class_name, method_name, lib);
- } catch {
- Console.WriteLine ("Exception when creating Issue in report {0}: {1} Return type [{2}], Namespace [{3}], Class [{4}], Method [{5}], Library [{6}]", file, type.DisplayName, return_type, ns, class_name, method_name, lib);
+ 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];
- throw;
+ for (int i = 0; i < ns_parts.Length - 1; i++)
+ {
+ if (ns.Length > 0)
+ {
+ ns += ".";
}
+ ns += ns_parts[i];
+ }
}
+ else
+ {
+ /* "::" not found */
+ method_parts = rest.Split('-');
+ method_name = method_parts[0].Split(new char[] { ' ' }, 2)[1];
+ lib = method_parts[1];
+ }
+ } catch {
+ Console.WriteLine ("Error parsing [{0}] in file {1}", r, file);
+ /* Carry on parsing,
+ * no need to error
+ * out here
+ */
+ continue;
}
- return(dbrep);
- }
-
- static string ReadMeta (StreamReader stream, Dictionary<string,string> meta)
- {
- string s;
-
- while ((s = stream.ReadLine ()) != null) {
- if (s.Length == 0 || s[0] == '[') {
- return s;
- }
-
- if (s[0] != '@') {
- return s;
- }
-
- int p = s.IndexOf (':');
- string v = s.Substring (p + 2);
- meta[s.Substring (0, p)] = v;
+ long iss_id = GetIssueID(conn, iss_type, return_type, ns, class_name, method_name, lib);
+ if (iss_id == 0)
+ {
+ iss_id = InsertIssue(conn, iss_type, return_type, ns, class_name, method_name, lib);
}
-
- return s;
- }
- }
+
+ InsertIssueReport(conn, iss_id, rep_id);
+ }
+
+ trans.Commit();
+ }
+ }
+
+ static string ReadMeta(StreamReader stream, Dictionary<string, string> meta)
+ {
+ string s;
+
+ while ((s = stream.ReadLine()) != null)
+ {
+ if (s.Length == 0 || s[0] == '[')
+ {
+ return s;
+ }
+ if (s[0] != '@')
+ {
+ return s;
+ }
+
+ int p = s.IndexOf(':');
+ string v = s.Substring(p + 2);
+ meta[s.Substring(0, p)] = v;
+ }
+
+ return s;
+ }
+ }
}
View
412 dbissue.cs
@@ -0,0 +1,412 @@
+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);
+ }
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.