protected virtual int Insert(DisconnectedMachineEntity machine, Table table, IDisconnectedStrategy strategy, SqlServerConnector newDatabase)
        {
            var          isPostgres      = Schema.Current.Settings.IsPostgres;
            DatabaseName newDatabaseName = new DatabaseName(null, newDatabase.DatabaseName(), isPostgres);

            var count = (int)CountNewItems(table, newDatabaseName).ExecuteScalar() !;

            if (count == 0)
            {
                return(0);
            }

            using (Transaction tr = new Transaction())
            {
                int result;
                using (DisableIdentityIfNecessary(table))
                {
                    SqlPreCommandSimple sql = InsertTableScript(table, newDatabaseName);

                    result = Executor.ExecuteNonQuery(sql);
                }

                foreach (var rt in table.TablesMList())
                {
                    using (DisableIdentityIfNecessary(rt))
                    {
                        SqlPreCommandSimple rsql = InsertRelationalTableScript(table, newDatabaseName, rt);

                        Executor.ExecuteNonQuery(rsql);
                    }
                }

                return(tr.Commit(result));
            }
        }
Beispiel #2
0
        public virtual Lite <DisconnectedExportEntity> BeginExportDatabase(DisconnectedMachineEntity machine)
        {
            Lite <DisconnectedExportEntity> export = new DisconnectedExportEntity
            {
                Machine = machine.ToLite(),
                Copies  = downloadTables.Select(t => new DisconnectedExportTableEmbedded
                {
                    Type = t.Type.ToTypeEntity().ToLite()
                }).ToMList()
            }.Save().ToLite();

            var cancelationSource = new CancellationTokenSource();

            var user = UserHolder.Current;

            var token = cancelationSource.Token;

            var task = Task.Factory.StartNew(() =>
            {
                using (UserHolder.UserSession(user))
                {
                    OnStartExporting(machine);
                    DisconnectedMachineEntity.Current = machine.ToLite();

                    try
                    {
                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.Lock, s => l).Execute()))
                        {
                            foreach (var tuple in downloadTables)
                            {
                                token.ThrowIfCancellationRequested();

                                if (tuple.Strategy.Upload == Upload.Subset)
                                {
                                    miUnsafeLock.MakeGenericMethod(tuple.Type).Invoke(this, new object[] { machine.ToLite(), tuple.Strategy, export });
                                }
                            }
                        }

                        string connectionString;
                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.CreateDatabase, s => l).Execute()))
                            connectionString = CreateDatabase(machine);

                        var newDatabase = new SqlServerConnector(connectionString, Schema.Current, ((SqlServerConnector)Connector.Current).Version);


                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.CreateSchema, s => l).Execute()))
                            using (Connector.Override(newDatabase))
                                using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                    AvoidDatabaseName = true
                                }))
                                {
                                    Administrator.TotalGeneration();
                                }

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.DisableForeignKeys, s => l).Execute()))
                            using (Connector.Override(newDatabase))
                                using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                    AvoidDatabaseName = true
                                }))
                                {
                                    foreach (var tuple in downloadTables.Where(t => !t.Type.IsEnumEntity()))
                                    {
                                        token.ThrowIfCancellationRequested();

                                        DisableForeignKeys(tuple.Table);
                                    }
                                }

                        var isPostgres = Schema.Current.Settings.IsPostgres;
                        DatabaseName newDatabaseName = new DatabaseName(null, newDatabase.DatabaseName(), isPostgres);

                        foreach (var tuple in downloadTables)
                        {
                            token.ThrowIfCancellationRequested();
                            int ms = 0;
                            using (token.MeasureTime(l => ms = l))
                            {
                                tuple.Strategy.Exporter !.Export(tuple.Table, tuple.Strategy, newDatabaseName, machine);
                            }

                            export.MListElementsLite(_ => _.Copies).Where(c => c.Element.Type.Is(tuple.Type.ToTypeEntity())).UnsafeUpdateMList()
                            .Set(mle => mle.Element.CopyTable, mle => ms)
                            .Execute();
                        }

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.EnableForeignKeys, s => l).Execute()))
                            foreach (var tuple in downloadTables.Where(t => !t.Type.IsEnumEntity()))
                            {
                                token.ThrowIfCancellationRequested();

                                EnableForeignKeys(tuple.Table);
                            }

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.ReseedIds, s => l).Execute()))
                        {
                            var tablesToUpload = Schema.Current.Tables.Values.Where(t => DisconnectedLogic.GetStrategy(t.Type).Upload != Upload.None)
                                                 .SelectMany(t => t.TablesMList().Cast <ITable>().And(t)).Where(t => t.PrimaryKey.Identity).ToList();

                            var maxIdDictionary = tablesToUpload.ToDictionary(t => t,
                                                                              t => DisconnectedTools.MaxIdInRange(t, machine.SeedMin, machine.SeedMax));

                            using (Connector.Override(newDatabase))
                                using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                    AvoidDatabaseName = true
                                }))
                                {
                                    foreach (var table in tablesToUpload)
                                    {
                                        token.ThrowIfCancellationRequested();

                                        long?max = maxIdDictionary.GetOrThrow(table);

                                        DisconnectedTools.SetNextId(table, (max + 1) ?? machine.SeedMin);
                                    }
                                }
                        }

                        CopyExport(export, newDatabase);

                        machine.InDB().UnsafeUpdate().Set(s => s.State, s => DisconnectedMachineState.Disconnected).Execute();
                        using (SqlServerConnector.Override(newDatabase))
                            using (ObjectName.OverrideOptions(new ObjectNameOptions {
                                AvoidDatabaseName = true
                            }))
                                machine.InDB().UnsafeUpdate().Set(s => s.State, s => DisconnectedMachineState.Disconnected).Execute();

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.BackupDatabase, s => l).Execute()))
                            BackupDatabase(machine, export, newDatabase);

                        using (token.MeasureTime(l => export.InDB().UnsafeUpdate().Set(s => s.DropDatabase, s => l).Execute()))
                            DropDatabase(newDatabase);

                        token.ThrowIfCancellationRequested();

                        export.InDB().UnsafeUpdate()
                        .Set(s => s.State, s => DisconnectedExportState.Completed)
                        .Set(s => s.Total, s => s.CalculateTotal())
                        .Execute();
                    }
                    catch (Exception e)
                    {
                        var ex = e.LogException();

                        export.InDB().UnsafeUpdate()
                        .Set(s => s.Exception, s => ex.ToLite())
                        .Set(s => s.State, s => DisconnectedExportState.Error)
                        .Execute();

                        OnExportingError(machine, export, e);
                    }
                    finally
                    {
                        runningExports.Remove(export);
                        DisconnectedMachineEntity.Current = null;

                        OnEndExporting();
                    }
                }
            });


            runningExports.Add(export, new RunningExports(task, cancelationSource));

            return(export);
        }
        public override ImportResult Import(DisconnectedMachineEntity machine, Table table, IDisconnectedStrategy strategy, SqlServerConnector newDatabase)
        {
            var isPostgres = Schema.Current.Settings.IsPostgres;
            int update     = strategy.Upload == Upload.Subset ? Update(machine, table, strategy, new DatabaseName(null, newDatabase.DatabaseName(), isPostgres)) : 0;

            int inserts = Insert(machine, table, strategy, newDatabase);

            return(new ImportResult {
                Inserted = inserts, Updated = update
            });
        }
        private void DropDatabase(SqlServerConnector newDatabase)
        {
            var isPostgres = Schema.Current.Settings.IsPostgres;

            DisconnectedTools.DropDatabase(new DatabaseName(null, newDatabase.DatabaseName(), isPostgres));
        }