XCode新增數據轉換功能(導數據)


用法:
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
DAL.AddConnStr( "xxgk" , "Data Source=192.168.1.21;Initial Catalog=信息公開;user id=sa;password=Pass@word" , null , "mssql" );
var dal = DAL.Create( "xxgk" );
 
DAL.AddConnStr( "xxgk2" , "Data Source=XXGK.db;Version=3;" , null , "sqlite" );
File.Delete( "XXGK.db" );
 
//DAL.ShowSQL = false;
 
var etf = new EntityTransform();
etf.SrcConn = "xxgk" ;
etf.DesConn = "xxgk2" ;
etf.AllowInsertIdentity = true ;
etf.TableNames.Remove( "PubInfoLog" );
//etf.OnTransformTable += (s, e) => { if (e.Arg.Name == "")e.Arg = null; };
var rs = etf.Transform();
Console.WriteLine( "共轉移:{0}" , rs);


其實你也可以自己實現,XCode內部代碼如下:
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using NewLife;
using NewLife.Collections;
using NewLife.Log;
#if !NET4
using NewLife.Reflection;
#endif
using XCode.DataAccessLayer;
 
namespace XCode.Transform
{
    /// <summary>實體轉換</summary>
    public class EntityTransform
    {
        #region 屬性
        private String _SrcConn;
        /// <summary>源</summary>
        public String SrcConn { get { return _SrcConn; } set { _SrcConn = value; } }
 
        private String _DesConn;
        /// <summary>目的</summary>
        public String DesConn { get { return _DesConn; } set { _DesConn = value; } }
 
        private ICollection<String> _TableNames;
        /// <summary>要導數據的表,為空表示全部</summary>
        public ICollection<String> TableNames
        {
            get
            {
                if (_TableNames == null )
                {
                    _TableNames = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
                    if (!String.IsNullOrEmpty(SrcConn))
                    {
                        foreach (var item in DAL.Create(SrcConn).Tables)
                        {
                            _TableNames.Add(item.Name);
                        }
                    }
                }
                return _TableNames;
            }
            set { _TableNames = value; }
        }
 
        private Int32 _BatchSize = 1000;
        /// <summary>每批處理多少行數據,默認1000</summary>
        public Int32 BatchSize { get { return _BatchSize; } set { _BatchSize = value; } }
 
        private Boolean _AllowInsertIdentity;
        /// <summary>是否允許插入自增列</summary>
        public Boolean AllowInsertIdentity { get { return _AllowInsertIdentity; } set { _AllowInsertIdentity = value; } }
        #endregion
 
        #region 方法
        /// <summary>把一個鏈接的數據全部導入到另一個鏈接</summary>
        /// <returns></returns>
        public Int32 Transform()
        {
            var dal = DAL.Create(SrcConn);
 
            var tables = dal.Tables;
            tables.RemoveAll(t => t.IsView);
            var tns = TableNames;
            if (tns != null && tns.Count > 0) tables.RemoveAll(t => !tns.Contains(t.Name) && !tns.Contains(t.Alias));
 
            var total = 0;
            foreach (var item in tables)
            {
                if (OnTransformTable != null )
                {
                    var e = new EventArgs<IDataTable>(item);
                    OnTransformTable( this , e);
                    if (e.Arg == null ) continue ;
                }
 
                total += TransformTable(dal.CreateOperate(item.Name));
            }
 
            return total;
        }
 
        /// <summary>把一個表的數據全部導入到另一個表</summary>
        /// <param name="eop">實體操作者。</param>
        /// <param name="getData">用於獲取數據的委托</param>
        /// <returns></returns>
        public Int32 TransformTable(IEntityOperate eop, Func<Int32, Int32, IEntityList> getData = null )
        {
            var name = eop.TableName;
            var count = eop.Count;
            if (getData == null ) getData = (start, max) => eop.FindAll( null , null , null , start, max);
 
            // 在目標鏈接上啟用事務保護
            eop.ConnName = DesConn;
            eop.BeginTransaction();
            try
            {
                XTrace.WriteLine( "{0} 共 {1}" , name, count);
 
                // 允許插入自增
                var oldII = eop.AllowInsertIdentity;
                if (AllowInsertIdentity) eop.AllowInsertIdentity = true ;
                // 關閉SQL日志
                var oldShowSql = DAL.ShowSQL;
                DAL.ShowSQL = false ;
 
                var total = 0;
                var index = 0;
                while ( true )
                {
                    eop.ConnName = SrcConn;
                    var list = getData(index, BatchSize);
                    if (list == null || list.Count < 1) break ;
                    index += list.Count;
 
                    // 處理事件,外部可以修改實體數據
                    if (OnTransformEntity != null )
                    {
                        var e = new EventArgs<IEntity>( null );
                        foreach (var entity in list)
                        {
                            e.Arg = entity;
                            OnTransformEntity( this , e);
                        }
                    }
 
                    eop.ConnName = DesConn;
                    var rs = list.Insert( true );
                    XTrace.WriteLine( "{0} 導入 {1}/{2} {3:p}" , name, index, count, (Double)index / count);
 
                    total += rs;
                }
                DAL.ShowSQL = oldShowSql;
                // 關閉插入自增
                if (AllowInsertIdentity) eop.AllowInsertIdentity = oldII;
 
                // 在目標鏈接上啟用事務保護
                eop.ConnName = DesConn;
                eop.Commit();
 
                return total;
            }
            catch (Exception ex)
            {
                XTrace.WriteLine( "{0} 錯誤 {1}" , name, ex.Message);
                // 在目標鏈接上啟用事務保護
                eop.ConnName = DesConn;
                eop.Rollback();
                throw ;
            }
        }
        #endregion
 
        #region 事件
        /// <summary>轉換表時觸發。如果參數被置空,表示不轉換該表</summary>
        public event EventHandler<EventArgs<IDataTable>> OnTransformTable;
 
        ///// <summary>轉換實體時觸發</summary>
        //public event EventHandler<EventArgs<IEntity>> OnTransformEntity;
 
        /// <summary>轉換實體時觸發</summary>
        public event EventHandler<EventArgs<IEntity>> OnTransformEntity;
        #endregion
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM