Пример #1
0
        private static VAL WriteVAL(DataTable dt, JsonStyle style)
        {
            string[] columns = dt.Columns.Cast <DataColumn>().Select(col => col.ColumnName).ToArray();
            VAL      L       = new VAL();

            foreach (DataRow row in dt.Rows)
            {
                VAL V = new VAL();
                for (int i = 0; i < columns.Length; i++)
                {
                    object obj;
                    switch (row[i])
                    {
                    case Guid x:
                        obj = "{" + x.ToString() + "}";
                        break;

                    case DBNull NULL:
                        obj = null;
                        break;

                    default:
                        obj = row[i];
                        break;
                    }

                    V.AddMember(columns[i], obj);
                }
                L.Add(V);
            }

            return(L);
        }
Пример #2
0
        public static string ToJson(DataTable dt)
        {
            //array
            if (dt.Columns.Count == 1)
            {
                //string name = dt.Columns[0].ColumnName;
                string json = VAL.Boxing(dt.ToArray(row => row[0])).ToJson();
                //string.Format("{0}={1}", name, json);
                return json;
            }

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

            return L.ToJson();
        }
Пример #3
0
        public static string WriteJson(this DataSet ds, JsonStyle style)
        {
            VAL val = new VAL();

            foreach (DataTable dt in ds.Tables)
            {
                var _dt = WriteVAL(dt, style);
                val.AddMember(dt.TableName, _dt);
            }

            return(ToJson(style, val));
        }
Пример #4
0
        public static string WriteJson(this DataLake lake, JsonStyle style)
        {
            VAL val = new VAL();

            foreach (var kvp in lake)
            {
                DataSet ds  = kvp.Value;
                VAL     _ds = new VAL();
                foreach (DataTable dt in ds.Tables)
                {
                    var _dt = WriteVAL(dt, style);
                    _ds.AddMember(dt.TableName, _dt);
                }

                val.AddMember(kvp.Key, _ds);
            }

            return(ToJson(style, val));
        }
Пример #5
0
        public static string WriteJson(this DataTable dt, JsonStyle style, bool excludeTableName)
        {
            if (dt.Columns.Count == 1)
            {
                string json = ToJson(style, VAL.Boxing(dt.ToArray(row => row[0])));
                return(json);
            }

            VAL _dt = WriteVAL(dt, style);

            if (excludeTableName)
            {
                return(ToJson(style, _dt));
            }

            VAL val = new VAL();

            val.AddMember(dt.TableName, _dt);
            return(ToJson(style, val));
        }
Пример #6
0
        private void createClass(Class clss, string prefix, string key, VAL val, bool classOnly)
        {
            TypeInfo ty   = null;
            string   path = null;

            if (val.IsAssociativeArray())
            {
                var clss1 = new Class(key)
                {
                    Modifier = Modifier.Public | Modifier.Partial
                };

                builder.AddClass(clss1);

                prefix = MakeVariableName(prefix, key);

                foreach (var member in val.Members)
                {
                    createClass(clss1, prefix, member.Name, member.Value, classOnly: false);
                }

                if (classOnly)
                {
                    return;
                }

                ty = new TypeInfo(key);
            }
            else if (val.IsList)
            {
                Dictionary <string, VAL> dict = new Dictionary <string, VAL>();
                foreach (var item in val)
                {
                    if (item.IsAssociativeArray())
                    {
                        foreach (var member in item.Members)
                        {
                            if (!dict.ContainsKey(member.Name))
                            {
                                dict.Add(member.Name, member.Value);
                            }
                        }
                    }
                }

                VAL _val = new VAL();
                foreach (var kvp in dict)
                {
                    _val.AddMember(kvp.Key, kvp.Value);
                }

                if (dict.Count > 0)
                {
                    //if (key.EndsWith("s"))
                    //    key = key.Substring(0, key.Length - 1);

                    createClass(clss, prefix, key, _val, classOnly: true);

                    ty = new TypeInfo(key)
                    {
                        IsArray = true
                    };

                    path = MakeVariableName(prefix, $"{key}[]");
                }
            }

            if (ty == null)
            {
                Type type = typeof(object);
                if (val.HostValue != null)
                {
                    type = val.HostValue.GetType();
                }

                ty = new TypeInfo(type);
            }

            if (path == null)
            {
                path = MakeVariableName(prefix, key);
            }

            Property prop = createProperty(key, ty, path);

            clss.Add(prop);
        }
