Example #1
0
        public static IDbProvider AutoDetectProvider()
        {
            IDbProvider provider = null;

            try
            {
                using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(DWKitRuntime.ConnectionStringData)) { };
                provider = new SQLServerProvider();
            }
            catch (ArgumentException) { }

            if (provider == null)
            {
                try
                {
                    using (IDbConnection connection = new Npgsql.NpgsqlConnection(DWKitRuntime.ConnectionStringData)) { };
                    provider = new PostgreSqlProvider();
                }
                catch (ArgumentException) { }
            }

            if (provider == null)
            {
                try
                {
                    using (IDbConnection connection = new OracleConnection(DWKitRuntime.ConnectionStringData)) {}
                    provider = new OracleProvider();
                }
                catch (ArgumentException)
                {
                }
            }

            return(provider);
        }
        /// <summary>
        ///		Obtiene el esquema
        /// </summary>
        internal async Task <SchemaDbModel> GetSchemaAsync(PostgreSqlProvider provider, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var schema = new SchemaDbModel();

            // Abre la conexión
            provider.Open();
            // Carga los datos
            using (DbDataReader rdoData = await provider.ExecuteReaderAsync(GetSqlReadSchema(), null, CommandType.Text, timeout, cancellationToken))
            {
                while (!cancellationToken.IsCancellationRequested && await rdoData.ReadAsync(cancellationToken))
                {
                    schema.Add(rdoData.IisNull <string>("TableType").Equals("TABLE", StringComparison.CurrentCultureIgnoreCase),
                               rdoData.IisNull <string>("SchemaName"),
                               rdoData.IisNull <string>("TableName"),
                               rdoData.IisNull <string>("ColumnName"),
                               GetFieldType(rdoData.IisNull <string>("ColumnType")),
                               rdoData.IisNull <string>("ColumnType"),
                               rdoData.IisNull <int>("ColumnLength", 0),
                               rdoData.IisNull <int>("PrimaryKey") == 1,
                               rdoData.IisNull <int>("IsRequired") == 1);
                }
            }
            // Cierra la conexión
            provider.Close();
            // Devuelve el esquema
            return(schema);
        }
        public void ConnectionPool()
        {
            var connectionPool = new PostgreSqlConnectionPool(CONNECTION_STRING, 2, 5000);
            var providerA      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            var providerB      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);

            Assert.AreEqual(1, providerA.ConnectionId);
            Assert.AreEqual(2, providerB.ConnectionId);
        }
        public void ConnectionPool_NoTimeout()
        {
            var connectionPool = new PostgreSqlConnectionPool(CONNECTION_STRING, 2, 1000);
            var providerA      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            var providerB      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);

            Task.Delay(1000).ContinueWith(k => providerA.Dispose());

            var providerC = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
        }
        public void ConnectionPool_ConnectionMaxReached()
        {
            var connectionPool = new PostgreSqlConnectionPool(CONNECTION_STRING, 2, 1000);
            var providerA      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            var providerB      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);

            Assert.Throws <TimeoutException>(() =>
            {
                var providerC = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            });
        }
        public void ConnectionPool_Disposing()
        {
            var connectionPool = new PostgreSqlConnectionPool(CONNECTION_STRING, 2, 5000);
            var providerA      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            var providerB      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);

            providerA.Dispose();
            var providerC = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);

            providerB.Dispose();
            var providerD = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
        }
