Ejemplo n.º 1
0
        /// <summary>
        /// Internal load a ScopeInfo by scope name
        /// </summary>
        internal async Task <(SyncContext context, ClientScopeInfo clientScopeInfo)> InternalGetClientScopeInfoAsync(
            SyncContext context, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            try
            {
                await using var runner = await this.GetConnectionAsync(context, SyncMode.Writing, SyncStage.ScopeLoading, connection, transaction, cancellationToken, progress).ConfigureAwait(false);

                bool exists;
                (context, exists) = await this.InternalExistsScopeInfoTableAsync(context, DbScopeType.Client, runner.Connection, runner.Transaction, runner.CancellationToken, runner.Progress).ConfigureAwait(false);

                if (!exists)
                {
                    (context, _) = await this.InternalCreateScopeInfoTableAsync(context, DbScopeType.Client, runner.Connection, runner.Transaction, runner.CancellationToken, runner.Progress).ConfigureAwait(false);
                }

                // Get scope from local client
                ClientScopeInfo localScopeInfo;
                (context, localScopeInfo) = await this.InternalLoadClientScopeInfoAsync(context, runner.Connection, runner.Transaction, runner.CancellationToken, runner.Progress).ConfigureAwait(false);

                var shouldSave = false;

                // Get scopeId representing the client unique id
                if (localScopeInfo == null)
                {
                    shouldSave = true;

                    localScopeInfo = this.InternalCreateScopeInfo(context.ScopeName, DbScopeType.Client) as ClientScopeInfo;

                    // Checking if we have already some scopes
                    // Then gets the first scope to get the id
                    // This ID is identifying the client database
                    List <ClientScopeInfo> allClientScopeInfos;
                    (context, allClientScopeInfos) = await this.InternalLoadAllClientScopesInfoAsync(context, runner.Connection, runner.Transaction, runner.CancellationToken, runner.Progress).ConfigureAwait(false);

                    if (allClientScopeInfos.Count > 0)
                    {
                        localScopeInfo.Id = allClientScopeInfos[0].Id;
                    }
                }

                if (shouldSave)
                {
                    // if not shouldSave, that means we already raised this event before
                    var scopeLoadedArgs = new ClientScopeInfoLoadedArgs(context, context.ScopeName, localScopeInfo, runner.Connection, runner.Transaction);
                    await this.InterceptAsync(scopeLoadedArgs, progress, cancellationToken).ConfigureAwait(false);

                    localScopeInfo = scopeLoadedArgs.ClientScopeInfo;

                    (context, localScopeInfo) = await this.InternalSaveClientScopeInfoAsync(localScopeInfo, context, runner.Connection, runner.Transaction, runner.CancellationToken, runner.Progress).ConfigureAwait(false);
                }

                await runner.CommitAsync().ConfigureAwait(false);

                return(context, localScopeInfo);
            }
            catch (Exception ex)
            {
                throw GetSyncError(context, ex);
            }
        }
Ejemplo n.º 2
0
        InternalLoadClientScopeInfoAsync(SyncContext context, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var scopeBuilder = this.GetScopeBuilder(this.Options.ScopeInfoTableName);

            using var command = scopeBuilder.GetCommandAsync(DbScopeCommandType.GetClientScopeInfo, connection, transaction);

            if (command == null)
            {
                return(context, null);
            }

            DbSyncAdapter.SetParameterValue(command, "sync_scope_name", context.ScopeName);

            var action = new ClientScopeInfoLoadingArgs(context, context.ScopeName, command, connection, transaction);

            await this.InterceptAsync(action, progress, cancellationToken).ConfigureAwait(false);

            if (action.Cancel || action.Command == null)
            {
                return(context, null);
            }

            await this.InterceptAsync(new DbCommandArgs(context, action.Command, connection, transaction), progress, cancellationToken).ConfigureAwait(false);

            using DbDataReader reader = await action.Command.ExecuteReaderAsync().ConfigureAwait(false);

            ClientScopeInfo clientScopeInfo = null;

            if (reader.Read())
            {
                clientScopeInfo = InternalReadClientScopeInfo(reader);
            }

            reader.Close();

            if (clientScopeInfo?.Schema != null)
            {
                clientScopeInfo.Schema.EnsureSchema();
            }

            if (clientScopeInfo != null)
            {
                var scopeLoadedArgs = new ClientScopeInfoLoadedArgs(context, context.ScopeName, clientScopeInfo, connection, transaction);
                await this.InterceptAsync(scopeLoadedArgs, progress, cancellationToken).ConfigureAwait(false);

                clientScopeInfo = scopeLoadedArgs.ClientScopeInfo;
            }

            action.Command.Dispose();

            return(context, clientScopeInfo);
        }
Ejemplo n.º 3
0
        InternalLoadAllClientScopesInfoAsync(SyncContext context, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var scopeBuilder = this.GetScopeBuilder(this.Options.ScopeInfoTableName);

            using var command = scopeBuilder.GetCommandAsync(DbScopeCommandType.GetAllClientScopesInfo, connection, transaction);

            if (command == null)
            {
                return(context, null);
            }

            var action = new ClientScopeInfoLoadingArgs(context, context.ScopeName, command, connection, transaction);

            await this.InterceptAsync(action, progress, cancellationToken).ConfigureAwait(false);

            if (action.Cancel || action.Command == null)
            {
                return(context, null);
            }

            var clientScopes = new List <ClientScopeInfo>();

            await this.InterceptAsync(new DbCommandArgs(context, action.Command, connection, transaction), progress, cancellationToken).ConfigureAwait(false);

            using DbDataReader reader = await action.Command.ExecuteReaderAsync().ConfigureAwait(false);

            while (reader.Read())
            {
                var scopeInfo = InternalReadClientScopeInfo(reader);

                if (scopeInfo.Schema != null)
                {
                    scopeInfo.Schema.EnsureSchema();
                }

                clientScopes.Add(scopeInfo);
            }

            reader.Close();

            foreach (var scopeInfo in clientScopes)
            {
                var scopeLoadedArgs = new ClientScopeInfoLoadedArgs(context, context.ScopeName, scopeInfo, connection, transaction);
                await this.InterceptAsync(scopeLoadedArgs, progress, cancellationToken).ConfigureAwait(false);
            }
            action.Command.Dispose();

            return(context, clientScopes);
        }