Ejemplo n.º 1
0
        public void TryGetValue()
        {
            var builder = new NpgsqlConnectionStringBuilder();

            builder.ConnectionString = "Host=myhost";
            Assert.That(builder.TryGetValue("Host", out var value), Is.True);
            Assert.That(value, Is.EqualTo("myhost"));
            Assert.That(builder.TryGetValue("SomethingUnknown", out value), Is.False);
        }
Ejemplo n.º 2
0
        private bool CreateDatabaseIfDoesNotExist()
        {
            try
            {
                var builder = new NpgsqlConnectionStringBuilder(this.ConnectionString);
                if (!builder.TryGetValue("Database", out var database) || database == null)
                {
                    return(false);
                }

                var databaseName             = database.ToString();
                var connectionStringOfMaster = this.ConnectionString.Replace(databaseName, "postgres");
                using (var otherConnection = new NpgsqlConnection(connectionStringOfMaster))
                {
                    otherConnection.Open();
                    using (var createCmd = new NpgsqlCommand($"CREATE DATABASE {databaseName}", otherConnection))
                    {
                        createCmd.ExecuteNonQuery();
                        Debug.Print($"Successfully created database {databaseName}");
                        this.databaseCreated = true;
                    }
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        public DataContextFixture()
        {
            Configuration = new ConfigurationBuilder()
                            .SetBasePath(Directory.GetCurrentDirectory())
                            .AddJsonFile("appsettings.json").Build();

            _connectionString = Configuration["ConnectionStrings:DefaultConnection"];

            var connectionStringBuilder = new NpgsqlConnectionStringBuilder(_connectionString);

            connectionStringBuilder.TryGetValue("Database", out var databaseNameValue);

            _databaseName = databaseNameValue as string;
            if (string.IsNullOrEmpty(_databaseName))
            {
                throw new ArgumentException("Invalid database name", "Database");
            }

            CreateDatabase();
            ExecuteSchema();

            _configuration = new Configuration();
            _configuration.SetProperty("connection.connection_string", _connectionString);
            _configuration.SetProperty("connection.driver_class", typeof(TuskyDriver).AssemblyQualifiedName);
            _configuration.SetProperty("dialect", typeof(PostgreSQLDialect).AssemblyQualifiedName);

            var mappings = _configuration.GetMappings();

            mappings.AddDefaultArrayTypes();
            mappings.AddDefaultListTypeDefs();
            mappings.AddDefaultRangeTypeDefs();
            mappings.AddDefaultNetworkTypes();
            mappings.AddNodaTimeTypeDefs();
            mappings.AddNodaTimeRangeTypeDefs();

            TypeUtil.RegisterDefaultArrayTypes(2);
            TypeUtil.RegisterDefaultListTypes();
            TypeUtil.RegisterDefaultRangeTypes();
            TypeUtil.RegisterNodaTimeTypes();
            TypeUtil.RegisterNodaTimeRangeTypes();

            ArrayProjections.RegisterHooks();
            ArrayRestrictions.RegisterHooks();
            ListProjections.RegisterHooks();
            ListRestrictions.RegisterHooks();
            RangeProjections.RegisterHooks();
            RangeRestrictions.RegisterHooks();

            _configuration.AddAssembly(_assemblyName);

            BuildSessionFactory();

            NpgsqlConnection.GlobalTypeMapper.UseNodaTime();
        }
Ejemplo n.º 4
0
        private (string connectionString, string databaseName) DeriveThrowawayConnectionString(string originalConnectionString)
        {
            var builder = new NpgsqlConnectionStringBuilder(originalConnectionString);

            var databaseName = $"throwawaydb{Guid.NewGuid().ToString("n").Substring(0, 10).ToLowerInvariant()}";

            if (builder.TryGetValue("Database", out var initialDb))
            {
                builder.Remove("Database");
            }

            builder.Database = databaseName;
            return(builder.ConnectionString, databaseName);
        }
        private (string connectionString, string databaseName) DeriveThrowawayConnectionString(string originalConnectionString, string databaseNamePrefix)
        {
            var builder        = new NpgsqlConnectionStringBuilder(originalConnectionString);
            var databasePrefix = string.IsNullOrWhiteSpace(databaseNamePrefix) ? defaultDatabaseNamePrefix : databaseNamePrefix;

            var databaseName = $"{databasePrefix}{Guid.NewGuid().ToString("n").Substring(0, 10).ToLowerInvariant()}";

            if (builder.TryGetValue("Database", out var initialDb))
            {
                builder.Remove("Database");
            }

            builder.Database = databaseName;
            return(builder.ConnectionString, databaseName);
        }