Esempio n. 1
0
        public void Generate(string cname)
        {
            Class clss = new Class(cname)
            {
                Modifier = Modifier.Public | Modifier.Partial
            };

            builder.AddClass(clss);

            foreach (VAR var in DS.Names)
            {
                VAL val = DS[var];
                createClass(clss, string.Empty, (string)var, val, classOnly: false);
            }
        }
Esempio n. 2
0
        public Packing(Type dpoType)
        {
            this.dpoType = dpoType;
            instance     = (PersistentObject)Activator.CreateInstance(this.dpoType);

            this.publicFields = dpoType.GetFields(BindingFlags.Public | BindingFlags.Instance);    //ignore public const fields

            Type baseType = typeof(BasePackage <>);

            baseType = baseType.MakeGenericType(dpoType);

            this.classBuilder = new CSharpBuilder()
            {
                Namespace = dpoType.Assembly.GetName().Name + "." + Setting.DPO_PACKAGE_SUB_NAMESPACE,
            };

            this.classBuilder.AddUsing("System")
            .AddUsing("System.Data")
            .AddUsing("System.Text")
            .AddUsing("System.Collections.Generic")
            .AddUsing("Sys")
            .AddUsing("Sys.Data")
            .AddUsing("Sys.Data.Manager")
            .AddUsing(dpoType.Namespace);


            var clss = new Class(ClassName, new CodeBuilder.TypeInfo {
                Type = baseType
            })
            {
                Modifier = Modifier.Public
            };



            //constructor
            clss.Add(new Constructor(ClassName));

            this.pack = new Method("Pack")
            {
                Modifier = Modifier.Protected | Modifier.Override
            };
            clss.Add(pack);

            classBuilder.AddClass(clss);
        }
Esempio n. 3
0
        private CSharpBuilder CreateClass(IEnumerable <Buildable> elements)
        {
            CSharpBuilder builder = new CSharpBuilder {
                Namespace = NamespaceName
            };
            Class clss = new Class(ClassName)
            {
                Modifier = Modifier.Public | Modifier.Static | Modifier.Partial
            };

            builder.AddUsing("System");

            clss.AddRange(elements);

            builder.AddClass(clss);
            return(builder);
        }
Esempio n. 4
0
        public Packing(Type dpoType)
        {
            this.dpoType = dpoType;
            instance = (PersistentObject)Activator.CreateInstance(this.dpoType);

            this.publicFields = dpoType.GetFields(BindingFlags.Public | BindingFlags.Instance);    //ignore public const fields

            Type baseType = typeof(BasePackage<>);
            baseType = baseType.MakeGenericType(dpoType);

            this.classBuilder = new CSharpBuilder()
            {
                nameSpace = dpoType.Assembly.GetName().Name + "." + Setting.DPO_PACKAGE_SUB_NAMESPACE,
            };

            this.classBuilder.AddUsing("System")
            .AddUsing("System.Data")
            .AddUsing("System.Text")
            .AddUsing("System.Collections.Generic")
            .AddUsing("Sys")
            .AddUsing("Sys.Data")
            .AddUsing("Sys.Data.Manager")
            .AddUsing(dpoType.Namespace);

            var clss = new Class(ClassName, new CodeBuilder.TypeInfo { type = baseType })
            {
                modifier = Modifier.Public
            };

            //constructor
            clss.Add(new Constructor(ClassName));

            this.pack = new Method("Pack") { modifier = Modifier.Protected | Modifier.Override };
            clss.Add(pack);

            classBuilder.AddClass(clss);
        }
