114 lines
3.6 KiB
C#
114 lines
3.6 KiB
C#
namespace bts
|
|
{
|
|
using BCG;
|
|
using BCG.Utilities.General;
|
|
|
|
public class TableMetaDataStream : ViewStream, IWorker
|
|
{
|
|
private class TableState
|
|
{
|
|
public ViewStream table_stream;
|
|
public IBlueTableSchema schema = null;
|
|
public bool processed = false;
|
|
public TableState(ViewStream table_stream_)
|
|
{
|
|
table_stream = table_stream_;
|
|
table_stream.onChange += _handle_table_update;
|
|
}
|
|
private void _handle_table_update(object dp_obj)
|
|
{
|
|
DataPacket dp = dp_obj as DataPacket;
|
|
schema = dp.schema;
|
|
}
|
|
public void unsub()
|
|
{
|
|
table_stream.onChange -= _handle_table_update;
|
|
}
|
|
}
|
|
private IBlueTableSchema _schema = BlueDataUtility.form_schema("TableMetaData",
|
|
Tuple.Create("id", TypeCode.Int32),
|
|
Tuple.Create("table_name", TypeCode.String),
|
|
Tuple.Create("col_idx", TypeCode.Int32),
|
|
Tuple.Create("col_type_code", TypeCode.Int32),
|
|
Tuple.Create("col_name", TypeCode.String));
|
|
|
|
private ViewStreamDatabase _vsd;
|
|
private ViewStream _base_stream;
|
|
private IDictionary<string, TableState> _table_map = new Dictionary<string, TableState>();
|
|
private BlueTable _state;
|
|
public TableMetaDataStream()
|
|
: this(ViewStreamDatabase.instance) { }
|
|
public TableMetaDataStream(ViewStreamDatabase vsd)
|
|
{
|
|
_vsd = vsd;
|
|
_base_stream = new PubSubServerStateStream(_vsd);
|
|
_state = new BlueTable(_schema);
|
|
}
|
|
protected override void myOnFirstSubscriber()
|
|
{
|
|
_base_stream.onChange += _handle_base_stream;
|
|
_vsd.addWorker(this);
|
|
}
|
|
|
|
protected override void myOnLastSubscriber()
|
|
{
|
|
_base_stream.onChange -= _handle_base_stream;
|
|
_vsd.removeWorker(this);
|
|
}
|
|
|
|
public void doWork()
|
|
{
|
|
bool changed = false;
|
|
foreach (TableState ts in _table_map.Values)
|
|
{
|
|
if (!ts.processed && ts.schema != null)
|
|
{
|
|
foreach (ColumnInfo ci in ts.schema.columnInfo)
|
|
{
|
|
int id = _state.Count;
|
|
var table_name = ts.schema.tableName;
|
|
var col_idx = ci.colNo;
|
|
var col_name = ci.colName;
|
|
TypeCode type_code = BlueDataUtility.get_typecode(ci.colType);
|
|
BlueRow nr = new BlueRow(_schema, id, table_name, col_idx, Convert.ToInt32(type_code),
|
|
col_name);
|
|
changed = true;
|
|
_state.addRow(nr);
|
|
}
|
|
ts.processed = true;
|
|
ts.unsub();
|
|
}
|
|
}
|
|
if (changed)
|
|
{
|
|
var dp_out = BlueDataUtility.build_dp_inc(_state, myLastValue as DataPacket);
|
|
myFireEvent(dp_out);
|
|
}
|
|
}
|
|
|
|
private void _handle_base_stream(object dp_obj)
|
|
{
|
|
DataPacket dp = dp_obj as DataPacket;
|
|
foreach (IBlueRow r in dp.newRows)
|
|
{
|
|
string tag = r.get<string>("tag");
|
|
if (tag.StartsWith("DS_"))
|
|
{
|
|
_handle_table(tag);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void _handle_table(string tag_name)
|
|
{
|
|
Log.assert(tag_name.StartsWith("DS_"));
|
|
string table_name = tag_name.Remove(0, 3);
|
|
TableState ts = null;
|
|
if (!_table_map.TryGetValue(table_name, out ts))
|
|
{
|
|
ts = new TableState(new TableStream(table_name, _vsd));
|
|
_table_map.Add(table_name, ts);
|
|
}
|
|
}
|
|
}
|
|
} |