Exemple #1
0
        /// <inheritdoc />
        public virtual async Task SynchronizeSchemaAsync(string connectionString, IDatabaseDriver databaseDriver, CancellationToken cancellationToken)
        {
            if (_synchronizationStrategy == SchemaSynchronizationStrategy.Ignore)
            {
                return;
            }

            if (ShouldSynchronizeSchema)
            {
                await _schemaSynchronizedSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

                try
                {
                    if (ShouldSynchronizeSchema)
                    {
                        if (connectionString == null)
                        {
                            throw new ArgumentNullException(nameof(connectionString));
                        }
                        if (databaseDriver == null)
                        {
                            throw new ArgumentNullException(nameof(databaseDriver));
                        }

                        _synchronizationsTries++;

                        using (var connection = databaseDriver.CreateConnection(connectionString))
                        {
                            await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

                            var tableExistsSql = databaseDriver.GetSqlStatementTemplate(SqlStatement.TableExists).Format(
                                new
                            {
                                // Do not parse the identifiers here.
                                schemaName = Schema ?? databaseDriver.DefaultSchema,
                                tableName  = Name
                            });

                            // Check if the table exists
                            var tableExists = await connection.ExecuteScalarAsync <bool>(
                                tableExistsSql,
                                cancellationToken).ConfigureAwait(false);

                            if (!tableExists)
                            {
                                await DatabaseSchema.CreateTableAsync(databaseDriver, connection, this, cancellationToken).ConfigureAwait(false);
                            }

                            await DatabaseSchema.UpdateTableSchemaAsync(databaseDriver, connection, this, cancellationToken).ConfigureAwait(false);

                            SchemaSynchronized = true;
                        }
                    }
                }
                finally
                {
                    _schemaSynchronizedSemaphore.Release();
                }
            }
        }
Exemple #2
0
 private FolkeConnection(IDatabaseDriver databaseDriver, IMapper mapper, string connectionString = null)
 {
     Cache         = new Dictionary <string, IDictionary <object, object> >();
     Driver        = databaseDriver;
     connection    = databaseDriver.CreateConnection(connectionString);
     Database      = connection?.Database;
     Mapper        = mapper;
     queryProvider = new ElmQueryProvider(this);
 }
        public static async Task <DbConnection> OpenConnection(this IDatabaseDriver databaseDriver, string connectionString)
        {
            var connection = databaseDriver.CreateConnection();

            connection.ConnectionString = connectionString;
            await connection.OpenAsync();

            return(connection);
        }
 public DbConnection CreateConnection(string connectionString) => _underlyingDatabaseDriver.CreateConnection(connectionString);