Пример #1
0
        private static List <string> InnerCheckPartial(SQLMetaData config, string[] partial_file_names)
        {
            var ret   = new List <string>();
            var regex = new Regex(@"(?<=\[)[^\]]+(?=\])", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            // 先检测一波表是否能对应
            for (int i = 0; i < partial_file_names.Length; i++)
            {
                var file1         = partial_file_names[i];
                var tmp_file_name = file1.Substring(file1.LastIndexOf('\\') + 1).Replace("Helper.cs", "");
                var table         = config.Tables.Find(p => p.Name == tmp_file_name);
                if (table == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("partial文件对应的表 [" + tmp_file_name + "] 不存在于当前数据库中,这极有可能是因为连接到错误的数据库引起的!");
                    Console.ResetColor();
                    return(ret);
                }
            }

            for (int i = 0; i < partial_file_names.Length; i++)
            {
                var file1      = partial_file_names[i];
                var content1   = File.ReadAllText(file1);
                var matches1   = regex.Matches(content1);
                var key_words1 = new List <string>();
                foreach (Match item in matches1)
                {
                    if (item.Success)
                    {
                        if (key_words1.Find(p => p.ToLower() == item.Value.ToLower()) == null)
                        {
                            key_words1.Add(item.Value);
                        }
                    }
                }

                var tmp_file_name = file1.Substring(file1.LastIndexOf('\\') + 1).Replace("Helper.cs", "");
                var table         = config.Tables.Find(p => p.Name == tmp_file_name);
                var key_words2    = table.Columns.Select(p => p.Name.ToLower()).ToList();
                key_words2.Add(table.Name.ToLower());

                var sb = new StringBuilder();
                foreach (var item in key_words1)
                {
                    if (key_words2.Find(p => p == item.ToLower()) == null)
                    {
                        sb.Append(string.Format("{0}, ", item));
                    }
                }

                var str = sb.ToString().TrimEnd(", ");
                if (!string.IsNullOrWhiteSpace(str))
                {
                    ret.Add("[" + table.Name + "]: \r\n" + str);
                }
            }

            return(ret);
        }
Пример #2
0
        public MetaDataParser(SQLMetaData metaData)
        {
            if (metaData == null)
            {
                throw new ArgumentNullException("metaData");
            }

            _config         = metaData;
            _tableNeedCheck = new List <TableMetaData>();
        }
Пример #3
0
        public static void InitConfig(SQLMetaData config)
        {
            _project    = _configuration.Project ?? _project_default;
            _outputpath = _configuration.OutputBasePath ?? _outputpath_default;

            var model_headerNode  = _configuration.ModelConfig.HeaderNote ?? _headerNode_default;
            var model_using       = _configuration.ModelConfig.Using ?? string.Format(_using_default, string.Empty);
            var model_namespace   = _configuration.ModelConfig.Namespace ?? "Model";
            var model_baseClass   = _configuration.ModelConfig.BaseClass ?? _baseClass_default;
            var model_classPrefix = _configuration.ModelConfig.ClassPrefix ?? _classPrefix_default;
            var model_classSuffix = _configuration.ModelConfig.ClassSuffix ?? _classSuffix_default;

            var dal_headerNode = _configuration.DALConfig.HeaderNote ?? _headerNode_default;
            var dal_using      = _configuration.DALConfig.Using ?? string.Format(_using_default, "using Dapper;");

            dal_using += string.Format("using {0}.{1};", _project, model_namespace);
            var dal_namespace   = _configuration.DALConfig.Namespace ?? "DAL";
            var dal_baseClass   = _configuration.DALConfig.BaseClass ?? _baseClass_default;
            var dal_classPrefix = _configuration.DALConfig.ClassPrefix ?? _classPrefix_default;
            var dal_classSuffix = _configuration.DALConfig.ClassSuffix ?? _classSuffix_default;
            var dal_methods     = _configuration.DALConfig.Methods.Select(p => p.Name) ?? _methods_default;

            config.Model_HeaderNote      = string.Format(model_headerNode, Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            config.Model_Using           = model_using.Replace(';', ';').Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p + ";").ToList();
            config.Model_Namespace       = string.Format("{0}.{1}", _project, model_namespace);
            config.Model_BaseClass       = model_baseClass;
            config.Model_ClassNamePrefix = model_classPrefix;
            config.Model_ClassNameSuffix = model_classSuffix;

            config.DAL_HeaderNote      = string.Format(dal_headerNode, Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            config.DAL_Using           = dal_using.Replace(';', ';').Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p + ";").ToList();
            config.DAL_Namespace       = string.Format("{0}.{1}", _project, dal_namespace);
            config.DAL_BaseClass       = dal_baseClass;
            config.DAL_ClassNamePrefix = dal_classPrefix;
            config.DAL_ClassNameSuffix = dal_classSuffix;
            config.DAL_Methods         = dal_methods.ToList();

            config.PartialCheck_DAL_Path = _configuration.PartialCheck_DAL_Path ?? _partial_check_dal_path;
            config.TraceFieldTables      = _configuration.TraceFieldTables == null?Enumerable.Empty <string>().ToList() : _configuration.TraceFieldTables.Select(p => p.Name).ToList();

            config.EntityTables = _configuration.EntityTables == null?Enumerable.Empty <string>().ToList() : _configuration.EntityTables.Select(p => p.Name).ToList();

            config.ExceptTables = _configuration.ExceptTables == null?Enumerable.Empty <string>().ToList() : _configuration.ExceptTables.Select(p => p.Name).ToList();

            config.ExceptColumns = _configuration.UpdateExceptColumns == null ?
                                   new Dictionary <string, List <string> >() :
                                   _configuration.UpdateExceptColumns.ToDictionary(p => p.Key, p => p.Value.Select(k => k.ColumnName).ToList());
            config.JoinedTables = _configuration.JoinedTables == null ?
                                  new Dictionary <string, Tuple <string, string> >() :
                                  _configuration.JoinedTables.ToDictionary(p => p.Table_Main.Name, p => Tuple.Create(p.Table_Sub.Name, p.Sub_InnerName));
        }
Пример #4
0
        public static void OutputModel(SQLMetaData config, bool enableProgress = true)
        {
            var path = Path.Combine(_basePath, "Model");

            Directory.CreateDirectory(path);

            ConsoleProgressBar progress = null;

            if (enableProgress)
            {
                progress = GetProgressBar();
            }

            var sb = new StringBuilder();
            var g  = new ModelGenerator(config);

            // 解析
            for (int i = 0; i < config.Tables.Count; i++)
            {
                var table = config.Tables[i];
                if (config.ExceptTables.Contains(table.Name))
                {
                    continue;
                }
                sb.Append(config.Model_HeaderNote);
                sb.Append(string.Join(Environment.NewLine, config.Model_Using));
                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine(config.Model_Namespace);
                sb.AppendLine("{");
                sb.AppendLine(g.Get_Class(table.Name));
                sb.AppendLine("}");

                File.AppendAllText(Path.Combine(path, string.Format("{0}.cs", table.Name)), sb.ToString());
                sb.Clear();

                if (progress != null)
                {
                    // 打印进度
                    ProgressPrint(progress, (i + 1), config.Tables.Count);
                }
            }

            // 拷贝公用文件到指定目录
            DirHelper.CopyDirectory(Path.Combine("CopyFiles", "Model"), path);
        }
Пример #5
0
        public static void DoPartialCheck(SQLMetaData config)
        {
            if (string.IsNullOrWhiteSpace(config.PartialCheck_DAL_Path))
            {
                return;
            }
            var partial_path  = Path.Combine(config.PartialCheck_DAL_Path, "partial");
            var partial_files = Directory.GetFiles(partial_path);
            var list          = InnerCheckPartial(config, partial_files);

            if (list.Count > 0)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("以下字段可能有问题:");
                list.ForEach(p => Console.WriteLine(p));
                Console.ResetColor();
            }
            else
            {
                Console.WriteLine("检测完毕");
            }
        }
