protected virtual IEnumerable <UoeDatabaseConnection> GetDatabaseConnections(bool addDatabasesInCurrentDirectory, bool throwWhenNoDatabaseFound) { var nbYield = 0; foreach (var connection in DatabaseConnections.ToNonNullEnumerable().Where(c => !string.IsNullOrEmpty(c))) { foreach (var databaseConnection in UoeDatabaseConnection.GetConnectionStrings(connection)) { nbYield++; yield return(databaseConnection); } } var ope = GetOperator(); foreach (var path in DatabasePaths.ToNonNullEnumerable().Where(d => !string.IsNullOrEmpty(d))) { nbYield++; var loc = new UoeDatabaseLocation(path); loc.ThrowIfNotExist(); yield return(ope.GetDatabaseConnection(loc)); } if (!addDatabasesInCurrentDirectory && throwWhenNoDatabaseFound) { throw new CommandException($"You must specify a database connection string using the option {OptionDatabaseConnectionShortName.PrettyQuote()} or a database path by using the option {OptionDatabaseFileShortName.PrettyQuote()}."); } if (nbYield > 0 || !addDatabasesInCurrentDirectory) { yield break; } foreach (var databaseLocation in GetDatabaseLocationsFromCd(throwWhenNoDatabaseFound)) { databaseLocation.ThrowIfNotExist(); yield return(ope.GetDatabaseConnection(databaseLocation)); } }
public void ValidateLogicalName(string input, bool exception) { if (exception) { Assert.ThrowsException <UoeDatabaseException>(() => UoeDatabaseLocation.ValidateLogicalName(input)); } else { UoeDatabaseLocation.ValidateLogicalName(input); } }
public void OeExecution_Test_DbConnection_ok() { if (!GetEnvExecution(out UoeExecutionEnv env)) { return; } env.UseProgressCharacterMode = true; // generate temp base var db = new UoeDatabaseOperator(env.DlcDirectoryPath); var dbPn = new UoeDatabaseLocation(Path.Combine(TestFolder, "test1.db")); db.Create(dbPn); Assert.IsTrue(dbPn.Exists()); // generate temp base var dbPn2 = new UoeDatabaseLocation(Path.Combine(TestFolder, "test2.db")); db.Create(dbPn2); Assert.IsTrue(dbPn2.Exists()); // try if connected well and can manage aliases env.DatabaseConnections = new [] { db.GetDatabaseConnection(dbPn), db.GetDatabaseConnection(dbPn2) }; env.DatabaseAliases = new List <IUoeExecutionDatabaseAlias> { new UoeExecutionDatabaseAlias { DatabaseLogicalName = "test1", AliasLogicalName = "alias1" }, new UoeExecutionDatabaseAlias { DatabaseLogicalName = "test1", AliasLogicalName = "alias2" }, new UoeExecutionDatabaseAlias { DatabaseLogicalName = "test2", AliasLogicalName = "alias3" } }; using (var exec = new UoeExecutionCustomTest(env)) { exec.NeedDatabaseConnection = true; exec.ProgramContent = @" DEFINE VARIABLE li_db AS INTEGER NO-UNDO. REPEAT li_db = 1 TO NUM-ALIASES: PUT UNFORMATTED ALIAS(li_db) SKIP. END."; exec.ExecuteNoWait(); exec.WaitForExit(); Assert.IsFalse(exec.ExecutionHandledExceptions, "ok"); Assert.AreEqual("dictdb alias1 alias2 alias3", new StringBuilder(exec.Output).CliCompactWhitespaces().ToString()); } env.Dispose(); }
protected virtual IEnumerable <UoeDatabaseLocation> GetDatabaseLocationsFromCd(bool throwWhenNoDatabaseFound) { var list = Directory.EnumerateFiles(Directory.GetCurrentDirectory(), $"*{UoeDatabaseLocation.Extension}", SearchOption.TopDirectoryOnly).ToList(); if (list.Count == 0) { if (!throwWhenNoDatabaseFound) { yield break; } throw new CommandException($"No database file {UoeDatabaseLocation.Extension.PrettyQuote()} found in the current folder: {Directory.GetCurrentDirectory().PrettyQuote()}. Initialize a new database using the command: {typeof(DatabaseCreateCommand).GetFullCommandLine<MainCommand>().PrettyQuote()}."); } foreach (var path in list) { var databaseLocation = new UoeDatabaseLocation(path); Log?.Debug($"Adding new database location from current directory: {databaseLocation.ToString().PrettyQuote()}."); yield return(databaseLocation); } }
public void OeExecutionDbExtractTableAndSequenceListTest_Dump_db() { if (!GetEnvExecution(out UoeExecutionEnv env)) { return; } var base1Db = new UoeDatabaseLocation(Path.Combine(TestFolder, "base1.db")); var base2Db = new UoeDatabaseLocation(Path.Combine(TestFolder, "base2.db")); if (!base1Db.Exists()) { var dfPath = Path.Combine(TestFolder, "base1.df"); File.WriteAllText(dfPath, "ADD SEQUENCE \"sequence1\"\n INITIAL 0\n INCREMENT 1\n CYCLE-ON-LIMIT no\n\nADD TABLE \"table1\"\n AREA \"Schema Area\"\n DESCRIPTION \"table one\"\n DUMP-NAME \"table1\"\n\nADD FIELD \"field1\" OF \"table1\" AS character \n DESCRIPTION \"field one\"\n FORMAT \"x(8)\"\n INITIAL \"\"\n POSITION 2\n MAX-WIDTH 16\n ORDER 10\n\nADD INDEX \"idx_1\" ON \"table1\" \n AREA \"Schema Area\"\n PRIMARY\n INDEX-FIELD \"field1\" ASCENDING"); using (var dbAdministrator = new UoeDatabaseAdministrator(env.DlcDirectoryPath)) { dbAdministrator.CreateWithDf(base1Db, dfPath); dbAdministrator.CreateWithDf(base2Db, dfPath); } } env.DatabaseConnections = new [] { UoeDatabaseConnection.NewSingleUserConnection(base1Db), UoeDatabaseConnection.NewSingleUserConnection(base2Db) }; env.DatabaseAliases = new List <IUoeExecutionDatabaseAlias> { new UoeExecutionDatabaseAlias { DatabaseLogicalName = "base1", AliasLogicalName = "alias1" } }; using (var exec = new UoeExecutionDbExtractTableCrcAndSequenceList(env)) { exec.ExecuteNoWait(); exec.WaitForExit(); Assert.IsFalse(exec.ExecutionHandledExceptions, "ExecutionHandledExceptions"); Assert.IsFalse(exec.DatabaseConnectionFailed, "DbConnectionFailed"); Assert.AreEqual("base1.sequence1,alias1.sequence1,base2.sequence1", string.Join(",", exec.Sequences), "sequences"); Assert.AreEqual("base1.table1,alias1.table1,base1._Sequence,alias1._Sequence,base2.table1,base2._Sequence", string.Join(",", exec.TablesCrc.Keys), "tables"); } env.Dispose(); }
public void Execute() { if (!GetEnvExecution(out UoeExecutionEnv env)) { return; } var base1Db = new UoeDatabaseLocation(Path.Combine(TestFolder, "base1.db")); var base2Db = new UoeDatabaseLocation(Path.Combine(TestFolder, "base2.db")); var beforeSchemaLoad = DateTime.Now; if (!base1Db.Exists()) { var dfPath = Path.Combine(TestFolder, "base1.df"); File.WriteAllText(dfPath, "ADD SEQUENCE \"sequence1\"\n INITIAL 0\n INCREMENT 1\n CYCLE-ON-LIMIT no\n\nADD TABLE \"table1\"\n AREA \"Schema Area\"\n DESCRIPTION \"table one\"\n DUMP-NAME \"table1\"\n\nADD FIELD \"field1\" OF \"table1\" AS character \n DESCRIPTION \"field one\"\n FORMAT \"x(8)\"\n INITIAL \"\"\n POSITION 2\n MAX-WIDTH 16\n ORDER 10\n\nADD INDEX \"idx_1\" ON \"table1\" \n AREA \"Schema Area\"\n PRIMARY\n INDEX-FIELD \"field1\" ASCENDING"); using (var dbAdministrator = new UoeDatabaseAdministrator(env.DlcDirectoryPath)) { dbAdministrator.CreateWithDf(base1Db, dfPath); dbAdministrator.CreateWithDf(base2Db, dfPath); } } var afterSchemaLoad = DateTime.Now; env.DatabaseConnections = new [] { UoeDatabaseConnection.NewSingleUserConnection(base1Db), UoeDatabaseConnection.NewSingleUserConnection(base2Db) }; using (var exec = new UoeExecutionDbExtractLastSchemaUpdate(env)) { exec.ExecuteNoWait(); exec.WaitForExit(); Assert.IsFalse(exec.ExecutionHandledExceptions, "ExecutionHandledExceptions"); Assert.IsFalse(exec.DatabaseConnectionFailed, "DbConnectionFailed"); Assert.AreEqual(2, exec.LastSchemaUpdates.Count); Assert.IsTrue(beforeSchemaLoad.CompareTo(exec.LastSchemaUpdates["base1"]) < 0, $"{beforeSchemaLoad} > {exec.LastSchemaUpdates["base1"]}"); Assert.IsTrue(afterSchemaLoad.CompareTo(exec.LastSchemaUpdates["base1"]) > 0, $"{exec.LastSchemaUpdates["base1"]} > {afterSchemaLoad}"); } env.Dispose(); }
public void UoeDatabaseLocation_() { var loc = UoeDatabaseLocation.FromOtherFilePath(Path.Combine(TestFolder, "data.st")); Assert.AreEqual(Path.Combine(TestFolder, "data.db"), loc.FullPath); Assert.AreEqual("data", loc.PhysicalName); Assert.AreEqual(TestFolder, loc.DirectoryPath); Assert.AreEqual(Path.Combine(TestFolder, "data.st"), loc.StructureFileFullPath); Assert.IsFalse(loc.Exists()); Assert.ThrowsException <UoeDatabaseException>(() => loc.ThrowIfNotExist()); var dbPath = Path.Combine(TestFolder, "data.db"); loc = new UoeDatabaseLocation(dbPath); Assert.AreEqual(dbPath, loc.FullPath); Assert.IsFalse(loc.Exists()); Directory.CreateDirectory(TestFolder); File.WriteAllText(dbPath, ""); Assert.IsTrue(loc.Exists()); loc.ThrowIfNotExist(); }
public void Execute() { if (!GetEnvExecution(out UoeExecutionEnv env)) { return; } var base1Db = new UoeDatabaseLocation(Path.Combine(TestFolder, "base1.db")); if (!base1Db.Exists()) { var dfPath = Path.Combine(TestFolder, "base1.df"); File.WriteAllBytes(dfPath, Resources.Resources.GetBytesFromResource("Database.complete.df")); using (var dbAdministrator = new UoeDatabaseAdministrator(env.DlcDirectoryPath)) { dbAdministrator.CreateWithDf(base1Db, dfPath); } } env.DatabaseConnections = new [] { UoeDatabaseConnection.NewSingleUserConnection(base1Db) }; using (var exec = new UoeExecutionDbExtractDefinition(env)) { exec.ExecuteNoWait(); exec.WaitForExit(); Assert.IsFalse(exec.ExecutionHandledExceptions, exec.ExecutionHandledExceptions ? exec.HandledExceptions[0].ToString() : "ok"); Assert.IsFalse(exec.DatabaseConnectionFailed, "DbConnectionFailed"); var db = exec.GetDatabases()[0]; Assert.AreEqual(DatabaseBlockSize.S4096, db.BlockSize); Assert.AreEqual("iso8859-1", db.Charset); Assert.AreEqual("basic", db.Collation); Assert.AreEqual("base1", db.LogicalName); Assert.AreEqual("base1", db.PhysicalName); Assert.AreEqual(2, db.Sequences.Count); Assert.AreEqual(true, db.Sequences[0].CycleOnLimit); Assert.AreEqual(2, db.Sequences[0].Increment); Assert.AreEqual(20, db.Sequences[0].Initial); Assert.AreEqual(200, db.Sequences[0].Max); Assert.AreEqual(null, db.Sequences[0].Min); Assert.AreEqual("theseq1", db.Sequences[0].Name); Assert.AreEqual(2, db.Tables.Count); Assert.AreEqual("Data Area", db.Tables[0].Area); Assert.AreEqual(5575, db.Tables[0].Crc); Assert.AreEqual("thedesc1", db.Tables[0].Description); Assert.AreEqual("thedump1", db.Tables[0].DumpName); Assert.AreEqual("theforeignname", db.Tables[0].Foreign); Assert.AreEqual(false, db.Tables[0].Frozen); Assert.AreEqual(false, db.Tables[0].Hidden); Assert.AreEqual("thelabel1", db.Tables[0].Label); Assert.AreEqual("T", db.Tables[0].LabelAttribute); Assert.AreEqual("thetable1", db.Tables[0].Name); Assert.AreEqual("thereplication1", db.Tables[0].Replication); Assert.AreEqual("false", db.Tables[0].ValidationExpression); Assert.AreEqual("not true", db.Tables[0].ValidationMessage); Assert.AreEqual("R", db.Tables[0].ValidationMessageAttribute); Assert.AreEqual(3, db.Tables[0].Fields.Count); Assert.AreEqual(true, db.Tables[0].Fields[0].CaseSensitive); Assert.AreEqual(null, db.Tables[0].Fields[0].ClobCharset); Assert.AreEqual(null, db.Tables[0].Fields[0].ClobCollation); Assert.AreEqual(0, db.Tables[0].Fields[0].ClobType); Assert.AreEqual("thecollabel1", db.Tables[0].Fields[0].ColumnLabel); Assert.AreEqual("T", db.Tables[0].Fields[0].ColumnLabelAttribute); Assert.AreEqual(UoeDatabaseDataType.Character, db.Tables[0].Fields[0].DataType); Assert.AreEqual(0, db.Tables[0].Fields[0].Decimals); Assert.AreEqual("thedesc1", db.Tables[0].Fields[0].Description); Assert.AreEqual(2, db.Tables[0].Fields[0].Extent); Assert.AreEqual("x(10)", db.Tables[0].Fields[0].Format); Assert.AreEqual("T", db.Tables[0].Fields[0].FormatAttribute); Assert.AreEqual("thehelp1", db.Tables[0].Fields[0].Help); Assert.AreEqual("T", db.Tables[0].Fields[0].HelpAttribute); Assert.AreEqual("theinitial1", db.Tables[0].Fields[0].InitialValue); Assert.AreEqual("T", db.Tables[0].Fields[0].InitialValueAttribute); Assert.AreEqual("thelabel1", db.Tables[0].Fields[0].Label); Assert.AreEqual("T", db.Tables[0].Fields[0].LabelAttribute); Assert.AreEqual(null, db.Tables[0].Fields[0].LobArea); Assert.AreEqual(0, db.Tables[0].Fields[0].LobBytes); Assert.AreEqual(null, db.Tables[0].Fields[0].LobSize); Assert.AreEqual(true, db.Tables[0].Fields[0].Mandatory); Assert.AreEqual("thefield1", db.Tables[0].Fields[0].Name); Assert.AreEqual(10, db.Tables[0].Fields[0].Order); Assert.AreEqual(2, db.Tables[0].Fields[0].Position); Assert.AreEqual(null, db.Tables[0].Fields[0].Triggers); Assert.AreEqual(44, db.Tables[0].Fields[0].Width); Assert.AreEqual(false, db.Tables[0].Fields[1].CaseSensitive); Assert.AreEqual(null, db.Tables[0].Fields[1].ClobCharset); Assert.AreEqual(null, db.Tables[0].Fields[1].ClobCollation); Assert.AreEqual(0, db.Tables[0].Fields[1].ClobType); Assert.AreEqual(null, db.Tables[0].Fields[1].ColumnLabel); Assert.AreEqual(null, db.Tables[0].Fields[1].ColumnLabelAttribute); Assert.AreEqual(UoeDatabaseDataType.Integer, db.Tables[0].Fields[1].DataType); Assert.AreEqual(0, db.Tables[0].Fields[1].Decimals); Assert.AreEqual("", db.Tables[0].Fields[1].Description); Assert.AreEqual(0, db.Tables[0].Fields[1].Extent); Assert.AreEqual("9999", db.Tables[0].Fields[1].Format); Assert.AreEqual(null, db.Tables[0].Fields[1].FormatAttribute); Assert.AreEqual("", db.Tables[0].Fields[1].Help); Assert.AreEqual("", db.Tables[0].Fields[1].HelpAttribute); Assert.AreEqual("0", db.Tables[0].Fields[1].InitialValue); Assert.AreEqual(null, db.Tables[0].Fields[1].InitialValueAttribute); Assert.AreEqual(null, db.Tables[0].Fields[1].Label); Assert.AreEqual(null, db.Tables[0].Fields[1].LabelAttribute); Assert.AreEqual(null, db.Tables[0].Fields[1].LobArea); Assert.AreEqual(0, db.Tables[0].Fields[1].LobBytes); Assert.AreEqual(null, db.Tables[0].Fields[1].LobSize); Assert.AreEqual(true, db.Tables[0].Fields[1].Mandatory); Assert.AreEqual("thefield2", db.Tables[0].Fields[1].Name); Assert.AreEqual(20, db.Tables[0].Fields[1].Order); Assert.AreEqual(3, db.Tables[0].Fields[1].Position); Assert.AreEqual(1, db.Tables[0].Fields[1].Triggers.Count); Assert.AreEqual(4, db.Tables[0].Fields[1].Width); Assert.AreEqual(3, db.Tables[0].Indexes.Count); Assert.AreEqual(true, db.Tables[0].Indexes[0].Active); Assert.AreEqual("Index Area", db.Tables[0].Indexes[0].Area); Assert.AreEqual(36396, db.Tables[0].Indexes[0].Crc); Assert.AreEqual("thedesc1", db.Tables[0].Indexes[0].Description); Assert.AreEqual("theindex1", db.Tables[0].Indexes[0].Name); Assert.AreEqual(1, db.Tables[0].Indexes[0].Fields.Count); Assert.AreEqual(false, db.Tables[0].Indexes[0].Primary); Assert.AreEqual(false, db.Tables[0].Indexes[0].Unique); Assert.AreEqual(true, db.Tables[0].Indexes[0].Word); Assert.AreEqual(true, db.Tables[0].Indexes[1].Active); Assert.AreEqual("Index Area", db.Tables[0].Indexes[1].Area); Assert.AreEqual(16650, db.Tables[0].Indexes[1].Crc); Assert.AreEqual("desc2", db.Tables[0].Indexes[1].Description); Assert.AreEqual(1, db.Tables[0].Indexes[1].Fields.Count); Assert.AreEqual(false, db.Tables[0].Indexes[1].Fields[0].Abbreviate); Assert.AreEqual(true, db.Tables[0].Indexes[1].Fields[0].Ascending); Assert.AreEqual("theindex2", db.Tables[0].Indexes[1].Name); Assert.AreEqual(true, db.Tables[0].Indexes[1].Primary); Assert.AreEqual(true, db.Tables[0].Indexes[1].Unique); Assert.AreEqual(false, db.Tables[0].Indexes[1].Word); Assert.AreEqual(1, db.Tables[0].Triggers.Count); Assert.AreEqual(0, db.Tables[0].Triggers[0].Crc); Assert.AreEqual(UoeDatabaseTriggerEvent.Create, db.Tables[0].Triggers[0].Event); Assert.AreEqual(true, db.Tables[0].Triggers[0].Overridable); Assert.AreEqual("theproc1", db.Tables[0].Triggers[0].Procedure); Assert.AreEqual("Data Area", db.Tables[1].Area); Assert.AreEqual(94, db.Tables[1].Crc); Assert.AreEqual("", db.Tables[1].Description); Assert.AreEqual("thetable2", db.Tables[1].DumpName); Assert.AreEqual(11, db.Tables[1].Fields.Count); Assert.AreEqual(null, db.Tables[1].Foreign); Assert.AreEqual(false, db.Tables[1].Frozen); Assert.AreEqual(false, db.Tables[1].Hidden); Assert.AreEqual(null, db.Tables[1].Indexes); Assert.AreEqual(null, db.Tables[1].Label); Assert.AreEqual(null, db.Tables[1].LabelAttribute); Assert.AreEqual("thetable2", db.Tables[1].Name); Assert.AreEqual(null, db.Tables[1].Replication); Assert.AreEqual(null, db.Tables[1].Triggers); Assert.AreEqual(UoeDatabaseTableType.T, db.Tables[1].Type); Assert.AreEqual(null, db.Tables[1].ValidationExpression); Assert.AreEqual("", db.Tables[1].ValidationMessage); Assert.AreEqual(null, db.Tables[1].ValidationMessageAttribute); } env.Dispose(); }
public void Delete() { if (!TestHelper.GetDlcPath(out string dlcPath)) { return; } var db = new UoeDatabaseOperator(dlcPath); var deleteDir = Path.Combine(TestFolder, "delete"); Directory.CreateDirectory(deleteDir); var stPath = Path.Combine(deleteDir, "test1.st"); File.WriteAllText(stPath, @" b . d ""Schema Area"":6,32;1 . d ""Data Area"":7,32;1 . d ""Index Area"":8,32;8 . d ""Data Area2"":9,32;8 . d ""Data Area3"":12,32;8 . "); var loc = new UoeDatabaseLocation(Path.Combine(deleteDir, "test1.db")); db.Create(loc, stPath); Assert.AreEqual(9, Directory.EnumerateFiles(deleteDir, "*", SearchOption.TopDirectoryOnly).Count()); File.Delete(stPath); db.Delete(loc); Assert.AreEqual(1, Directory.EnumerateFiles(deleteDir, "*", SearchOption.TopDirectoryOnly).Count(), "only the .st should be left (it has been recreated to list files to delete)."); File.WriteAllText(stPath, @" b ./2 a ./3 f 1024 a ./3 f 1024 a !""./3"" f 1024 t . f 4096 d ""Employee"",32 ./1/emp f 1024 d ""Employee"",32 ./1/emp d ""Inventory"",32 ./1/inv f 1024 d ""Inventory"",32 ./1/inv d ""Cust_Data"",32;64 ./1/cust f 1024 d ""Cust_Data"",32;64 ./1/cust d ""Cust_Index"",32;8 ./1/cust d ""Order"",32;64 ./1/ord f 1024 d ""Order"",32;64 ./1/ord d ""Misc"",32 !""./1/misc data"" f 1024 d ""Misc"",32 !""./1/misc data"" d ""schema Area"" . "); db.CreateVoidDatabase(loc, stPath); Assert.AreEqual(20, Directory.EnumerateFiles(deleteDir, "*", SearchOption.AllDirectories).Count()); db.Delete(loc); Assert.AreEqual(1, Directory.EnumerateFiles(deleteDir, "*", SearchOption.AllDirectories).Count()); }
public void GetValidPhysicalName(string input, string expect) { Assert.AreEqual(expect, UoeDatabaseLocation.GetValidPhysicalName(input)); }