Esempio n. 5
0
        /// <summary>
        /// create C# data from data table
        /// </summary>
        /// <param name="cmd"></param>
        public void ExportCSharpData(Command cmd)
        {
            if (!(SqlShell.LastResult is DataTable))
            {
                stdio.ErrorFormat("display data table first by sql clause or command [type]");
                return;
            }

            string ns = cmd.GetValue("ns") ?? "Sql.Data";
            string cname = cmd.GetValue("class") ?? "Table";

            var dt = SqlShell.LastResult as DataTable;

            var builder = new CSharpBuilder { nameSpace = ns };
            var clss = new Class(cname)
            {
                modifier = Modifier.Public | Modifier.Partial
            };

            builder.AddClass(clss);

            Property prop;
            foreach (DataColumn column in dt.Columns)
            {
                bool nullable = dt.AsEnumerable().Any(row => row[column] is DBNull);
                TypeInfo ty = new TypeInfo(column.DataType) { Nullable = nullable };

                prop = new Property(ty, column.ColumnName.ToFieldName()) { modifier = Modifier.Public};
                clss.Add(prop);
            }

            clss = new Class(cname + "Data")
            {
                modifier = Modifier.Public
            };
            builder.AddClass(clss);

            Func<int, string> tab = n => new string('\t', n);

            string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray();
            List<string> L = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                List<string> V = new List<string>();
                for (int i = 0; i < columns.Length; i++)
                {
                    V.Add(string.Format("{0} = {1}", columns[i], VAL.Boxing(row[i]).ToString()));
                }

                L.Add($"{tab(3)}new {cname} {{ " + string.Join(", ", V) + " }");
            }

            var value = $"new {cname}[]\n" + $"{tab(2)}{{\n" + string.Join(",\n", L) + $"\n{tab(2)}}}";

            Field field = new Field(new TypeInfo { userType = $"{cname}[]" }, "data")
            {
                modifier = Modifier.Public | Modifier.Static | Modifier.Readonly,
                userValue = value
            };

            clss.Add(field);

            string code = $"{builder}";

            string path = cmd.GetValue("out");
            if (path == null)
            {
                stdio.WriteLine(code);
            }
            else
            {
                string file = Path.ChangeExtension(Path.Combine(path, cname), "cs");
                code.WriteIntoFile(file);
                stdio.WriteLine("code generated on {0}", file);
            }
        }
Esempio n. 6
0
        private void ExportConstant(DataTable dt)
        {
            //command: export /c# /type:const /field:col1,col2 /value:col3,col4
            string[] optionColumns   = cmd.GetStringArray("field");
            string[] optionConstants = cmd.GetStringArray("value");

            if (optionColumns.Length == 0)
            {
                cerr.WriteLine("missing parameter /field:col1,col2");
                return;
            }

            if (optionConstants.Length == 0)
            {
                optionConstants = optionColumns;
            }
            else if (optionColumns.Length != optionConstants.Length)
            {
                cerr.WriteLine($"invalid parameter /value:{string.Join(",", optionConstants)}");
                return;
            }

            CSharpBuilder builder = new CSharpBuilder()
            {
                Namespace = NamespaceName
            };

            builder.AddUsingRange(base.Usings);

            string cname = ClassName;
            Class  clss  = new Class(cname)
            {
                Modifier = Modifier.Public | Modifier.Static
            };

            builder.AddClass(clss);

            SortedDictionary <string, object> dict = new SortedDictionary <string, object>();
            Type type = null;

            int i = 0;

            foreach (string column in optionColumns)
            {
                string constant = optionConstants[i++];

                Type _type = dt.Columns[constant].DataType;
                if (type == null)
                {
                    type = _type;
                }
                else if (type != _type)
                {
                    cerr.WriteLine($"column [{constant}] data type is imcompatible");
                    continue;
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[column] == DBNull.Value)
                    {
                        continue;
                    }

                    string key = row.Field <string>(column);
                    if (!dict.ContainsKey(key))
                    {
                        dict.Add(key, row[constant]);
                    }
                }
            }

            foreach (var kvp in dict)
            {
                string fieldName = Sys.ident.Identifier(kvp.Key);

                Field field = new Field(new TypeInfo(type), fieldName, new Value(kvp.Value))
                {
                    Modifier = Modifier.Public | Modifier.Const,
                    Comment  = new Comment(kvp.Key),
                };

                clss.Add(field);
            }

            PrintOutput(builder, cname);
        }
Esempio n. 7
0
        /// <summary>
        /// create C# data from data table
        /// </summary>
        /// <param name="cmd"></param>
        public void ExportCSData(DataTable dt)
        {
            string dataclass = cmd.GetValue("dataclass") ?? "DbReadOnly";

            CSharpBuilder builder = new CSharpBuilder
            {
                Namespace = NamespaceName
            };

            builder.AddUsingRange(base.Usings);

            string cname = ClassName;

            Dictionary <string, TypeInfo> codeColumns = CodeColumnDef();
            var clss = new Class(cname)
            {
                Modifier = Modifier.Public | Modifier.Partial
            };

            if (!cmd.Has("dataonly"))
            {
                builder.AddClass(clss);
            }

            Property prop;

            foreach (DataColumn column in dt.Columns)
            {
                bool     nullable = dt.AsEnumerable().Any(row => row[column] is DBNull);
                TypeInfo ty       = new TypeInfo(column.DataType)
                {
                    Nullable = nullable
                };
                if (codeColumns.ContainsKey(column.ColumnName))
                {
                    ty = codeColumns[column.ColumnName];
                }

                prop = new Property(ty, column.ColumnName.ToFieldName())
                {
                    Modifier = Modifier.Public
                };
                clss.Add(prop);
            }

            clss = new Class(dataclass)
            {
                Modifier = Modifier.Public | Modifier.Partial
            };

            if (!cmd.Has("classonly"))
            {
                builder.AddClass(clss);
            }


            string[] columns = dt.Columns.Cast <DataColumn>().Select(col => col.ColumnName).ToArray();

            string fieldName = cmd.GetValue("dataname") ?? $"{cname}Data";

            if (dataType == DataClassType.List || dataType == DataClassType.Array)
            {
                Field field = CreateListOrArrayField(fieldName, dataType, dt, cname, columns, codeColumns);
                clss.Add(field);
            }
            else
            {
                if (dt.Columns.Count < 2)
                {
                    cerr.WriteLine("cannot generate dictionary class, column# > 2");
                    return;
                }

                Field field = CreateDictionaryField(fieldName, dt, cname, columns, codeColumns);
                clss.Add(field);
            }

            PrintOutput(builder, cname);
        }