Пример #6
0
        public static void InitConfig(SQLMetaData config)
        {
            var model_headerNode  = ConfigurationManager.AppSettings["Model_HeaderNote"] ?? _headerNode_default;
            var model_using       = ConfigurationManager.AppSettings["Model_Using"] ?? string.Format(_using_default, string.Empty);
            var model_namespace   = ConfigurationManager.AppSettings["Model_Namespace"] ?? "Model";
            var model_baseClass   = ConfigurationManager.AppSettings["Model_BaseClass"] ?? _baseClass_default;
            var model_classPrefix = ConfigurationManager.AppSettings["Model_ClassPrefix"] ?? _classPrefix_default;
            var model_classSuffix = ConfigurationManager.AppSettings["Model_ClassSuffix"] ?? _classSuffix_default;

            var dal_headerNode = ConfigurationManager.AppSettings["DAL_HeaderNote"] ?? _headerNode_default;
            var dal_using      = ConfigurationManager.AppSettings["DAL_Using"] ?? string.Format(_using_default, "using Dapper;");

            dal_using += string.Format("using {0}.{1};", _project, model_namespace);
            var dal_namespace   = ConfigurationManager.AppSettings["DAL_Namespace"] ?? "DAL";
            var dal_baseClass   = ConfigurationManager.AppSettings["DAL_BaseClass"] ?? _baseClass_default;
            var dal_classPrefix = ConfigurationManager.AppSettings["DAL_ClassPrefix"] ?? _classPrefix_default;
            var dal_classSuffix = ConfigurationManager.AppSettings["DAL_ClassSuffix"] ?? _classSuffix_default;
            var dal_methods     = ConfigurationManager.AppSettings["DAL_Methods"] ?? _methods_default;

            config.PartialCheck_DAL_Path = ConfigurationManager.AppSettings["PartialCheck_DAL_Path"] ?? _partial_check_dal_path;
            config.DoPartialCheck        = string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["DoPartialCheck"]) ? _do_partial_check : bool.Parse(ConfigurationManager.AppSettings["DoPartialCheck"]);
            config.ExceptTables          = _exceptTables_default.Replace(';', ';').Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            config.Model_HeaderNote      = string.Format(model_headerNode, Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            config.Model_Using           = model_using.Replace(';', ';').Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p + ";").ToList();
            config.Model_Namespace       = string.Format("namespace {0}.{1}", _project, model_namespace);
            config.Model_BaseClass       = model_baseClass;
            config.Model_ClassNamePrefix = model_classPrefix;
            config.Model_ClassNameSuffix = model_classSuffix;

            config.DAL_HeaderNote      = string.Format(dal_headerNode, Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            config.DAL_Using           = dal_using.Replace(';', ';').Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => p + ";").ToList();
            config.DAL_Namespace       = string.Format("namespace {0}.{1}", _project, dal_namespace);
            config.DAL_BaseClass       = dal_baseClass;
            config.DAL_ClassNamePrefix = dal_classPrefix;
            config.DAL_ClassNameSuffix = dal_classSuffix;
            config.DAL_Methods         = dal_methods.Replace(',', ',').Split(',').ToList();
        }
Пример #7
0
 public EnumGenerator(SQLMetaData config)
 {
     this._config = config;
 }
 public ModelGenerator(SQLMetaData config)
 {
     this._config = config;
 }
Пример #9
0
        public static void OutputEnum(SQLMetaData config, bool enableProgress = true)
        {
            var path = Path.Combine(_basePath, "Enum");

            Directory.CreateDirectory(path);

            ConsoleProgressBar progress = null;

            if (enableProgress)
            {
                progress = GetProgressBar();
            }

            var sb = new StringBuilder();
            var g  = new EnumGenerator(config);
            // 解析
            var regex = new Regex(@"(?<=\[)[^\]]+(?=\])", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            for (int i = 0; i < config.Tables.Count; i++)
            {
                var table = config.Tables[i];
                if (config.ExceptTables.Contains(table.Name))
                {
                    continue;
                }

                foreach (var column in table.Columns)
                {
                    if (!string.IsNullOrWhiteSpace(column.Comment))
                    {
                        var match = regex.Match(column.Comment);
                        if (match.Success)
                        {
                            var comment   = match.Value.Replace(":", " ").Replace("、", " ").Replace("。", " ").Replace(";", " ").Replace(".", " ").Replace(";", " ").Replace(":", " ");
                            var tempname  = Regex.Replace(table.Name, @"\d", "").Replace("_", "");
                            var enum_name = string.Format("{0}_{1}_{2}", tempname, column.Name, "Enum");
                            if (_exist_enum.Contains(enum_name))
                            {
                                continue;
                            }
                            var arrs = comment.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            sb.Append(config.Model_HeaderNote);
                            sb.Append(string.Join(Environment.NewLine, config.Model_Using));
                            sb.AppendLine();
                            sb.AppendLine();
                            sb.AppendLine(string.Format("namespace {0}.{1}", _project, "GenEnum"));
                            sb.AppendLine("{");
                            sb.AppendLine(g.Get_Enum(enum_name, arrs));
                            sb.AppendLine("}");
                            File.AppendAllText(Path.Combine(path, string.Format("{0}.cs", enum_name)), sb.ToString());
                            sb.Clear();
                            _exist_enum.Add(enum_name);
                        }
                    }
                }

                if (progress != null)
                {
                    // 打印进度
                    ProgressPrint(progress, (i + 1), config.Tables.Count);
                }
            }

            // 拷贝公用文件到指定目录
            DirHelper.CopyDirectory(Path.Combine("CopyFiles", "Enum"), path);
        }
Пример #10
0
        public static void OutputDAL(SQLMetaData config, bool enableProgress = true)
        {
            var path = Path.Combine(_basePath, "DAL");

            Directory.CreateDirectory(path);

            ConsoleProgressBar progress = null;

            if (enableProgress)
            {
                progress = GetProgressBar();
            }

            var sb = new StringBuilder();
            var g  = new DALGenerator(config);

            // 解析
            for (int i = 0; i < config.Tables.Count; i++)
            {
                var table = config.Tables[i];
                if (config.ExceptTables.Contains(table.Name))
                {
                    continue;
                }
                sb.Append(config.DAL_HeaderNote);
                sb.Append(string.Join(Environment.NewLine, config.DAL_Using));
                sb.AppendLine();
                sb.AppendLine();
                sb.AppendLine(config.DAL_Namespace);
                sb.AppendLine("{");
                sb.AppendLine(string.Format("{0}public partial class {1}{2}{3}{4}",
                                            '\t',
                                            config.DAL_ClassNamePrefix,
                                            table.Name,
                                            config.DAL_ClassNameSuffix,
                                            string.IsNullOrWhiteSpace(config.DAL_BaseClass) ? string.Empty : (" : " + config.DAL_BaseClass)));
                sb.AppendLine(string.Format("{0}{{", '\t'));
                // 按方法生成
                foreach (var item in config.DAL_Methods)
                {
                    switch (item.ToLower())
                    {
                    case "exists":
                    {
                        sb.AppendLine(g.Get_Exists(table.Name));
                    }
                    break;

                    case "insert":
                    {
                        sb.AppendLine(g.Get_Insert(table.Name));
                    }
                    break;

                    case "delete":
                    {
                        sb.AppendLine(g.Get_Delete(table.Name));
                        sb.AppendLine(g.Get_BatchDelete(table.Name));
                    }
                    break;

                    case "update":
                    {
                        sb.AppendLine(g.Get_Update(table.Name));
                    }
                    break;

                    case "getmodel":
                    {
                        sb.AppendLine(g.Get_GetModel(table.Name));
                    }
                    break;

                    case "getlist":
                    {
                        sb.AppendLine(g.Get_GetList(table.Name));
                    }
                    break;

                    case "getcount":
                    {
                        sb.AppendLine(g.Get_Count(table.Name));
                    }
                    break;

                    case "getlistbypage":
                    {
                        sb.Append(g.Get_GetListByPage(table.Name));
                    }
                    break;
                    }
                }
                sb.AppendLine(string.Format("{0}}}", '\t'));
                sb.AppendLine("}");

                File.AppendAllText(Path.Combine(path, string.Format("{0}Helper.cs", table.Name)), sb.ToString());
                sb.Clear();

                if (progress != null)
                {
                    // 打印进度
                    ProgressPrint(progress, (i + 1), config.Tables.Count);
                }
            }

            // 拷贝公用文件到指定目录
            DirHelper.CopyDirectory(Path.Combine("CopyFiles", "DAL"), path);
        }
Пример #11
0
 private static bool IsExceptColumn(SQLMetaData config, string table, string colunm)
 {
     return(config.ExceptColumns.ContainsKey("*") && config.ExceptColumns["*"].Contains(colunm) ||
            config.ExceptColumns.ContainsKey(table) && config.ExceptColumns[table].Contains(colunm));
 }
Пример #12
0
        public static void OutputModel(SQLMetaData config, bool enableProgress = true)
        {
            var path = Path.Combine(_outputpath, "Model");

            Directory.CreateDirectory(path);

            ConsoleProgressBar progress = null;

            if (enableProgress)
            {
                progress = GetProgressBar();
            }

            var sb = new StringBuilder();
            var g  = new ModelGenerator(config);

            // 解析
            for (int i = 0; i < config.Tables.Count; i++)
            {
                var table = config.Tables[i];
                if (config.ExceptTables.Contains(table.Name))
                {
                    continue;
                }
                sb.Append(config.Model_HeaderNote);
                sb.AppendLine(string.Join(Environment.NewLine, config.Model_Using));
                sb.AppendLine($"using {config.DAL_Namespace};");
                sb.AppendLine($"using {config.DAL_Namespace}.Metadata;");
                sb.AppendLine();
                sb.AppendLine($"namespace {config.Model_Namespace}");
                sb.AppendLine("{");
                sb.AppendLine(g.Get_Class(table.Name));
                sb.AppendLine("}");

                File.AppendAllText(Path.Combine(path, string.Format("{0}.cs", table.Name)), sb.ToString());
                sb.Clear();

                if (progress != null)
                {
                    // 打印进度
                    ProgressPrint(progress, (i + 1), config.Tables.Count);
                }
            }

            // 如果配置文件指定了JoinedTables,那么这里需要为这些关联表生成额外的包装model,
            // 路径:Model\JoinedViewModel
            if (config.JoinedTables.Count > 0)
            {
                Directory.CreateDirectory(Path.Combine(path, "JoinedViewModel"));
                var sb2 = new StringBuilder();
                foreach (var pair in config.JoinedTables)
                {
                    var main_table = pair.Key;
                    var sub_table  = pair.Value;
                    sb2.Append(config.Model_HeaderNote);
                    sb2.AppendLine(string.Join(Environment.NewLine, config.Model_Using));
                    sb.AppendLine($"using {config.DAL_Namespace};");
                    sb2.AppendLine($"using {config.DAL_Namespace}.Metadata;");
                    sb2.AppendLine();
                    sb2.AppendLine($"namespace {config.Model_Namespace}.JoinedViewModel");
                    sb2.AppendLine("{");
                    sb2.AppendLine(g.Get_Joined_Class(main_table, sub_table));
                    sb2.AppendLine("}");

                    File.AppendAllText(Path.Combine(path, "JoinedViewModel", string.Format("{0}.cs", "Joined" + main_table)), sb2.ToString());
                    sb2.Clear();
                }
            }

            // 如果配置文件指定了EntityTables,那么这里需要生成实现接口IEntity接口的model
            // 路径:Model\EntityModel
            if (config.EntityTables.Count > 0)
            {
                Directory.CreateDirectory(Path.Combine(path, "EntityModel"));
                var sb2 = new StringBuilder();
                foreach (var talbe in config.EntityTables)
                {
                    sb2.Append(config.Model_HeaderNote);
                    sb2.AppendLine(string.Join(Environment.NewLine, config.Model_Using));
                    sb.AppendLine($"using {config.DAL_Namespace};");
                    sb2.AppendLine($"using {config.DAL_Namespace}.Metadata;");
                    sb2.AppendLine();
                    sb2.AppendLine($"namespace {config.Model_Namespace}.EntityModel");
                    sb2.AppendLine("{");
                    sb2.AppendLine(g.Get_Entity_Class(talbe));
                    sb2.AppendLine("}");

                    File.AppendAllText(Path.Combine(path, "EntityModel", string.Format("{0}.cs", "Entity" + talbe)), sb2.ToString());
                    sb2.Clear();
                }
            }

            // 拷贝公用文件到指定目录
            DirHelper.CopyDirectory(Path.Combine("CopyFiles", "Model"), path);
        }