Example #1
0
        public void Parse(string sql, int dotIndex,out DataViewDB dvDB, out List<ParseErrorInfo> errors, out ContextMenu contextMenu)
        {
            errors = new List<ParseErrorInfo>();
            contextMenu = new ContextMenu();
            dvDB = null;

            try
            {
                var parseInfo = new ParseInfo();
                parseInfo.Type = ParseType.DataView;
                parseInfo.Index = dotIndex;
                parseInfo.Content = sql;
                var parseInfo_json = Newtonsoft.Json.JsonConvert.SerializeObject(parseInfo);
                string json = null;

                if (!ConnectProxy.Instance().Send(parseInfo_json))
                {
                    return;
                }
                json = ConnectProxy.Instance().Read();
                //if (string.IsNullOrWhiteSpace(json))//repeat call if retrive empty text
                //{
                //    if (!ConnectProxy.Instance().Send(parseInfo_json))
                //    {
                //        return;
                //    }
                //    json = ConnectProxy.Instance().Read();
                //}
                try
                {
                    var dvModel = JsonConvert.DeserializeObject<DataViewDB>(json);
                    dvDB = dvModel;
                    errors = dvModel.Errors;
                    dvModel.Parse();

                    foreach (var error in errors)
                    {
                        this.Errors.Add(error);
                    }
                    //
                }
                catch (Exception e)
                {
                    this.Errors.Add(new CodeHelper.Core.Error.ParseErrorInfo()
                    {
                        FileId = Guid.Empty,
                        File = "",
                        ErrorType = CodeHelper.Core.Error.ErrorType.Error,
                        Message = "模块解析错误: " + e.Message
                    });
                    Console.Out.WriteLine(e.StackTrace);
                }
            }
            catch
            {
            }
        }
