コード例 #1
0
        protected override void DoRun(IJobRunEnv env)
        {
            m_source.Mode        = TabularDataStoreMode.Read;
            m_target.Mode        = TabularDataStoreMode.Write;
            m_target.CopyOptions = m_copyOptions;

            Async.SafeOpen(m_source.Connection);
            Async.SafeOpen(m_target.Connection);

            IAsyncResult asyncs = m_source.BeginGetRowFormat(null);

            m_sourceStruct = m_source.EndGetRowFormat(asyncs);

            IAsyncResult asynct = m_target.BeginGetRowFormat(null);

            m_targetStruct = m_target.EndGetRowFormat(asynct);
            var targetFull = m_targetStruct;

            if (m_transform == null)
            {
                m_transform = RowTransformAddonType.Instance.LoadRowTransform(m_transformXml, m_sourceStruct, m_targetStruct);
                if (!m_target.AvailableRowFormat)
                {
                    m_target.SetRowFormat(m_transform.OutputFormat);
                    m_targetStruct = m_transform.OutputFormat;
                }
            }

            GenericDataQueue queue = new GenericDataQueue(m_sourceStruct, m_transform.OutputFormat, m_transform);

            if (m_target.Connection != null && m_target.Connection.SystemConnection != null)
            {
                var fi            = m_source as IDataFormatHolder;
                var fmt           = fi != null ? fi.FormatSettings : new DataFormatSettings();
                var outputAdapter = new RecordToDbAdapter(m_transform.OutputFormat, targetFull, m_target.Connection.Dialect, fmt);
                outputAdapter.ProgressInfo = ProgressInfo;
                queue.AddOutputAdapter(outputAdapter);
            }

            m_source.ProgressInfo = ProgressInfo;
            m_target.ProgressInfo = ProgressInfo;

            IAsyncResult async_src = m_source.BeginRead(null, queue);
            IAsyncResult async_dst = m_target.BeginWrite(null, queue);

            if (async_src is ICancelable)
            {
                m_cancelSrc = (ICancelable)async_src;
            }
            if (async_dst is ICancelable)
            {
                m_cancelDst = (ICancelable)async_dst;
            }
            try
            {
                m_source.EndRead(async_src);
            }
            finally
            {
                m_target.EndWrite(async_dst);
            }

            Async.SafeClose(m_source.Connection);
            Async.SafeClose(m_target.Connection);
        }
コード例 #2
0
ファイル: CopyDbJob.cs プロジェクト: janproch/datadmin
        public static void CopyDatabase(IDatabaseSource src, IDatabaseWriter dst, IProgressInfo progress, DatabaseCopyOptions copyOpts)
        {
            IDatabaseWriter dst2 = null;

            for (; ;)
            {
                dst2 = dst.GetRedirectedWriter();
                if (dst2 == null)
                {
                    break;
                }
                dst = dst2;
            }
            dst.SetSourceInfo(new DatabaseWriterSourceInfo
            {
                Dialect = src.Dialect,
                //CopyMode = copyOpts.Mode,
                SchemaMode = copyOpts.SchemaMode,
            });
            try
            {
                dst.ProgressInfo = progress;

                Async.SafeOpen(src.Connection);
                dst.OpenConnection();

                if (dst.DirectCopy(src))
                {
                    dst.RunDirectCopy(src, copyOpts);
                }
                else
                {
                    copyOpts.CopyMembers.IgnoreSystemObjects = true;
                    IDatabaseStructure tmpDb    = src.InvokeLoadStructure(copyOpts.CopyMembers, progress);
                    DatabaseStructure  sourceDb = new DatabaseStructure(tmpDb);
                    //sourceDb.AutoFillRefs();
                    DatabaseStructure targetDb = sourceDb.GetMappedDatabase(copyOpts, dst.Dialect);
                    if (dst.Dialect != null)
                    {
                        dst.Dialect.MigrateDatabase(targetDb, copyOpts.MigrationProfile, progress);
                    }

                    if (copyOpts.CopyStructure)
                    {
                        dst.WriteStructureBeforeData(targetDb);
                    }

                    bool copydata = copyOpts.DataMode != DbCopyDataMode.None && src.TableCaps.DataStoreForReading && dst.WriterCaps.AcceptData;
                    if (copydata)
                    {
                        dst.BeforeFillData();

                        foreach (var tbl in sourceDb.Tables.SortedByKey <ITableStructure, int>(tbl => copyOpts.DataCopyTables.IndexOf(tbl.FullName)))
                        {
                            if (!copyOpts.CopyTableData(tbl.FullName))
                            {
                                continue;
                            }
                            Logging.Debug("Copying table {0}", tbl);
                            if (progress != null)
                            {
                                progress.SetCurWork(String.Format("{0} {1}", Texts.Get("s_copying_table"), tbl));
                            }
                            GenericDataQueue queue = new GenericDataQueue(tbl, tbl, new IdentityTransform(tbl));
                            queue.ProgressInfo = progress;
                            if (dst.WriterCaps.ExecuteSql)
                            {
                                var ada = new RecordToDbAdapter(tbl, tbl, dst.Dialect, new DataFormatSettings());
                                ada.ProgressInfo = progress;
                                queue.AddOutputAdapter(ada);
                            }
                            ITableSource      tsrc           = src.GetTable(tbl.FullName);
                            ITabularDataStore srcds          = tsrc.GetDataStoreAndReuse();
                            IAsyncResult      async_src      = srcds.BeginRead(null, queue);
                            ITableStructure   newTableStruct = (ITableStructure)targetDb.FindByGroupId(tbl.GroupId);
                            dst.FillTable(newTableStruct, queue, copyOpts.TableOptions);
                            srcds.EndRead(async_src);
                        }
                        dst.AfterFillData();
                    }
                    if (copyOpts.CopyStructure)
                    {
                        dst.WriteStructureAfterData(targetDb);
                    }
                }
            }
            catch (Exception)
            {
                dst.ProcessFailed();
                throw;
            }
            finally
            {
                Async.SafeClose(src.Connection);
                dst.CloseConnection();
            }
        }