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); }
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(); }
private static VAL Host2Valor(object host, VAL val) { if (host == null || host is System.DBNull) { val = new VAL(); } else if (host is string || host is char || host is byte || host is int || host is short || host is long || host is bool || host is double || host is float || host is decimal || host is DateTime) { val = VAL.Boxing1(host); } else if (host is IValizable) { val = ((IValizable)host).GetValData(); } else if (host is Type) { val = VAL.Script(string.Format("typeof({0})", ((Type)host).FullName)); } else if (host.GetType().IsEnum) { val = VAL.Script(HostOperation.EnumBitFlags(host)); } else if (host is ICollection) { val = VAL.Array(); foreach (object a in (ICollection)host) { val.Add(Host2Valor(a, new VAL())); } } else { VAL temp = ValizerScript.ToValor(host); if ((object)temp != null) { return(temp); } FieldInfo[] fields = host.GetType().GetFields(); foreach (FieldInfo fieldInfo in fields) { Attribute[] A = (Attribute[])fieldInfo.GetCustomAttributes(typeof(NonValizedAttribute), true); if (A.Length != 0) { continue; } object fieldValue = fieldInfo.GetValue(host); VAL persistent = ValizerScript.ToValor(fieldInfo, fieldValue); if ((object)persistent == null) { persistent = VAL.Boxing(fieldValue); if (!fieldInfo.FieldType.IsValueType && persistent.IsHostType) { persistent = Host2Valor(fieldValue, new VAL()); } } val[fieldInfo.Name] = persistent; } PropertyInfo[] properties = host.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { ValizableAttribute[] attributes = (ValizableAttribute[])propertyInfo.GetCustomAttributes(typeof(ValizableAttribute), true); if (attributes.Length == 0 || !propertyInfo.CanRead) { continue; } object propertyValue = propertyInfo.GetValue(host, null); if (propertyValue == null) { continue; } VAL persistent = ValizerScript.ToValor(propertyInfo, propertyValue); if ((object)persistent == null) { if (propertyValue is ICollection) { ICollection collection = (ICollection)propertyValue; persistent = VAL.Array(); foreach (object obj in collection) { persistent.Add(VAL.Boxing(obj)); } } else { persistent = VAL.Boxing(propertyValue); if (!propertyInfo.PropertyType.IsValueType && persistent.IsHostType) { persistent = Host2Valor(propertyValue, new VAL()); } } } val[propertyInfo.Name] = persistent; } } val.Class = host.GetType().FullName; return(val); }
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()); } }
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); }