/// <summary> /// Reads a table and inject it as a tuple set in OPL. /// </summary> /// <param name="name">The name of the Data Set</param> /// <param name="reader">The SqlDataReader to read from</param> private void ReadTupleSet(String name, DbDataReader reader) { OplDataHandler handler = DataHandler; OplElement element = handler.getElement(name); ITupleSchema schema = element.AsTupleSet().Schema; int size = schema.Size; String[] oplFieldsName = new String[size]; OplElementDefinitionType.Type[] oplFieldsType = new OplElementDefinitionType.Type[size]; this.FillNamesAndTypes(schema, oplFieldsName, oplFieldsType); handler.StartElement(name); handler.StartSet(); while (reader.Read()) { handler.StartTuple(); for (int column = 0; column < oplFieldsName.Length; column++) { String columnName = oplFieldsName[column]; HandleColumnValue(handler, oplFieldsType[column], reader[columnName]); } handler.EndTuple(); } handler.EndSet(); }
public override void CustomRead() { OplDataHandler handler = DataHandler; // Get the element of the predefined name. // If no such element is defined then exit early. // The element must be either a tuple or a set of tuples. // Any custom load specification specified by the element is then processed. OplElement spec = handler.getElement(ELEM_NAME); if (spec == null) { return; } if (spec.ElementType == OplElementType.Type.TUPLE) { loadSpec(handler, spec.AsTuple()); } else if (spec.ElementType == OplElementType.Type.SET_TUPLE) { foreach (ITuple tuple in spec.AsTupleSet()) { loadSpec(handler, tuple); } } else { throw new NotImplementedException(ELEM_NAME + " must be (set of) tuple"); } }
/// <summary> /// Writes the specified data set as the specified target table. /// </summary> /// <param name="name">The name of the OPL data set</param> /// <param name="targetTable">The target table</param> public void WriteTable(DbConnection con, string name, String targetTable) { // The opl element to write OplElement element = this.Model.GetElement(name); ITupleSet tupleSet = element.AsTupleSet(); ITupleSchema tupleSchema = tupleSet.Schema; // drop existing table DropTable(con, targetTable); // Create table String[] tableDef = TableDefinitionFromOplTupleSchema(tupleSchema, true); CreateTable(con, tableDef, targetTable); // Populate table String insertCommand = CreateInsertCommand(tupleSchema, targetTable); foreach (ITuple t in tupleSet) { InsertIntoTable(con, tupleSchema, t, insertCommand); } }
/// <summary> /// Process one load specification. /// </summary> /// <param name="handler">The data loader that constructs OPL elements.</param> /// <param name="tuple">The load specification.</param> private void loadSpec(OplDataHandler handler, ITuple tuple) { // Get the connection string // In the SimpleData implementation we don't use that string. // If you create a data source that is backed up by a file or // by a database, then this string can be used to specify // locations and/or credentials. string connection = tuple.GetStringValue(CONN_FIELD); connection = Interpolate(connection, handler); string connectionType = tryLoadField(tuple, TYPE_FIELD, "SimpleData"); connectionType = Interpolate(connectionType, handler); IDataProvider provider = null; if (connectionType.Equals("SimpleData")) { provider = new SimpleData(connection); } else if (connectionType.Equals("SQLite")) { provider = new SQLiteData(connection); } else { provider = new GenericData(connectionType, connection); } // Process each of load specifications and load the respective // element. using (provider) { ISymbolSet data = tuple.GetSymbolSetValue(DATA_FIELD); IEnumerator e = data.GetEnumerator(); while (e.MoveNext()) { // Split specification into element name and // initialiation string (statement). string s = Interpolate(e.Current.ToString().Trim(), handler); int eq = s.IndexOf('='); string name = s.Substring(0, eq).Trim(); string stmt = s.Substring(eq + 1).Trim(); // Inspect the type of the element and populate it. OplElement elem = handler.getElement(name); OplElementType.Type type = elem.ElementType; using (IEnumerator <IRow> rows = provider.getRows(stmt)) { // (collections of) integers if (type == OplElementType.Type.INT) { loadPrimitive(handler, name, rows, SET_INT); } else if (type == OplElementType.Type.SET_INT) { loadPrimitiveCollection(handler, name, rows, START_SET, END_SET, SET_INT); } else if (type == OplElementType.Type.MAP_INT) { loadPrimitiveCollection(handler, name, rows, START_ARRAY, END_ARRAY, SET_INT); } // (collections of) floating point values else if (type == OplElementType.Type.NUM) { loadPrimitive(handler, name, rows, SET_FLOAT); } else if (type == OplElementType.Type.SET_NUM) { loadPrimitiveCollection(handler, name, rows, START_SET, END_SET, SET_FLOAT); } else if (type == OplElementType.Type.MAP_NUM) { loadPrimitiveCollection(handler, name, rows, START_ARRAY, END_ARRAY, SET_FLOAT); } // (collections of) tuples else if (type == OplElementType.Type.STRING) { loadPrimitive(handler, name, rows, SET_STRING); } else if (type == OplElementType.Type.SET_SYMBOL) { loadPrimitiveCollection(handler, name, rows, START_SET, END_SET, SET_STRING); } else if (type == OplElementType.Type.MAP_SYMBOL) { loadPrimitiveCollection(handler, name, rows, START_ARRAY, END_ARRAY, SET_STRING); } else if (type == OplElementType.Type.TUPLE) { loadTuple(handler, name, rows); } else if (type == OplElementType.Type.SET_TUPLE) { loadTupleCollection(handler, name, rows, elem.AsTupleSet().Schema, START_SET, END_SET); } else if (type == OplElementType.Type.MAP_TUPLE) { loadTupleCollection(handler, name, rows, elem.AsTupleMap().Schema, START_ARRAY, END_ARRAY); } else { throw new NotImplementedException("element type " + type + " not implemented"); } } } } }