示例#1
0
        public static void GetTargetsType(string From, string Target, ref DBColumnInfo Column)
        {
            if (string.IsNullOrWhiteSpace(Target) || From == Target)
            {
                return;
            }

            if (ODATypeMap != null && ODATypeMap.ContainsKey(From))
            {
                var FromDict = ODATypeMap[From] as Dictionary <string, object>;

                if (FromDict != null && FromDict.ContainsKey(Target))
                {
                    var TargetDict = FromDict[Target] as Dictionary <string, object>;
                    if (TargetDict != null && TargetDict.ContainsKey(Column.ColumnType))
                    {
                        string colType = TargetDict[Column.ColumnType].ToString();
                        Column.IsBigData = false;
                        string[] typeInfo = colType.Split(',');
                        if (typeInfo.Length > 0)
                        {
                            Column.ColumnType = typeInfo[0];
                        }
                        if (typeInfo.Length > 1)
                        {
                            if (typeInfo[1] == "0")
                            {
                                Column.NoLength = true;
                                Column.Length   = 0;
                            }
                            else
                            {
                                try
                                {
                                    int setLength = 0;
                                    int.TryParse(typeInfo[1], out setLength);
                                    if (Column.Length == 0 && setLength != 0)
                                    {
                                        Column.Length = setLength;
                                    }
                                    else if (setLength != 0)
                                    {
                                        Column.Length = Column.Length * setLength;
                                    }
                                }
                                catch { }
                            }
                        }
                        if (typeInfo.Length > 2 && typeInfo[1].Trim().ToLower() == "y")
                        {
                            Column.IsBigData = true;
                        }
                    }
                }
            }
        }
