Ejemplo n.º 1
0
        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 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();
        }