Example #7
0
        public ItemProvider(ItemProviderSettings settings, TypeManager typeManager, ILogWriter log)
        {
            var entityProviderLog = log.NewRegion(nameof(EntityProvider));
            var pool        = new PostgreSqlConnectionPool(settings.PostgreSqlConnectionString, settings.MaxConnections, settings.Timeout);
            var sqlProvider = new PostgreSqlProvider(pool, entityProviderLog.NewRegion("PostgreSQL"), settings.Schema);

            sqlProvider.EnsureSchemaExists();

            this.provider = new EntityProvider(sqlProvider, entityProviderLog);
            this.log      = log;
            this.context  = new ItemContext(typeManager);
        }
        /// <summary>
        ///     ConfigureServices Services
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            var logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .WriteTo.Console(theme: AnsiConsoleTheme.Code)
                         .WriteTo.Seq("http://seq:5341")
                         .CreateLogger();

            services.AddTransient(x => logger);

            var provider = new PostgreSqlProvider(configuration["PostgreSQL:DefaultConnection"]);

            services.AddTransient <IDbProvider <NpgsqlConnection> >(x => provider);

            var authServiceRepository = new AuthServiceRepository(provider, logger);

            services.AddTransient <IAuthServiceRepository>(x => authServiceRepository);

            var authService = new AuthService(authServiceRepository, logger);

            services.AddTransient(x => authService);

            var confidentialClientApplication = ConfidentialClientApplicationBuilder
                                                .Create(configuration["MicrosoftAPI:ApiID"])
                                                .WithTenantId(configuration["MicrosoftAPI:TenantId"])
                                                .WithClientSecret(configuration["MicrosoftAPI:ClientSecret"])
                                                .Build();
            var authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
            var graphServiceClient     = new GraphServiceClient(authenticationProvider);

            services.AddTransient(x => graphServiceClient);

            var activityFactory = new ActivitiesFactory(graphServiceClient, logger);

            services.AddTransient(x => activityFactory);

            var useCaseFactory = new UseCaseFactory(activityFactory, logger);

            services.AddTransient(x => useCaseFactory);

            var userService = new UserService(useCaseFactory, logger);

            services.AddTransient(x => userService);

            var groupService = new GroupService(useCaseFactory, logger);

            services.AddTransient(x => groupService);

            var teamService = new TeamService(useCaseFactory, logger);

            services.AddTransient(x => teamService);
        }
        public void ConnectionPool_Timeout()
        {
            var connectionPool = new PostgreSqlConnectionPool(CONNECTION_STRING, 2, 1000);
            var providerA      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            var providerB      = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);

            Task.Delay(1100).ContinueWith(k => providerA.Dispose());

            Assert.Throws <TimeoutException>(() =>
            {
                var providerC = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            });
        }
Example #10
0
        public void BeforeEach()
        {
            var log       = new CustomLogData(l => Debug.WriteLine(l));
            var logwriter = new RegionBasedLogWriter(log, nameof(PostgreSqlProviderTests));

            logwriter.StartStopwatch();

            provider = new PostgreSqlProvider(connectionPool, logwriter, SCHEMA);
            provider.EnsureSchemaExists();

            var tables = provider.GetTableNamesBySchema(SCHEMA);

            foreach (var table in tables)
            {
                provider.DropTable(table);
            }
        }
Example #11
0
        public void BeforeEach()
        {
            var log = new CustomLogData(l => Debug.WriteLine(l));

            logwriter   = new RegionBasedLogWriter(log, "Spec");
            sqlProvider = new PostgreSqlProvider(connectionPool, logwriter.NewRegion(nameof(PostgreSqlProvider)), SCHEMA);
            sqlProvider.EnsureSchemaExists();

            typeManager = new TypeManager();
            typeManager.Register <DeviceType>();
            typeManager.Register <SensorType>();

            entityProvider = new EntityProvider(sqlProvider, logwriter.NewRegion(nameof(EntityProvider)));
            provider       = new ItemProvider(entityProvider, typeManager, logwriter.NewRegion(nameof(ItemProvider)));

            var tables = sqlProvider.GetTableNamesBySchema(SCHEMA);

            foreach (var table in tables)
            {
                sqlProvider.DropTable(table);
            }
        }
Example #12
0
        public static IDbProvider AutoDetectProvider(IConfigurationRoot configuration, ILogger logger)
        {
            IDbProvider provider = null;
            bool        createDb = "true".Equals(configuration["DWKit:CreateDatabaseObjects"], StringComparison.InvariantCultureIgnoreCase);

            try
            {
                using (new System.Data.SqlClient.SqlConnection(DWKitRuntime.ConnectionStringData))
                { }

                provider = new SQLServerProvider();

                if (createDb)
                {
                    CreateDatabaseIfNotExists(configuration, new MSSQL.DbCreator(DWKitRuntime.ConnectionStringData), "MSSQL", logger);
                }
            }
            catch (ArgumentException)
            {
            }

            if (provider == null)
            {
                try
                {
                    using (IDbConnection connection = new Npgsql.NpgsqlConnection(DWKitRuntime.ConnectionStringData)) { }
                    provider = new PostgreSqlProvider();

                    if (createDb)
                    {
                        CreateDatabaseIfNotExists(configuration, new PostgreSQL.DbCreator(DWKitRuntime.ConnectionStringData), "PostgreSql", logger);
                    }
                }
                catch (ArgumentException)
                {
                }
            }

            if (provider == null)
            {
                try
                {
                    using (IDbConnection connection = new OracleConnection(DWKitRuntime.ConnectionStringData)) { }
                    provider = new OracleProvider();

                    if (createDb)
                    {
                        CreateDatabaseIfNotExists(configuration, new Oracle.DbCreator(DWKitRuntime.ConnectionStringData), "Oracle", logger);
                    }
                }
                catch (ArgumentException)
                {
                }
            }

            if (provider == null)
            {
                throw new Exception($"Can't autodetect provider for connection string: {DWKitRuntime.ConnectionStringData}");
            }


            return(provider);
        }