Esempio n. 8
0
        public void ExportCSharpData()
        {
            string code = LoadCode();

            if (code == null)
            {
                return;
            }

            ClassType ctype = getClassType();

            string _GetValueMethodName    = cmd.GetValue("method");
            string _ConstKeyClassName     = cmd.GetValue("kc");
            string _DefaultValueClassName = cmd.GetValue("dc");

            var builder = new CSharpBuilder {
                Namespace = NamespaceName
            };

            builder.AddUsing("System");
            builder.AddUsing("System.Collections.Generic");
            string cname = ClassName;

            if (ctype == ClassType.TieDataContract || ctype == ClassType.JsonDataContract)
            {
                bool isExpression = ctype == ClassType.JsonDataContract;

                string inputPath = cmd.InputPath();
                if (inputPath != null && Path.GetExtension(inputPath).ToLower() == ".json")
                {
                    isExpression = true;
                }

                ConvertJson2CS(code, builder, cname, isExpression);
                return;
            }

            var maker = new ConfigScript(code);

            if ((ctype & ClassType.HierarchicalProperty) == ClassType.HierarchicalProperty)
            {
                maker.HierarchicalMemberType = CodeMemberType.Property;
            }
            else if ((ctype & ClassType.HierarchicalMethod) == ClassType.HierarchicalMethod)
            {
                maker.HierarchicalMemberType = CodeMemberType.Method;
            }
            else
            {
                maker.HierarchicalMemberType = CodeMemberType.Field;
            }

            if (_GetValueMethodName != null)
            {
                maker.GetValueMethodName = _GetValueMethodName;
            }

            if (_ConstKeyClassName != null)
            {
                maker.ConstKeyClassName = _ConstKeyClassName;
            }

            if (_DefaultValueClassName != null)
            {
                maker.DefaultValueClassName = _DefaultValueClassName;
            }

            var clss = maker.Generate(cname);

            builder.AddClass(clss);

            if (ctype == ClassType.ConstKey)
            {
                builder = CreateClass(maker.ConstKeyFields);
            }
            else if (ctype == ClassType.DefaultValue)
            {
                builder = CreateClass(maker.DefaultValueFields);
            }
            else if (ctype == ClassType.StaticField)
            {
                builder = CreateClass(maker.StaticFields);
            }
            else if (ctype == ClassType.StaticPropery)
            {
                builder = CreateClass(maker.StaticProperties);
            }
            else if (ctype == ClassType.StaticMethod)
            {
                builder = CreateClass(maker.StaticMethods);
            }
            else if (ctype == ClassType.HierarchicalField || ctype == ClassType.HierarchicalProperty || ctype == ClassType.HierarchicalMethod)
            {
                //skip, because clss has created class already
            }
            else
            {
                if ((ctype & ClassType.HierarchicalField) != ClassType.HierarchicalField &&
                    (ctype & ClassType.HierarchicalProperty) != ClassType.HierarchicalProperty &&
                    (ctype & ClassType.HierarchicalMethod) != ClassType.HierarchicalMethod
                    )
                {
                    clss.Clear();
                }

                if ((ctype & ClassType.StaticField) == ClassType.StaticField)
                {
                    clss.AddRange(maker.StaticFields);
                }

                if ((ctype & ClassType.StaticPropery) == ClassType.StaticPropery)
                {
                    clss.AddRange(maker.StaticProperties);
                }

                if ((ctype & ClassType.StaticMethod) == ClassType.StaticMethod)
                {
                    clss.AddRange(maker.StaticMethods);
                }

                if ((ctype & ClassType.ConstKey) == ClassType.ConstKey)
                {
                    clss.AddRange(maker.ConstKeyFields);
                }

                if ((ctype & ClassType.DefaultValue) == ClassType.DefaultValue)
                {
                    clss.AddRange(maker.DefaultValueFields);
                }
            }

            builder.AddUsingRange(base.Usings);
            PrintOutput(builder, cname);
        }