示例#2
0
        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] rtlMsg = new string[2];
            try
            {
                StringBuilder    sbrlt = new StringBuilder();
                TransferParams   prm   = e.Argument as TransferParams;
                BackgroundWorker bgw   = sender as BackgroundWorker;

                StringBuilder tblScript = new StringBuilder();
                string        TargetDB  = prm.TargetDB.DBAType.ToString();

                var TblPkeys = prm.SourceDB.GetPrimarykey();

                for (int i = 0; i < prm.TranTable.Count; i++)
                {
                    ReportStatus RS = new ReportStatus()
                    {
                        Percent     = i * 100 / prm.TranTable.Count,
                        TransObject = prm.TranTable[i],
                        TransType   = "Table"
                    };
                    bgw.ReportProgress(RS.Percent, RS);

                    DataRow[] drs = prm.SrcTables.Select("TABLE_NAME ='" + prm.TranTable[i] + "'");

                    if (drs == null || drs.Length == 0)
                    {
                        continue;
                    }
                    DBColumnInfo[] ColumnInfo = new DBColumnInfo[drs.Length];
                    ODAParameter[] Oprms      = new ODAParameter[drs.Length];

                    bool isBigData = false;
                    try
                    {
                        for (int j = 0; j < drs.Length; j++)
                        {
                            int Scale = 0;
                            int.TryParse(drs[j]["SCALE"].ToString().Trim(), out Scale);
                            int length = 2000;
                            int.TryParse(drs[j]["LENGTH"].ToString().Trim(), out length);
                            string ColumnName = drs[j]["COLUMN_NAME"].ToString().Trim();

                            DBColumnInfo DBColInfo = new DBColumnInfo()
                            {
                                ColumnName = ColumnName,
                                ColumnType = drs[j]["DATATYPE"].ToString().Trim(),
                                Length     = length,
                                Scale      = Scale,
                                NotNull    = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y",
                            };
                            CurrentDatabase.GetTargetsType(prm.SourceDB.DBAType.ToString(), TargetDB, ref DBColInfo);
                            ColumnInfo[j] = DBColInfo;
                            isBigData     = isBigData || DBColInfo.IsBigData;

                            DBColumnInfo ODAColInfo = new DBColumnInfo()
                            {
                                ColumnName = ColumnName,
                                ColumnType = drs[j]["DATATYPE"].ToString().Trim(),
                                Length     = length,
                                Scale      = Scale,
                                NotNull    = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y",
                            };
                            CurrentDatabase.GetTargetsType(prm.SourceDB.DBAType.ToString(), "ODA", ref ODAColInfo);
                            ODAdbType OdaType = ODAdbType.OVarchar;

                            OdaType = (ODAdbType)Enum.Parse(typeof(ODAdbType), ODAColInfo.ColumnType, true);

                            Oprms[j] = new ODAParameter()
                            {
                                ColumnName = drs[j]["COLUMN_NAME"].ToString(),
                                DBDataType = OdaType,
                                Direction  = ParameterDirection.Input,
                                ParamsName = drs[j]["COLUMN_NAME"].ToString(),
                                Size       = ColumnInfo[j].Length
                            };
                        }
                    }
                    catch (Exception ex)
                    {
                        sbrlt.AppendLine(string.Format("分析表【{0}】字段时发生异常,异常信信:{1} ", prm.TranTable[i], ex.Message));
                    }
                    string sql = "";
                    try
                    {
                        string[] Pkeys = null;
                        if (TblPkeys != null && TblPkeys.ContainsKey(prm.TranTable[i]))
                        {
                            Pkeys = TblPkeys[prm.TranTable[i]];
                        }
                        sql = this.CreateTable(prm.TargetDB, prm.TranTable[i], ColumnInfo, Pkeys);
                        tblScript.AppendLine(sql);
                    }
                    catch (Exception ex)
                    {
                        sbrlt.AppendLine(string.Format("读取表【{0}】主键并生成建表脚本时生异常,异常信信:{1} ", prm.TranTable[i], ex.Message));
                    }

                    try
                    {
                        if (prm.NeedTransTable)
                        {
                            if (prm.NeedTransTable)
                            {
                                try
                                {
                                    string dropSQL = "DROP TABLE " + prm.TranTable[i];

                                    prm.TargetDB.ExecuteSQL(dropSQL, null);
                                }
                                catch { }
                                prm.TargetDB.ExecuteSQL(sql.ToString(), null);
                            }
                            ReportStatus RST = new ReportStatus()
                            {
                                Percent     = (i + 1) * 100 / prm.TranTable.Count,
                                TransObject = "Table [" + prm.TranTable[i] + "] Created",
                                TransType   = "Table"
                            };
                            bgw.ReportProgress(RS.Percent, RST);
                        }
                    }
                    catch (Exception ex)
                    {
                        sbrlt.AppendLine(string.Format("创建表【{0}】时发生异常,建表脚本  {1}   ,异常信信:{2} ", prm.TranTable[i], sql.ToString(), ex.Message));
                    }
                    try
                    {
                        if (prm.NeedTransData)
                        {
                            int       total     = 0;
                            int       maxR      = isBigData ? 50 : 10000;
                            int       startIndx = 0;
                            DataTable DT_total  = CurrentDatabase.DataSource.Select("SELECT COUNT(*) FROM " + prm.TranTable[i], null);
                            int.TryParse(DT_total.Rows[0][0].ToString(), out total);
                            while (startIndx < total)
                            {
                                ReportStatus RSData0 = new ReportStatus()
                                {
                                    Percent     = total == 0 ? 0 : startIndx * 100 / total,
                                    TransObject = prm.TranTable[i] + " Preparing " + startIndx.ToString() + " ~ " + (startIndx + maxR).ToString() + "/" + total.ToString() + " record ",
                                    TransType   = "Data"
                                };
                                bgw.ReportProgress(RS.Percent, RSData0);
                                DataTable Source = CurrentDatabase.DataSource.Select("SELECT * FROM " + prm.TranTable[i], null, startIndx, maxR, null);
                                Source.TableName = prm.TranTable[i];

                                int          endIdx  = (startIndx + maxR) > total ? total : startIndx + maxR;
                                ReportStatus RSData1 = new ReportStatus()
                                {
                                    Percent     = total == 0 ? 0 : endIdx * 100 / total,
                                    TransObject = prm.TranTable[i] + " Importing " + startIndx.ToString() + " ~ " + endIdx.ToString() + "/" + total.ToString() + " record ",
                                    TransType   = "Data"
                                };
                                bgw.ReportProgress(RS.Percent, RSData1);

                                TarDB.Import(Source, Oprms);
                                startIndx = startIndx + maxR;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        sbrlt.AppendLine(string.Format("导入数据到表【{0}】时发生异常:{1} ", prm.TranTable[i], ex.Message));
                    }
                }
                if (sbrlt.Length == 0)
                {
                    sbrlt.Append("数据复制完成!");
                }
                rtlMsg[0] = sbrlt.ToString();
                rtlMsg[1] = tblScript.ToString();
            }
            catch (Exception ex)
            {
                rtlMsg[0] = ex.ToString();
            }
            e.Result = rtlMsg;
        }
示例#3
0
        public string[] Generate_Code(params string[] TablesAndViews)
        {
            System.Text.StringBuilder strbldr   = new System.Text.StringBuilder();
            System.Text.StringBuilder ModelCode = new System.Text.StringBuilder();

            strbldr.AppendLine("using System;");
            strbldr.AppendLine("using System.Data;");
            strbldr.AppendLine("using System.Collections.Generic;");
            strbldr.AppendLine("using System.Reflection;");
            strbldr.AppendLine("using NYear.ODA;");
            strbldr.AppendLine("using NYear.ODA.Model;");
            strbldr.AppendLine();
            strbldr.AppendLine("namespace NYear.ODA.Cmd");
            strbldr.AppendLine("{");

            ModelCode.AppendLine("using System;");
            ModelCode.AppendLine();
            ModelCode.AppendLine("namespace NYear.ODA.Model");
            ModelCode.AppendLine("{");

            DataTable pdt_tables = _DBA.GetTableColumns();
            DataTable pdt_views  = _DBA.GetViewColumns();

            for (int i = 0; i < TablesAndViews.Length; i++)
            {
                string TablePascalName                  = TablesAndViews[i];
                System.Text.StringBuilder strCmd        = new System.Text.StringBuilder();
                System.Text.StringBuilder strModel      = new System.Text.StringBuilder();
                System.Text.StringBuilder GetColumnList = new StringBuilder();
                GetColumnList.AppendLine("\t\t public override List<ODAColumns> GetColumnList() ");
                GetColumnList.AppendLine("\t\t { ");
                GetColumnList.Append("\t\t\t return new List<ODAColumns>() { ");

                DataRow[] drs = pdt_tables.Select("TABLE_NAME ='" + TablesAndViews[i] + "'");
                if (drs.Length < 1)
                {
                    drs = pdt_views.Select("TABLE_NAME ='" + TablesAndViews[i] + "'");
                    strCmd.AppendLine("\t\t public override bool Insert(params IODAColumns[] Cols) { throw new ODAException(\"Not suport Insert CmdName \" + CmdName);}");
                    strCmd.AppendLine("\t\t public override bool Update(params IODAColumns[] Cols) {  throw new ODAException(\"Not Suport Update CmdName \" + CmdName);}");
                    strCmd.AppendLine("\t\t public override bool Delete() {  throw new ODAException(\"Not Suport Delete CmdName \" + CmdName);}");
                }

                for (int j = 0; j < drs.Length; j++)
                {
                    int Scale = 0;
                    int.TryParse(drs[j]["SCALE"].ToString().Trim(), out Scale);
                    int length = 2000;
                    int.TryParse(drs[j]["LENGTH"].ToString().Trim(), out length);
                    string ColumnName       = drs[j]["COLUMN_NAME"].ToString().Trim();
                    string ColumnPascalName = "Col" + this.Pascal(ColumnName);

                    DBColumnInfo CsharpColumnInfo = new DBColumnInfo()
                    {
                        ColumnName = ColumnName,
                        ColumnType = drs[j]["DATATYPE"].ToString().Trim(),
                        Length     = length,
                        Scale      = Scale,
                        NotNull    = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y",
                    };
                    DBColumnInfo ODAColInfo = new DBColumnInfo()
                    {
                        ColumnName = ColumnName,
                        ColumnType = drs[j]["DATATYPE"].ToString().Trim(),
                        Length     = length,
                        Scale      = Scale,
                        NotNull    = drs[j]["NOT_NULL"].ToString().Trim().ToUpper() == "Y",
                    };


                    CurrentDatabase.GetTargetsType(CurrentDatabase.DataSource.DBAType.ToString(), "CSHARP", ref CsharpColumnInfo);
                    CurrentDatabase.GetTargetsType(CurrentDatabase.DataSource.DBAType.ToString(), "ODA", ref ODAColInfo);
                    strModel.AppendLine("\t\t public " + CsharpColumnInfo.ColumnType + " " + ColumnName + " {get; set;}");
                    strCmd.AppendLine("\t\t public ODAColumns " + ColumnPascalName + "{ get { return new ODAColumns(this, \"" + ColumnName + "\", ODAdbType." + ODAColInfo.ColumnType + ", " + ODAColInfo.Length + "," + (drs[j]["NOT_NULL"].ToString().Trim() == "Y" ? "true" : "false") + " ); } }");
                    GetColumnList.Append(ColumnPascalName + ",");
                }

                strCmd.AppendLine("\t\t public override string CmdName { get { return \"" + TablesAndViews[i] + "\"; }}");
                strCmd.AppendLine(GetColumnList.Remove(GetColumnList.Length - 1, 1).ToString() + "};");
                strCmd.AppendLine("\t\t }");

                ModelCode.AppendLine("\tpublic partial class " + TablePascalName);
                ModelCode.AppendLine("\t{");
                ModelCode.Append(strModel);
                ModelCode.AppendLine("\t}");

                ////Cmd的代碼
                strbldr.Append("\tinternal partial class ");
                strbldr.Append("Cmd" + Pascal(TablePascalName));
                strbldr.AppendLine(":ORMCmd<" + TablePascalName + ">");
                strbldr.AppendLine("\t{");
                strbldr.Append(strCmd);
                strbldr.AppendLine("\t}");
            }

            strbldr.AppendLine("}");
            ModelCode.AppendLine("}");
            return(new string[] { strbldr.ToString(), ModelCode.ToString() });
        }