Exemplo n.º 1
0
        public bool AddProperty(SQLTableInfo dbi, KeelExt.ModelKind km)
        {
            string C*K                   = "√";
            bool   i                     = false;
            string t_fieldname           = ClearBadChars(dbi.t_fieldname);
            CodeAttributeDeclaration cad = null;

            try
            {
                if (dbi.t_tablekey == C*K)
                {
                    cad = new CodeAttributeDeclaration(new CodeTypeReference(typeof(Keel.ORM.KeyFieldAttribute)));
                    cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_fieldname)));
                    cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(Keel.DB.Common.NowDataBase.PasteDBType(dbi.t_fieldtype).ToString())));
                    if (dbi.t_identity != C*K)
                    {
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(false)));
                    }
                }
                else
                {
                    cad = new CodeAttributeDeclaration(new CodeTypeReference(typeof(Keel.ORM.DataFieldAttribute)));
                    cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_fieldname)));
                    cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(Keel.DB.Common.NowDataBase.PasteDBType(dbi.t_fieldtype).ToString())));
                    if (Kit.SlnKeel.ModelForUI)
                    {
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression((object)dbi.t_fielddefaultvalue)));
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression((dbi.t_fieldcannull == C*K))));
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_fielddesc)));
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_fieldindex)));
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_fieldlenght)));
                    }
                    if (dbi.t_fieldiscomputed == C*K || dbi.t_identity == C*K)
                    {
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_fieldiscomputed == C*K)));
                        cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dbi.t_identity == C*K)));
                    }
                }

                Type t = Keel.DB.Common.NowDataBase.PasteType(dbi.t_fieldtype);
                switch (km)
                {
                case KeelExt.ModelKind.DotNetAndC:
                    int len = (int)dbi.t_fieldbitcount > dbi.t_fieldlenght ? dbi.t_fieldbitcount : dbi.t_fieldlenght;
                    i = AddPropertyStruct(t_fieldname, t, cad, len, dbi.t_fielddesc.ToString());
                    break;

                case KeelExt.ModelKind.DotNetAndDotNet:
                default:
                    i = AddProperty(t_fieldname, t, cad, (string)dbi.t_fielddesc);
                    break;
                }
            }
            catch (Exception ex)
            {
                Common.ShowInfo(string.Format("在处理属性{0}时遇到未料到的异常:{1}", dbi.t_fieldname, ex.Message));
            }
            return(i);
        }
Exemplo n.º 2
0
        public static void GeneratedCode(KeelExt.ModelKind mk)
        {
            Common.ShowInfo("正在检查数据库是否可用!");
            if (Kit.CheckDataBase())
            {
                Common.ShowInfo("正在从数据库中读取表信息...");
                EnvDTE.Project      pjt    = Kit.GetProjectModel();
                List <SQLTableInfo> dbix   = GetSqlTableInfoList();
                string[]            lsttab = (Kit.SlnKeel.DataTables + Kit.SlnKeel.DataViews).Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string keelcpp             = null;
                foreach (var item in lsttab)
                {
                    #region 生成一个model
                    tablenamenow = item;
                    Common.ShowInfo(string.Format("正在生成{0}的实体,请稍候...", tablenamenow));
                    Predicate <SQLTableInfo> pdbi  = new Predicate <SQLTableInfo>(FindByTableName);
                    List <SQLTableInfo>      lstdb = dbix.FindAll(pdbi);
                    ModelGengerator          mg    = new ModelGengerator(Kit.GetRootNamespace(pjt), item);
                    if (mk == KeelExt.ModelKind.DotNetAndC)
                    {
                        mg.AddStruct(tablenamenow);
                        mg.AddFunForStructModel();
                    }
                    foreach (var tableitem in lstdb)
                    {
                        mg.AddProperty(tableitem, mk);
                        mg.AddTableNameConst(tableitem.t_fieldname, tableitem.t_tabledesc);
                    }
                    mg.AddCtor(lstdb, mk);
                    cfLangType cl       = Kit.GetProjectLangType(pjt);
                    string     filename = mg.Save(pjt.FullName, cl);

                    AddFileToProject(pjt, filename);
                    if (cl == cfLangType.CPP)
                    {
                        keelcpp += string.Format("#include \"{0}\"", new System.IO.FileInfo(filename).Name) + Environment.NewLine;
                    }
                    Common.ShowInfo(string.Format("恭喜!{0}的实体保存完毕!", tablenamenow));
                    #endregion
                }
                ForCpptoDo(pjt, keelcpp);
                Common.ShowInfo(string.Format("全部实体生成完毕,开始生产DBContext"));
                GenDAL(lsttab);
                Common.ShowInfo(string.Format("KeelKit所需生成的内容生成完毕!"));
            }
        }