Пример #7
0
        private static void _DisplayColumnNodes(ApplicationCommand cmd, TableName tname)
        {
            TableSchema schema = new TableSchema(tname);

            cout.WriteLine("TABLE: {0}", tname.Path);

            bool hasJson = cmd.Has("json");
            VAL  lines   = new VAL();

            int i     = 0;
            int count = 0;
            int h     = 0;

            foreach (IColumn column in schema.Columns)
            {
                if (IsMatch(cmd.wildcard, column.ColumnName))
                {
                    count++;

                    List <string> L = new List <string>();
                    if (column.IsIdentity)
                    {
                        L.Add("++");
                    }
                    if (column.IsPrimary)
                    {
                        L.Add("pk");
                    }

                    string       fk  = string.Empty;
                    ColumnSchema col = column as ColumnSchema;
                    if (col.IsForeignKey)
                    {
                        L.Add("fk");
                        if (cmd.HasForeignKey)
                        {
                            fk = $"-> {col.PK_Schema}.[{col.PK_Table}].[{col.PK_Column}]";
                        }
                    }

                    string keys = string.Join(",", L);

                    if (!hasJson)
                    {
                        cout.WriteLine("{0,5} {1,26} {2,-16} {3,10} {4,10} {5}",
                                       sub(++i),
                                       string.Format("[{0}]", column.ColumnName),
                                       column.GetSQLType(),
                                       keys,
                                       column.Nullable ? "null" : "not null",
                                       fk);
                    }
                    else
                    {
                        VAL line = new VAL();
                        if (keys != "")
                        {
                            line.AddMember("Key", keys);
                        }
                        line.AddMember("Column", column.ColumnName);
                        line.AddMember("Type", column.GetSQLType());
                        if (column.Nullable)
                        {
                            line.AddMember("Null", column.Nullable);
                        }
                        lines.Add(line);
                    }

                    h = PagePause(cmd, ++h);
                }
            }

            if (!hasJson)
            {
                cout.WriteLine("\t{0} Column(s)", count);
            }
            else
            {
                cout.WriteLine(lines.ToJson());
            }
        }
Пример #8
0
        static VAL functions(string func, VAL parameters, Memory DS)
        {
            int size = parameters.Size;
            VAL L0   = size > 0 ? parameters[0] : null;
            VAL L1   = size > 1 ? parameters[1] : null;
            VAL L2   = size > 2 ? parameters[2] : null;


            switch (func)
            {
            //run("command");
            case "run":
            {
                string line = null;
                if (size == 1 && L0.VALTYPE == VALTYPE.stringcon)
                {
                    line = L0.Str;
                }
                else
                {
                    cerr.WriteLine("invalid arguments on function void run(string)");
                }

                if (line != null)
                {
                    IShell shell = DS[SHELL].Value as IShell;
                    if (shell != null)
                    {
                        int result = (int)shell.Run(line);
                        return(new VAL(result));
                    }
                    else
                    {
                        cerr.WriteLine("shell not found");
                        return(new VAL());
                    }
                }
            }
            break;

            default:
                var query = DS[func];
                if (query.VALTYPE == VALTYPE.stringcon)
                {
                    VAL val = VAL.Array(0);
                    for (int i = 0; i < parameters.Size; i++)
                    {
                        VAL    parameter = parameters[i];
                        string name      = parameter.GetName();

                        if (name == null)
                        {
                            cout.WriteLine("require parameter name at arguments({0}), run func(id=20,x=2);", i + 1);
                            return(new VAL(2));
                        }
                        val.AddMember(name, parameter);
                    }

                    VAL result = VAL.Array(0);
                    result.Add(query);
                    result.Add(val);
                    return(result);
                }
                break;
            }

            return(null);
        }