//takes string p, and appends a check digit., and converts it to a long //p will be a cocatentation of rnd+namespace+partitionId internal long Verhoefferise(string p) { string VerhoefferisedResult; VerhoefferisedResult = p + Verhoeff.GenerateVerhoeff(p); return(long.Parse(VerhoefferisedResult)); }
private string RepDumpFile = "SCTID_Repository_Dump.txt"; // COMMENT: BH - consistantly case similar scoped variables //Constructor will set the current Directory to launch directory, and make sure a DB exists public SCTIdRepository(int initialiserNamespace) { System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); // COMMENT: BH - Could break apart this method (cyclomatic complexity) its a bit large. First part is the "Check if exists" then create - following part is Init. //If the SCTIDRepo.db doesn't exist, attempt to create one. if (!File.Exists(RepoFile)) { try { SQLiteConnection.CreateFile(RepoFile); const string CreateTableSchema = @"DROP TABLE IF EXISTS SCTIDs; CREATE TABLE SCTIDs (SCTID LONG,Bean INTEGER,NameSpace INTEGER,IdType TEXT); CREATE INDEX IF NOT EXISTS ix_SCTIDs_SCTID ON SCTIDs(SCTID); CREATE INDEX IF NOT EXISTS ix_SCTIDs_Bean ON SCTIDs(Bean); CREATE INDEX IF NOT EXISTS ix_SCTIDs_NameSpace ON SCTIDs(NameSpace); CREATE INDEX IF NOT EXISTS ix_SCTIDs_IdType ON SCTIDs(IdType);"; // COMMENT: BH - as above consistantly case similar scoped variables. Inside a method should probably start with Lower case using (SQLiteConnection cnn = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand(CreateTableSchema, cnn)) { cnn.Open(); cmd.ExecuteNonQuery(); } } } catch (Exception e) { Console.WriteLine("{0} Problem setting up the database:", e); } } //Check if the DB has been initialised for the given namesapce. //(initialising involves seeding with 3 SCTIDs having a Bean of 0 string CheckIfInitialisedForNameSpace = "select count(*) from SCTIDs where NameSpace = @NameSpace"; using (SQLiteConnection cnn = new SQLiteConnection(connectionString)) { using (SQLiteCommand cmd = new SQLiteCommand(CheckIfInitialisedForNameSpace, cnn)) { cmd.Parameters.AddWithValue("@NameSpace", initialiserNamespace); cnn.Open(); var NumberOfEntriesForNameSpace = int.Parse(cmd.ExecuteScalar().ToString()); //There should at least 3. More if the db has been used. if (NumberOfEntriesForNameSpace < 3) { //initialise with same base 1 SCTIDs for the given namespace ReserveId("1" + initialiserNamespace + "10" + Verhoeff.GenerateVerhoeff("1" + initialiserNamespace + "10")); ReserveId("1" + initialiserNamespace + "11" + Verhoeff.GenerateVerhoeff("1" + initialiserNamespace + "11")); ReserveId("1" + initialiserNamespace + "12" + Verhoeff.GenerateVerhoeff("1" + initialiserNamespace + "12")); } } } }