Download OPL Language Reference Manual

Transcript
In OPL 4 and later
To do the same in OPL 4.x (where you write database instructions in data files),
you can define literal strings inside the model file (which will be compiled) and
use them in the data file, like this:
In the .mod file:
string connectionString = "scott/tiger@TEST";
string myQuery = "select id from table";
{int} setOfInt = ...;
dvar int X in 1..5;
minimize X;
subject to {
forall (i in setOfInt)
X >= i;
};
In the .dat file:
DBconnection db("oracle9", connectionString);
setOfInt from DBread (db, myQuery);
Writing to a database
Explains the process of writing to a database from OPL.
Publishing results to a database is similar to parameterized data initialization. Here
is an example extracted from the oil code sample:
All database publishing requests are carried out during postprocessing, if a
solution is available. Such requests are processed in the order declared in the .dat
file(s). If your RDMBS supports transactions, every single publishing request is
sent within its own transaction.
Adding rows
1. Write in the model file:
tuple result {
string oil;
string gas;
float blend;
float a;
}
{result} Result =
{ <o,g,Blend[o][g],a[g]> | o in Oils, g in Gasolines };
2. Write in the data file:
DBExecute(db,"drop table Result");
DBExecute(db,"create table Result(oil varchar(10), gas varchar(10), blend real, a real)");
Result to DBUpdate(db,"INSERT INTO Result(oil,gas,blend,a) VALUES(?,?,?,?)");
In this example, you use:
v a DBExecute statement to send SQL DDL (data definition language) instructions
to the Relational Database Management Server (RDBMS)
v a DBUpdate statement to modify the data (see “Updating existing rows” on page
28).
Chapter 2. OPL, the modeling language
27