Example #2
0
        private void GenEFFunction_OneTable(DataViewDB dvDB)
        {
            //获得对应的model
            var modelName = "unknown";

            modelName = GeneratorUtil.ClassName(this.txtResultModel.Text.Trim());

            //生成spacification
            var spec_class = new StringBuilder();
            //推断出model类名
            var condition_class = GeneratorUtil.ClassName(this.txtName.Text.Trim() + "_Condition");
            var condition_variable = "condition";

            spec_class.AppendLine(string.Format("public static ISpecification<{0}> {1}({2} {3})"
                , modelName, GeneratorUtil.ClassName(this.txtName.Text.Trim()), condition_class, condition_variable));

            spec_class.AppendLine("{");
            var spec = dvDB.Context.Unions[0].Context.Condition.RenderEFSpec(spec_class, modelName, condition_class, dvDB.Context.Parameters);
            if (string.IsNullOrWhiteSpace(spec))
            {
                spec_class.AppendLine(string.Format("\t\t return new DirectSpecification<{0}>(x=>true);", modelName));
            }
            else
            {
                spec_class.AppendLine(string.Format("\t\t return {0};", spec));
            }
            spec_class.AppendLine("}");

            var resultType = string.Format("List<{0}>", modelName);
            if (dvDB.IsPager)
            {
                resultType = string.Format("PageOfReturn<{0}>", modelName);
            }

            var functionName = GeneratorUtil.ClassName(this.txtName.Text);
            var conditionName = functionName + "_Condition";
            //var condition_variable = "condition";

            spec_class.AppendLine();
            spec_class.AppendLine();
            spec_class.AppendLine();
            spec_class.AppendLine(string.Format("public {0} {1}( {2} {3} )", resultType, functionName, conditionName, condition_variable));
            spec_class.AppendLine("{");
            spec_class.AppendLine(string.Format("\t var data = this.GetAll( {0}.{1}(condition ));", modelName + "Specification", functionName));
            var group = dvDB.Context.Unions[0].Context.Group;
            var returnFields = dvDB.Context.Unions[0].Context.ReturnFields;
            if (group != null && group.Fields.Count > 0)
            {
                spec_class.AppendLine(string.Format("\t data = data.GroupBy(x=> new "));
                spec_class.AppendLine("\t {");
                foreach (var f in group.Fields.OrderByName())
                {
                    spec_class.Append("\t\t x." + f);
                    if (f != group.Fields[group.Fields.Count - 1])
                    {
                        spec_class.AppendLine(", ");
                    }
                    else
                    {
                        spec_class.AppendLine();
                    }
                }
                spec_class.AppendLine("\t })");
                spec_class.AppendFormatLine("\t .Select(x => new {0}", modelName);
                spec_class.AppendLine("\t {");
                foreach (var f in returnFields.OrderByName())
                {
                    if (group.Fields.Contains(f.FullName))
                    {
                        spec_class.AppendFormatLine("\t\t {0} = x.Key.{0},", f.ShortName);
                    }
                    else
                    {
                        FunctionFieldInfo func = f as FunctionFieldInfo;
                        if (func == null)
                        {
                            spec_class.AppendFormatLine("\t\t {0} = x.{0},", f.ShortName);
                        }
                        else
                        {
                            if (func.FunctionName.Equals("count", StringComparison.OrdinalIgnoreCase))
                            {
                                spec_class.AppendFormatLine("\t\t {0} = x.Count(),", f.Alias);
                            }
                            else if (func.FunctionName.Equals("sum", StringComparison.OrdinalIgnoreCase))
                            {
                                spec_class.AppendFormatLine("\t\t {0} = x.Sum(o=>o.{1}),", f.Alias, func.ParameterFields[0].ShortName);
                            }
                            else if (func.FunctionName.Equals("avg", StringComparison.OrdinalIgnoreCase))
                            {
                                spec_class.AppendFormatLine("\t\t {0} = x.Average(o=>o.{1}),", f.Alias, func.ParameterFields[0].ShortName);
                            }
                        }
                    }
                }
                spec_class.AppendLine("\t }");

            }
            //order by
            if (dvDB.OrderPairs.Count > 0)
            {
                spec_class.AppendLine(string.Format("\t if( condition.OrderBy.HasValue )"));
                spec_class.AppendLine("\t {");
                spec_class.AppendLine("\t\t switch(condition.OrderBy)");
                spec_class.AppendLine("\t\t {");
                foreach (var order in dvDB.OrderPairs.OrderByName())
                {
                    if (order.OrderType == DataViewDB.OrderType.Asc)
                    {
                        spec_class.AppendLine(string.Format("\t\t case {0}.OrderByType.{1}:",
                            condition_class, GeneratorUtil.ClassName(order.SafeName + "_Asc")));

                        spec_class.AppendLine(string.Format("\t\t\t data = data.OrderBy(x=>x.{0});", order.FieldName));
                        spec_class.AppendLine("\t\t\t break;");

                    }
                    else if (order.OrderType == DataViewDB.OrderType.Desc)
                    {
                        spec_class.AppendLine(string.Format("\t\t case {0}.OrderByType.{1}:",
                            condition_class, GeneratorUtil.ClassName(order.SafeName + "_Desc")));
                        spec_class.AppendLine(string.Format("\t\t\t data = data.OrderByDescending(x=>x.{0});", order.FieldName));
                        spec_class.AppendLine("\t\t\t break;");

                    }
                }
                spec_class.AppendLine("\t\t default:");
                spec_class.AppendLine("\t\t\t break;");
                spec_class.AppendLine("\t\t }");
                spec_class.AppendLine("\t }");
            }
            var top = dvDB.Context.Unions[0].Context.TopValue;
            if (top != null)
            {
                if (top.IsParameter)
                {
                    spec_class.AppendLine(string.Format("\t data = data.Take(x.{0});", top.ClassName));
                }
                else if (top is ValueFieldInfo)
                {
                    spec_class.AppendLine(string.Format("\t data = data.Take({0});", ((ValueFieldInfo)top).Value));
                }
            }
            if (dvDB.IsPager)
            {
                spec_class.AppendLine(string.Format("\t var result = new {0}();", resultType));
                spec_class.AppendLine(string.Format("\t result.RecordCount = data.Count();", resultType));
                spec_class.AppendLine(string.Format("\t result.PageIndex = condition.PageIndex;", resultType));
                spec_class.AppendLine(string.Format("\t result.PageSize = condition.PageSize;", resultType));
                spec_class.AppendLine(string.Format("\t result.PageRecords = data.Skip(condition.PageSize * (condition.PageIndex - 1)).Take(condition.PageSize).ToList();", resultType));
                spec_class.AppendLine(string.Format("\t return result;", resultType));
            }
            else
            {
                spec_class.AppendLine(string.Format("\t return data.ToList();", resultType));
            }

            spec_class.AppendLine("}");

            spec_class.AppendLine();

            textEditorControl_Output.Document.TextContent = spec_class.ToString();// dvDB.Context.Render();

            textEditorControl_Output.Refresh();

            var frm = new ShowCodeFrm();
            frm.Show();
            frm.SetText(textEditorControl_Output.Document.TextContent);
        }
