Пример #1
0
        public void LoadSchemaDefinition()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope    = new UoeDatabaseOperator(dlcPath);
            var db     = GetDb("loaddf");
            var dfPath = Path.Combine(TestFolder, $"{db.PhysicalName}.df");

            ope.Create(db);
            Assert.IsTrue(db.Exists());

            // create .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\n");

            using (var dataAdmin = new UoeDatabaseAdministrator(dlcPath)) {
                dataAdmin.LoadSchemaDefinition(UoeDatabaseConnection.NewSingleUserConnection(db), dfPath);
            }

            ope.Delete(db);
            ope.Create(db);

            // create .df
            File.WriteAllText(dfPath, "ADD 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");

            using (var dataAdmin = new UoeDatabaseAdministrator(dlcPath)) {
                Assert.ThrowsException <UoeDatabaseException>(() => dataAdmin.LoadSchemaDefinition(UoeDatabaseConnection.NewSingleUserConnection(db), dfPath));
            }
        }
        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();
        }
        public void TruncateLog()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);
            var db  = GetDb("trunclog");

            ope.Create(db);
            Assert.IsTrue(db.Exists());

            ope.TruncateLog(db);

            try {
                ope.Start(db);

                var oldSize = new FileInfo(Path.Combine(TestFolder, "trunclog.lg")).Length;

                ope.TruncateLog(db);

                var newSize = new FileInfo(Path.Combine(TestFolder, "trunclog.lg")).Length;

                Assert.AreNotEqual(oldSize, newSize);

                var cs = ope.GetDatabaseConnection(db);
                Assert.IsFalse(cs.SingleUser);
                Assert.IsTrue(string.IsNullOrEmpty(cs.Service));
            } finally {
                ope.Kill(db);
            }
        }
        public void Start()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);

            var tgtDb = GetDb("start");

            ope.Create(tgtDb);
            Assert.IsTrue(tgtDb.Exists());

            Assert.AreEqual(DatabaseBusyMode.NotBusy, ope.GetBusyMode(tgtDb));

            var nextPort = UoeDatabaseOperator.GetNextAvailablePort();

            try {
                ope.Start(tgtDb, "localhost", nextPort.ToString(), new UoeProcessArgs().Append("-minport", "50000", "-maxport", "50100", "-L", "20000") as UoeProcessArgs);

                Assert.AreEqual(DatabaseBusyMode.MultiUser, ope.GetBusyMode(tgtDb));
            } finally {
                ope.Kill(tgtDb);
            }

            Assert.AreEqual(DatabaseBusyMode.NotBusy, ope.GetBusyMode(tgtDb));
        }
        public void AddAndRemoveExtents()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);

            var tgtDb = GetDb("extents");

            ope.Create(tgtDb);
            Assert.IsTrue(tgtDb.Exists());

            var stPath = Path.Combine(TestFolder, "extents_add.st");

            File.WriteAllText(stPath, $"d \"Data Area\" \"{TestFolder}\"");

            ope.AddStructureDefinition(tgtDb, stPath);

            try {
                ope.Start(tgtDb);
                File.WriteAllText(stPath, $"d \"New Area\" \"{TestFolder}\"");
                ope.AddStructureDefinition(tgtDb, stPath);
            } finally {
                ope.Stop(tgtDb);
            }

            Assert.IsTrue(File.Exists(Path.Combine(TestFolder, "extents_7.d1")));
            Assert.IsTrue(File.Exists(Path.Combine(TestFolder, "extents_8.d1")));

            ope.RemoveStructureDefinition(tgtDb, "d", "Data Area");

            Assert.IsFalse(File.Exists(Path.Combine(TestFolder, "extents_7.d1")));
        }
        public void RepairDatabaseControlInfo()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);

            var tgtDb = GetDb("repair");

            ope.Create(tgtDb);
            Assert.IsTrue(tgtDb.Exists());

            var stPath = Path.Combine(TestFolder, "repair.st");

            File.WriteAllText(stPath, $"b .\nd \"Schema Area\" \"{TestFolder}\"");

            ope.RepairDatabaseControlInfo(tgtDb);

            File.WriteAllText(stPath, "b .\nd \"Schema Area\" .");

            File.Delete(tgtDb.FullPath);
            ope.RepairDatabaseControlInfo(tgtDb);

            tgtDb.ThrowIfNotExist();
        }
        public void Copy()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);

            var stPath = Path.Combine(TestFolder, "copysource.st");

            File.WriteAllText(stPath, $"b .\nd \"Schema Area\" \"{TestFolder}\"");

            var srcDb = GetDb("copysource");
            var tgtDb = GetDb("copytarget");

            ope.Create(srcDb, stPath);
            Assert.IsTrue(srcDb.Exists());

            ope.Copy(tgtDb, srcDb, false, false);
            Assert.IsTrue(tgtDb.Exists());

            ope.Delete(tgtDb);
            Assert.IsFalse(tgtDb.Exists());

            ope.Copy(tgtDb, srcDb);
            Assert.IsTrue(tgtDb.Exists());
        }
        public void Create()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);
            var db  = GetDb("test1.db");

            ope.Create(db);

            Assert.IsTrue(db.Exists());

            ope.Delete(db);

            Assert.IsFalse(db.Exists());

            var stPath = Path.Combine(TestFolder, "test1.st");

            File.WriteAllText(stPath, "b .\nd \"Schema Area\" .\nd \"data\" .");

            ope.Create(db, stPath);

            Assert.IsTrue(db.Exists());
            Assert.IsTrue(File.Exists(Path.Combine(TestFolder, "test1_7.d1")));

            ope.Delete(db);

            Assert.IsFalse(db.Exists());

            File.WriteAllText(stPath, "b .\nd \"Schema Area\" ./sub\nd \"data\" ./data");

            ope.Create(db, stPath, DatabaseBlockSize.S4096, "utf", false, false);

            Assert.IsTrue(db.Exists());

            Assert.IsTrue(File.Exists(Path.Combine(TestFolder, "sub", "test1.d1")));
            Assert.IsTrue(File.Exists(Path.Combine(TestFolder, "data", "test1_7.d1")));

            ope.Delete(db);

            Assert.IsFalse(File.Exists(Path.Combine(TestFolder, "sub", "test1.d1")));
            Assert.IsFalse(File.Exists(Path.Combine(TestFolder, "data", "test1_7.d1")));
        }
        public void Analysis()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);
            var db  = GetDb("analys");

            ope.Create(db);
            Assert.IsTrue(db.Exists());

            Assert.IsTrue(ope.GenerateAnalysisReport(db).Length > 10);
        }
        public void UpdateStructureFile()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);

            var tgtDb = GetDb("updatest");

            ope.Create(tgtDb);
            Assert.IsTrue(tgtDb.Exists());

            File.Delete(tgtDb.StructureFileFullPath);

            ope.UpdateStructureFile(tgtDb, false, false);

            Assert.IsTrue(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(Path.Combine(tgtDb.DirectoryPath, "updatest.b1")));

            File.Delete(tgtDb.StructureFileFullPath);

            ope.UpdateStructureFile(tgtDb, false, true);

            Assert.IsFalse(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(Path.Combine(tgtDb.DirectoryPath, "updatest.b1")));
            Assert.IsTrue(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(tgtDb.DirectoryPath));

            File.Delete(tgtDb.StructureFileFullPath);

            ope.UpdateStructureFile(tgtDb, true, false);

            Assert.IsFalse(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(Path.Combine(tgtDb.DirectoryPath, "updatest.b1")));
            Assert.IsFalse(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(tgtDb.DirectoryPath));
            Assert.IsTrue(File.ReadAllText(tgtDb.StructureFileFullPath).Contains("updatest.b1"));

            File.Delete(tgtDb.StructureFileFullPath);

            ope.UpdateStructureFile(tgtDb, true, true);

            Assert.IsFalse(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(Path.Combine(tgtDb.DirectoryPath, "updatest.b1")));
            Assert.IsFalse(File.ReadAllText(tgtDb.StructureFileFullPath).Contains(tgtDb.DirectoryPath));
            Assert.IsFalse(File.ReadAllText(tgtDb.StructureFileFullPath).Contains("updatest.b1"));
            Assert.IsTrue(File.ReadAllText(tgtDb.StructureFileFullPath).Contains("."));
        }
        public void GetConnectionString()
        {
            if (!TestHelper.GetDlcPath(out string dlcPath))
            {
                return;
            }

            var ope = new UoeDatabaseOperator(dlcPath);
            var db  = GetDb("connec");

            ope.Create(db);

            Assert.IsTrue(ope.GetDatabaseConnection(db).SingleUser);
            Assert.IsTrue(string.IsNullOrEmpty(ope.GetDatabaseConnection(db).Service));
            Assert.AreEqual(null, ope.GetDatabaseConnection(db).LogicalName);
            Assert.AreEqual("test", ope.GetDatabaseConnection(db, "test").LogicalName);

            try {
                ope.Start(db);

                Assert.IsFalse(ope.GetDatabaseConnection(db).SingleUser);
                Assert.IsTrue(string.IsNullOrEmpty(ope.GetDatabaseConnection(db).Service));
            } finally {
                ope.Kill(db);
            }

            try {
                ope.Start(db, null, UoeDatabaseOperator.GetNextAvailablePort().ToString());

                Assert.IsFalse(ope.GetDatabaseConnection(db).SingleUser);
                Assert.IsFalse(string.IsNullOrEmpty(ope.GetDatabaseConnection(db).Service));
                Assert.IsFalse(string.IsNullOrEmpty(ope.GetDatabaseConnection(db).HostName));
            } finally {
                ope.Kill(db);
            }
        }
        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());
        }