Download IBM ILOG OPL Language Reference Manual

Transcript
Reading from a database
In OPL, database relations can be read into sets or arrays. For instance, these instructions
from the model file:
tuple gasType {
string name;
float demand;
float price;
float octane;
float lead;
}
tuple oilType {
string name;
float capacity;
float price;
float octane;
float lead;
}
And these instructions from the data file:
GasData from DBRead(db,"SELECT * FROM GasData");
OilData from DBRead(db,"SELECT * FROM OilData");
Together illustrate how to initialize a set of tuples from the relation OilData in the database
db. In this example, the DBRead instruction inserts an element into the set for each tuple of
the relations.
Important conventions adopted by OPL:
1. If read into a set, the resulting set must be a set of integers, floats, or strings, or a set
of tuples whose elements are integers, floats, or strings.
2. If read into an array, the resulting array must be an array of integers, floats, or strings,
or an array of tuples whose elements are integers, floats, or strings.
3. In the case of tuples, the columns of the SQL query result are mapped by position to
the field of the OPL tuples. For instance, in the above query, the column name has been
mapped to the field name and so on.
4. When initializing an array with a DBRead statement, the indexing set and array cells
are initialized at the same time.
Note: OPL does not parse the query; it simply sends the string to the database system that
has full responsibility for handling it. As a consequence, the syntax and the semantics
of these queries are outside the scope of this book and users should consult the
appropriate database manual for more information.
It is also possible to implement parameterized queries in OPL, for example:
I B M
I L O G
O P L
L A N G U A G E
R E F E R E N C E
M A N U A L
55