Example #3
0
        private void GenEFFunction_MulitTable(DataViewDB dvDB)
        {
            //获得对应的model
            var modelName = "unknown";

            modelName = GeneratorUtil.ClassName(this.txtResultModel.Text.Trim());

            var functionName = GeneratorUtil.ClassName(this.txtName.Text);
            var conditionName = functionName + "_Condition";
            var resultType = string.Format("List<{0}>", modelName);
            if (dvDB.IsPager)
            {
                resultType = string.Format("PageOfReturn<{0}>", modelName);
            }
            var condition_class = GeneratorUtil.ClassName(this.txtName.Text.Trim() + "_Condition");
            var condition_variable = "condition";

            var builder = new StringBuilder();
            builder.AppendLine();

            //生成相关的join条件类
            for (var i = 1; i < dvDB.Context.Unions[0].Context.TableJoinInfos.Count; i++)
            {
                List<FieldInfo> join_codition_fields = new List<FieldInfo>();
                var joinInfo_prev = dvDB.Context.Unions[0].Context.TableJoinInfos[i - 1];
                var joinInfo = dvDB.Context.Unions[0].Context.TableJoinInfos[i];
                var conditions = joinInfo.Condition.Conditions[0].CompareComplexConditionInfos;
                if (conditions.Count > 1)
                {
                    for (var index = 0; index < conditions.Count; index++)
                    {
                        var binary = conditions[index].AtomConditionInfo as BinaryConditionInfo;
                        join_codition_fields.Add(binary.LeftValue);
                    }
                    var join_info_name = GeneratorHelper.ClassName(this.txtName.Text.Trim() + "_" + joinInfo_prev.Alias + "_" + joinInfo.Alias + "_JoinCondition");
                    builder.AppendFormat("class {0}", join_info_name);
                    builder.AppendLine();
                    builder.AppendLine("{");
                    foreach (var field in join_codition_fields)
                    {
                        //关联时可能其中一个为可空
                        var systemType = EnumUtils.GetDescription(field.DbType);
                        if (this.IsValueType(field.DbType) && field.DbType != CodeHelper.Core.Parse.ParseResults.DataViews.DbType.String)
                            systemType += "?";

                        builder.AppendFormat("\tinternal {0} {1}", systemType, field.ClassName);
                        builder.AppendLine("\t{get;set;}");
                    }
                    builder.AppendLine("}");
                    builder.AppendLine();
                }
            }

            builder.AppendLine(string.Format("public {0} {1}( {2} {3} )", resultType, functionName, conditionName, condition_variable));
            builder.AppendLine("{");
            builder.AppendLine("\t " + DBGlobalService.DbContexUsingClause);
            builder.AppendLine("\t {");
            dvDB.Context.RenderEF(builder);
            //builder.AppendLine(string.Format("\t var data = this.GetAll( {0}.{1}(condition ));", modelName + "Specification", functionName));

            builder.Append(string.Format("\t var list = q_final.Select(x=>new {0}()", modelName));
            builder.AppendLine("{");
            var first_return_field = "";
            foreach (var f in dvDB.Context.Unions[0].Context.ReturnFields)
            {
                if (f is MutiField)
                {
                    var mutiField = f as MutiField;
                    foreach (var field in mutiField.AllFields)
                    {
                        if (string.IsNullOrWhiteSpace(first_return_field)) first_return_field = field.ClassName;
                        builder.AppendLine(string.Format("\t\t {0} = x.{1}.{2},", field.ClassName, mutiField.Table, field.FullName));
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(first_return_field)) first_return_field = f.ClassName;
                    builder.AppendLine(string.Format("\t\t {0} = x.{1},", f.ClassName, f.FullName));
                }
            }
            builder.AppendLine("\t });");

            //order by
            if (dvDB.OrderPairs.Count > 0)
            {
                builder.AppendLine(string.Format("\t if( condition.OrderBy.HasValue )"));
                builder.AppendLine("\t {");
                builder.AppendLine("\t\t switch(condition.OrderBy)");
                builder.AppendLine("\t\t {");
                foreach (var order in dvDB.OrderPairs)
                {
                    if (order.OrderType == DataViewDB.OrderType.Asc)
                    {
                        builder.AppendLine(string.Format("\t\t case {0}.OrderByType.{1}:",
                            condition_class, GeneratorUtil.ClassName(order.SafeName + "_Asc")));

                        builder.AppendLine(string.Format("\t\t\t list = list.OrderBy(x=>x.{0});", order.FieldName));
                        builder.AppendLine("\t\t\t break;");

                    }
                    else if (order.OrderType == DataViewDB.OrderType.Desc)
                    {
                        builder.AppendLine(string.Format("\t\t case {0}.OrderByType.{1}:",
                            condition_class, GeneratorUtil.ClassName(order.SafeName + "_Desc")));
                        builder.AppendLine(string.Format("\t\t\t list = list.OrderByDescending(x=>x.{0});", order.FieldName));
                        builder.AppendLine("\t\t\t break;");

                    }
                }
                builder.AppendLine("\t\t }");
                builder.AppendLine("\t }");
            }
            if (dvDB.IsPager)
            {
                if (dvDB.OrderPairs.Count == 0)
                {
                    builder.AppendLine(string.Format("\t list = list.OrderBy( x=>x.{0});//if no order,ef cannot pager!!!", first_return_field));
                }
                builder.AppendLine(string.Format("\t var result = new {0}();", resultType));
                builder.AppendLine(string.Format("\t result.RecordCount = list.Count();", resultType));
                builder.AppendLine(string.Format("\t result.PageIndex = condition.PageIndex;", resultType));
                builder.AppendLine(string.Format("\t result.PageSize = condition.PageSize;", resultType));
                builder.AppendLine(string.Format("\t result.PageRecords = list.Skip(condition.PageSize * (condition.PageIndex - 1)).Take(condition.PageSize).ToList();", resultType));
                builder.AppendLine(string.Format("\t return result;", resultType));
            }
            else
            {
                /*
                builder.Append(string.Format("\t return list.Select(x=>new {0}()", modelName));
                builder.AppendLine("{");
                foreach (var f in dvDB.Context.Unions[0].Context.ReturnFields)
                {
                    if (f is MutiField)
                    {
                        var mutiField = f as MutiField;
                        foreach (var field in mutiField.AllFields)
                        {
                            builder.AppendLine(string.Format("\t\t {0} = x.{1}.{2},", field.ClassName, mutiField.Table, field.FullName));
                        }
                    }
                    else
                    {
                        builder.AppendLine(string.Format("\t\t {0} = x.{1},", f.ClassName, f.FullName));
                    }
                }
                builder.AppendLine("\t }).ToList();");
                 */

                builder.AppendLine(string.Format("\t return list.ToList();", modelName));
            }

            builder.AppendLine("\t }");
            //builder.AppendLine(string.Format("\t return default({0});", resultType));
            builder.AppendLine("}");

            builder.AppendLine();

            textEditorControl_Output.Document.TextContent = builder.ToString();// dvDB.Context.Render();

            textEditorControl_Output.Refresh();

            var frm = new ShowCodeFrm();
            frm.Show();
            frm.SetText(textEditorControl_Output.Document.TextContent);
        }