92 lines
3.3 KiB
C#
92 lines
3.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
using Amazon.Runtime.Internal.Util;
|
|
|
|
using BCG;
|
|
using BCG.Utilities;
|
|
|
|
using bts;
|
|
|
|
namespace bts.BlueOrb{
|
|
public class LiteralStream : ViewStream {
|
|
|
|
public static List<Tuple<ColumnInfo, object>> parse_columns(string key_value_pairs) {
|
|
List<Tuple<ColumnInfo, object>> rv = new List<Tuple<ColumnInfo, object>>();
|
|
string[] pairs = key_value_pairs.Split(' ');
|
|
foreach (string p in pairs) {
|
|
string[] kvp = p.Split(':');
|
|
string col_name = kvp[0];
|
|
string val = kvp[1];
|
|
var tup = BlueDataUtility.guess_type(val);
|
|
var ci = new ColumnInfo(col_name, rv.Count, tup.Item1);
|
|
rv.Add(Tuple.Create(ci, tup.Item2));
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
//literal("table1", "x:123, y:1.001, z:abc;x:123, y:1.001, z:abc")
|
|
// col_name: col_value ... ; col_name: col_value
|
|
public LiteralStream(string table_name, string rows) {
|
|
Func<string, int, IBlueRow> get_row = (json_row, id) => {
|
|
Dictionary<string, object> map = new Dictionary<string, object>();
|
|
string[] toks = json_row.Split(',', StringSplitOptions.TrimEntries);
|
|
//Logger.assert(col_vals.Length == 2);
|
|
foreach (string t in toks) {
|
|
var col_vals = t.Split(':', StringSplitOptions.TrimEntries);
|
|
string col = col_vals[0];
|
|
string val = col_vals[1];
|
|
map.Add(col, val);
|
|
}
|
|
map["id"] = id;
|
|
return new BlueRow(map);
|
|
};
|
|
string[] row_arr = rows.Split(';', StringSplitOptions.TrimEntries);
|
|
BlueTable table = null;
|
|
foreach (string json_string in row_arr) {
|
|
IBlueRow r = //BlueDataUtility.convert_json_to_row(json_string, table == null ? 0 : table.Count);
|
|
get_row(json_string, table == null ? 0 : table.Count);
|
|
table = table ?? new BlueTable(r.schema);
|
|
table.addRow(r);
|
|
}
|
|
ViewStream str = new BlueTableStream(table);
|
|
str = new SchemaGuesserStream(str);
|
|
mySetRealSource(str);
|
|
}
|
|
|
|
public LiteralStream(string tableName, string schema, string value) {
|
|
string[] columns = schema.Split(' ');
|
|
|
|
|
|
ColumnInfo[] ci = new ColumnInfo[columns.Length + 1];
|
|
ci[0] = new ColumnInfo("id", 0, typeof(int));
|
|
int n = 1;
|
|
foreach (string c in columns) {
|
|
string[] columnAndType = c.Split('/');
|
|
if (columnAndType.Length == 1)
|
|
ci[n] = new ColumnInfo(c, n, typeof(double));
|
|
else
|
|
ci[n] = new ColumnInfo(columnAndType[0], n, Type.GetType("System." + columnAndType[1], true));
|
|
n++;
|
|
}
|
|
|
|
|
|
BlueTable t = new BlueTable(new BlueTableSchema(tableName, ci));
|
|
string[] rows = value.Split(';');
|
|
n = 0;
|
|
foreach (string r in rows)
|
|
try {
|
|
if (r == string.Empty)
|
|
continue;
|
|
t.addRow(
|
|
BlueDataUtility.createRowFromCSVFileRow(r, t.schema, ++n));
|
|
} catch (Exception e) {
|
|
throw (new Exception("Error occured at line: " + n.ToString() + " " + e.Message));
|
|
}
|
|
|
|
mySetRealSource(new BlueRowCollectionStaticStream(t));
|
|
}
|
|
}
|
|
}
|