Beispiel #1
0
        /// <summary>
        /// Internal exists scope
        /// </summary>
        internal async Task <bool> InternalExistsScopeInfoAsync(SyncContext ctx, DbScopeType scopeType, string scopeId, DbScopeBuilder scopeBuilder, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            // Get exists command
            var existsCommand = scopeBuilder.GetCommandAsync(DbScopeCommandType.ExistScope, scopeType, connection, transaction);

            // Just in case, in older version we may have sync_scope_name as primary key;
            DbSyncAdapter.SetParameterValue(existsCommand, "sync_scope_name", scopeId);
            // Set primary key value
            DbSyncAdapter.SetParameterValue(existsCommand, "sync_scope_id", scopeId);

            if (existsCommand == null)
            {
                return(false);
            }

            var existsResultObject = await existsCommand.ExecuteScalarAsync().ConfigureAwait(false);

            var exists = Convert.ToInt32(existsResultObject) > 0;

            return(exists);
        }
        internal virtual async Task <bool> InternalUpgradeAsync(SyncContext context, SyncSet schema, List <ServerScopeInfo> serverScopeInfos, DbScopeBuilder builder, DbConnection connection, DbTransaction transaction,
                                                                CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var version = SyncVersion.Current;

            // get the smallest version of all scope in the scope info server tables
            foreach (var serverScopeInfo in serverScopeInfos)
            {
                var tmpVersion = SyncVersion.EnsureVersion(serverScopeInfo.Version);

                if (tmpVersion < version)
                {
                    version = tmpVersion;
                }
            }

            // beta version
            if (version.Major == 0)
            {
                // Migrate from 0.5.x to 0.6.0
                if (version.Minor <= 5)
                {
                    version = await UpgdrateTo600Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                // Migrate from 0.6.0 to 0.6.1
                if (version.Minor <= 6 && version.Build <= 0)
                {
                    version = await UpgdrateTo601Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            foreach (var serverScopeInfo in serverScopeInfos)
            {
                var oldVersion = SyncVersion.EnsureVersion(serverScopeInfo.Version);
                if (oldVersion != version)
                {
                    serverScopeInfo.Version = version.ToString();
                    await this.InternalSaveScopeAsync(context, DbScopeType.Server, serverScopeInfo, builder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            return(version == SyncVersion.Current);
        }
Beispiel #3
0
        /// <summary>
        /// Internal upsert scope info in a scope table
        /// </summary>
        internal async Task <T> InternalSaveScopeAsync <T>(SyncContext ctx, DbScopeType scopeType, T scopeInfo, DbScopeBuilder scopeBuilder, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress) where T : class
        {
            var scopeId = scopeType switch
            {
                DbScopeType.Client => (scopeInfo as ScopeInfo).Id.ToString(),
                DbScopeType.Server => (scopeInfo as ServerScopeInfo).Name,
                DbScopeType.ServerHistory => (scopeInfo as ServerHistoryScopeInfo).Id.ToString(),
                _ => throw new NotImplementedException($"Can't set parameters to scope command type {scopeType}.")
            };

            var scopeExists = await InternalExistsScopeInfoAsync(ctx, scopeType, scopeId, scopeBuilder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);

            DbCommand command;

            if (scopeExists)
            {
                command = scopeBuilder.GetCommandAsync(DbScopeCommandType.UpdateScope, scopeType, connection, transaction);
            }
            else
            {
                command = scopeBuilder.GetCommandAsync(DbScopeCommandType.InsertScope, scopeType, connection, transaction);
            }

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

            command = scopeType switch
            {
                DbScopeType.Client => SetSaveScopeParameters(scopeInfo as ScopeInfo, command),
                DbScopeType.Server => SetSaveScopeParameters(scopeInfo as ServerScopeInfo, command),
                DbScopeType.ServerHistory => SetSaveScopeParameters(scopeInfo as ServerHistoryScopeInfo, command),
                _ => throw new NotImplementedException($"Can't set parameters to scope command type {scopeType}.")
            };

            var action = new ScopeSavingArgs(ctx, scopeBuilder.ScopeInfoTableName.ToString(), scopeType, scopeInfo, command, connection, transaction);

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

            if (action.Cancel || action.Command == null)
            {
                return(default);
Beispiel #4
0
        /// <summary>
        /// Internal exists scope table routine
        /// </summary>
        internal async Task <bool> InternalExistsScopeInfoTableAsync(SyncContext ctx, DbScopeType scopeType, DbScopeBuilder scopeBuilder, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            // Get exists command
            var existsCommand = scopeBuilder.GetCommandAsync(DbScopeCommandType.ExistsScopeTable, scopeType, connection, transaction);

            if (existsCommand == null)
            {
                return(false);
            }

            var existsResultObject = await existsCommand.ExecuteScalarAsync().ConfigureAwait(false);

            var exists = Convert.ToInt32(existsResultObject) > 0;

            return(exists);
        }
Beispiel #5
0
        /// <summary>
        /// Internal load all scopes routine
        /// </summary>
        internal async Task <List <T> > InternalGetAllScopesAsync <T>(SyncContext ctx, DbScopeType scopeType, string scopeName, DbScopeBuilder scopeBuilder, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress) where T : class
        {
            var command = scopeBuilder.GetCommandAsync(DbScopeCommandType.GetScopes, scopeType, connection, transaction);

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

            DbSyncAdapter.SetParameterValue(command, "sync_scope_name", scopeName);

            var action = new ScopeLoadingArgs(ctx, scopeName, scopeType, command, connection, transaction);

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

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

            var scopes = new List <T>();

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

            while (reader.Read())
            {
                T scopeInfo = scopeType switch
                {
                    DbScopeType.Server => ReaderServerScopeInfo(reader) as T,
                    DbScopeType.ServerHistory => ReadServerHistoryScopeInfo(reader) as T,
                    DbScopeType.Client => ReadScopeInfo(reader) as T,
                    _ => throw new NotImplementedException($"Can't get {scopeType} from the reader ")
                };

                if (scopeInfo != null)
                {
                    scopes.Add(scopeInfo);
                }
            }

            reader.Close();

            return(scopes);
        }
Beispiel #6
0
        /// <summary>
        /// Internal load scope routine
        /// </summary>
        internal async Task <T> InternalGetScopeAsync <T>(SyncContext ctx, DbScopeType scopeType, string scopeName, DbScopeBuilder scopeBuilder, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress) where T : class
        {
            if (typeof(T) != typeof(ScopeInfo) && typeof(T) != typeof(ServerScopeInfo))
            {
                throw new NotImplementedException($"Type {typeof(T).Name} is not implemented when trying to get a single instance");
            }

            var scopes = await InternalGetAllScopesAsync <T>(ctx, scopeType, scopeName, scopeBuilder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);

            if (scopes == null || scopes.Count <= 0)
            {
                scopes = new List <T>();

                // create a new scope id for the current owner (could be server or client as well)
                T scope = scopeType switch
                {
                    DbScopeType.Client => new ScopeInfo {
                        Id = Guid.NewGuid(), Name = scopeName, IsNewScope = true, LastSync = null, LastServerSyncTimestamp = null, LastSyncTimestamp = null, Version = SyncVersion.Current.ToString()
                    } as T,
                    DbScopeType.Server => new ServerScopeInfo {
                        Name = scopeName, LastCleanupTimestamp = 0, Version = SyncVersion.Current.ToString()
                    } as T,
                    _ => throw new NotImplementedException($"Type {typeof(T).Name} is not implemented when trying to get a single instance")
                };

                scope = await this.InternalSaveScopeAsync(ctx, scopeType, scope, scopeBuilder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);

                scopes.Add(scope);
            }

            // get first scope
            var localScope = scopes.FirstOrDefault();

            if (typeof(T) == typeof(ScopeInfo))
            {
                //check if we have already a good last sync. if no, treat it as new
                scopes.ForEach(sc => (sc as ScopeInfo).IsNewScope = (sc as ScopeInfo).LastSync == null);

                var scopeInfo = localScope as ScopeInfo;

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

                var scopeLoadedArgs = new ScopeLoadedArgs <ScopeInfo>(ctx, scopeName, scopeType, scopeInfo, connection, transaction);
                await this.InterceptAsync(scopeLoadedArgs, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                var scopeInfo = localScope as ServerScopeInfo;

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

                var scopeLoadedArgs = new ScopeLoadedArgs <ServerScopeInfo>(ctx, scopeName, scopeType, scopeInfo, connection, transaction);
                await this.InterceptAsync(scopeLoadedArgs, cancellationToken).ConfigureAwait(false);
            }

            return(localScope);
        }
Beispiel #7
0
        /// <summary>
        /// Internal create scope info table routine
        /// </summary>
        internal async Task <bool> InternalCreateScopeInfoTableAsync(SyncContext ctx, DbScopeType scopeType, DbScopeBuilder scopeBuilder, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var command = scopeBuilder.GetCommandAsync(DbScopeCommandType.CreateScopeTable, scopeType, connection, transaction);

            if (command == null)
            {
                return(false);
            }

            var action = new ScopeTableCreatingArgs(ctx, scopeBuilder.ScopeInfoTableName.ToString(), scopeType, command, connection, transaction);

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

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

            await action.Command.ExecuteNonQueryAsync();

            await this.InterceptAsync(new ScopeTableCreatedArgs(ctx, scopeBuilder.ScopeInfoTableName.ToString(), scopeType, connection, transaction), cancellationToken).ConfigureAwait(false);

            return(true);
        }
        internal virtual async Task <bool> InternalUpgradeAsync(SyncContext context, SyncSet schema, ScopeInfo scopeInfo, DbScopeBuilder builder, DbConnection connection, DbTransaction transaction,
                                                                CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var version    = SyncVersion.EnsureVersion(scopeInfo.Version);
            var oldVersion = version.Clone() as Version;

            // beta version
            if (version.Major == 0)
            {
                // Migrate from 0.5.x to 0.6.0
                if (version.Minor <= 5)
                {
                    version = await UpgdrateTo600Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                // Migrate from 0.6.0 to 0.6.1
                if (version.Minor <= 6 && version.Build <= 0)
                {
                    version = await UpgdrateTo601Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            if (oldVersion != version)
            {
                scopeInfo.Version = version.ToString();
                await this.InternalSaveScopeAsync(context, DbScopeType.Client, scopeInfo, builder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
            }
            return(version == SyncVersion.Current);
        }
Beispiel #9
0
        internal virtual async Task <ScopeInfo> InternalUpgradeAsync(SyncContext context, SyncSet schema, SyncSetup setup, ScopeInfo scopeInfo, DbScopeBuilder builder, DbConnection connection, DbTransaction transaction,
                                                                     CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var version    = SyncVersion.EnsureVersion(scopeInfo.Version);
            var oldVersion = version.Clone() as Version;

            // beta version
            if (version.Major == 0)
            {
                if (version.Minor <= 5)
                {
                    version = await UpgdrateTo600Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build == 0)
                {
                    version = await UpgdrateTo601Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build == 1)
                {
                    version = await UpgdrateTo602Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                // last version of 0.6 Can be 0.6.2 or beta version 0.6.3 (that will never be released but still in the nuget packages available)
                if (version.Minor == 6 && version.Build >= 2)
                {
                    version = await UpgdrateTo700Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 0)
                {
                    version = await UpgdrateTo701Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 1)
                {
                    version = await UpgdrateTo702Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 2)
                {
                    version = await UpgdrateTo703Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            if (oldVersion != version)
            {
                scopeInfo.Version = version.ToString();
                scopeInfo         = await this.InternalSaveScopeAsync(context, DbScopeType.Client, scopeInfo, builder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
            }
            return(scopeInfo);
        }
Beispiel #10
0
        internal virtual async Task <ScopeInfo> InternalUpgradeAsync(SyncContext context, SyncSet schema, SyncSetup setup, ScopeInfo scopeInfo, DbScopeBuilder builder, DbConnection connection, DbTransaction transaction,
                                                                     CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var version    = SyncVersion.EnsureVersion(scopeInfo.Version);
            var oldVersion = version.Clone() as Version;

            // beta version
            if (version.Major == 0)
            {
                if (version.Minor <= 5)
                {
                    version = await AutoUpgdrateToNewVersionAsync(context, new Version(0, 6, 0), connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build == 0)
                {
                    version = await UpgdrateTo601Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build == 1)
                {
                    version = await UpgdrateTo602Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build >= 2)
                {
                    version = await UpgdrateTo700Async(context, schema, setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 0)
                {
                    version = await AutoUpgdrateToNewVersionAsync(context, new Version(0, 7, 1), connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 1)
                {
                    version = await AutoUpgdrateToNewVersionAsync(context, new Version(0, 7, 2), connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 2)
                {
                    version = await AutoUpgdrateToNewVersionAsync(context, new Version(0, 7, 3), connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build >= 3)
                {
                    version = await AutoUpgdrateToNewVersionAsync(context, new Version(0, 8, 0), connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            if (oldVersion != version)
            {
                scopeInfo.Version = version.ToString();
                scopeInfo         = await this.InternalSaveScopeAsync(context, DbScopeType.Client, scopeInfo, builder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
            }
            return(scopeInfo);
        }
Beispiel #11
0
        internal virtual async Task <bool> InternalUpgradeAsync(SyncContext context, SyncSet schema, List <ServerScopeInfo> serverScopeInfos, DbScopeBuilder builder, DbConnection connection, DbTransaction transaction,
                                                                CancellationToken cancellationToken, IProgress <ProgressArgs> progress)
        {
            var version = SyncVersion.Current;

            // get the smallest version of all scope in the scope info server tables
            foreach (var serverScopeInfo in serverScopeInfos)
            {
                var tmpVersion = SyncVersion.EnsureVersion(serverScopeInfo.Version);

                if (tmpVersion < version)
                {
                    version = tmpVersion;
                }
            }

            // beta version
            if (version.Major == 0)
            {
                if (version.Minor <= 5)
                {
                    version = await UpgdrateTo600Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build == 0)
                {
                    version = await UpgdrateTo601Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 6 && version.Build == 1)
                {
                    version = await UpgdrateTo602Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                // last version of 0.6 Can be 0.6.2 or beta version 0.6.3 (that will never be released but still in the nuget packages available)
                if (version.Minor == 6 && version.Build >= 2)
                {
                    version = await UpgdrateTo700Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 0)
                {
                    version = await UpgdrateTo701Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 1)
                {
                    version = await UpgdrateTo702Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }

                if (version.Minor == 7 && version.Build == 2)
                {
                    version = await UpgdrateTo703Async(context, schema, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            foreach (var serverScopeInfo in serverScopeInfos)
            {
                var oldVersion = SyncVersion.EnsureVersion(serverScopeInfo.Version);
                if (oldVersion != version)
                {
                    serverScopeInfo.Version = version.ToString();
                    await this.InternalSaveScopeAsync(context, DbScopeType.Server, serverScopeInfo, builder, connection, transaction, cancellationToken, progress).ConfigureAwait(false);
                }
            }

            return(version == SyncVersion.Current);
        }