Skip to content

Commit 52df1a0

Browse files
committed
merges
2 parents 2612dbd + 9ba6f9d commit 52df1a0

File tree

11 files changed

+187
-20
lines changed

11 files changed

+187
-20
lines changed

examples/studio_integration/demo.dat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ prepare {
2121
db.execute("CREATE TABLE result(oil VARCHAR(30), gas VARCHAR(30), blend FLOAT, a FLOAT)");
2222

2323
// write results to database
24-
db.update("items", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)");
24+
db.update("Result", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)");
2525
}
2626

2727
MaxProduction = 14000;

lib/jdbc-custom-data-source.jar

252 Bytes
Binary file not shown.

src/main/java/com/ibm/opl/customdatasource/JdbcConfiguration.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public OutputParameters(boolean autodrop,
4242
private final static String URL = "url";
4343
private final static String USER = "user";
4444
private final static String PASSWORD = "password";
45-
45+
private final static String IS_MAPPING_NAME = "is_mapping_name";
46+
4647
private final static String READ = "read";
4748
private final static String WRITE = "write";
4849

@@ -54,6 +55,7 @@ public OutputParameters(boolean autodrop,
5455
private String _url = null;
5556
private String _user = null;
5657
private String _password = null;
58+
private boolean _is_mapping_name = false;
5759

5860
/** Resolve <code>s</code> using environment variables.
5961
* If <code>s</code> starts with "$" and is the name of an existing
@@ -78,6 +80,14 @@ private static String resolveString(String s) {
7880
public JdbcConfiguration() {
7981
}
8082

83+
public boolean isMappingName() {
84+
return _is_mapping_name;
85+
}
86+
public void setMappingName(boolean value)
87+
{
88+
_is_mapping_name = value;
89+
}
90+
8191
public String getUrl() {
8292
return _url;
8393
}
@@ -205,6 +215,11 @@ public void readProperties(InputStream input) throws IOException {
205215
this._url = properties.getProperty(URL);
206216
this._user = properties.getProperty(USER);
207217
this._password = properties.getProperty(PASSWORD);
218+
String v = properties.getProperty(IS_MAPPING_NAME);
219+
if (v != null) {
220+
this._is_mapping_name = Boolean.valueOf(v);
221+
}
222+
208223

209224
// iterate properties to find read and write
210225
Enumeration<?> propertyNames = properties.propertyNames();
@@ -277,6 +292,12 @@ public void readXML(InputStream input) throws IOException {
277292
_user = doc.getElementsByTagName(USER).item(0).getTextContent();
278293
_password = doc.getElementsByTagName(PASSWORD).item(0).getTextContent();
279294

295+
NodeList nl = doc.getElementsByTagName(IS_MAPPING_NAME);
296+
if (nl.getLength() > 0) {
297+
String v = nl.item(0).getTextContent();
298+
_is_mapping_name = Boolean.valueOf(v);
299+
}
300+
280301
// input parameters
281302
Node readNode = doc.getElementsByTagName(READ).item(0);
282303
if (readNode != null && readNode.getNodeType() == Node.ELEMENT_NODE) {

src/main/java/com/ibm/opl/customdatasource/JdbcCustomDataSource.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ public void readTupleSet(String name, String query) throws SQLException {
349349
Type[] oplFieldsType = new Type[size];
350350

351351
fillNamesAndTypes(schema, oplFieldsName, oplFieldsType);
352+
353+
boolean is_mapping_name = _configuration.isMappingName();
352354

353355
final RunQuery q = new RunQuery(query);
354356
try {
@@ -359,13 +361,24 @@ public void readTupleSet(String name, String query) throws SQLException {
359361
while (rs.next()) {
360362
handler.startTuple();
361363
for (int column = 0; column < oplFieldsName.length; column++) {
362-
String columnName = oplFieldsName[column];
363-
if (oplFieldsType[column] == Type.INTEGER) {
364-
handler.addIntItem(rs.getInt(columnName));
365-
} else if (oplFieldsType[column] == Type.FLOAT) {
366-
handler.addNumItem(rs.getDouble(columnName));
367-
} else if (oplFieldsType[column] == Type.STRING) {
368-
handler.addStringItem(rs.getString(columnName));
364+
if (is_mapping_name) {
365+
String columnName = oplFieldsName[column];
366+
if (oplFieldsType[column] == Type.INTEGER) {
367+
handler.addIntItem(rs.getInt(columnName));
368+
} else if (oplFieldsType[column] == Type.FLOAT) {
369+
handler.addNumItem(rs.getDouble(columnName));
370+
} else if (oplFieldsType[column] == Type.STRING) {
371+
handler.addStringItem(rs.getString(columnName));
372+
}
373+
} else {
374+
int sql_index = column+1;
375+
if (oplFieldsType[column] == Type.INTEGER) {
376+
handler.addIntItem(rs.getInt(sql_index));
377+
} else if (oplFieldsType[column] == Type.FLOAT) {
378+
handler.addNumItem(rs.getDouble(sql_index));
379+
} else if (oplFieldsType[column] == Type.STRING) {
380+
handler.addStringItem(rs.getString(sql_index));
381+
}
369382
}
370383
}
371384
handler.endTuple();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Specification-Title: JDBC custom data source for OPL
2-
Specification-Version: 1.0
2+
Specification-Version: 1.1
33
Specification-Vendor: IBM

src/test/java/com/ibm/opl/customdatasource/JavaApiTest.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.InputStream;
77
import java.sql.Connection;
88
import java.sql.DriverManager;
9+
import java.sql.SQLException;
910
import java.sql.Statement;
1011

1112
import static org.junit.Assert.*;
@@ -25,6 +26,9 @@ public class JavaApiTest {
2526
public static String OIL_DAT="models/oil.dat";
2627
public static String OIL_MOD="models/oil.mod";
2728
public static String CONFIG_RESOURCE = "models/oil_sqlite.xml";
29+
public static String CONFIG_RESOURCE_NAME_MAPPING_OK = "models/oil_sqlite_name_mapping_ok_select.xml";
30+
public static String CONFIG_RESOURCE_NAME_MAPPING_WRONG = "models/oil_sqlite_name_mapping_wrong_select.xml";
31+
public static String CONFIG_RESOURCE_NO_NAME_MAPPING = "models/oil_sqlite_no_name_mapping.xml";
2832

2933
public static String CREATE_OIL_MOD="models/oil_create_db.mod";
3034
public static String CREATE_CONFIG_RESOURCE = "models/oil_create_db.xml";
@@ -121,16 +125,40 @@ public final void testApiCall() {
121125
}
122126

123127
// use a .mod to create tmp database
128+
// The tmp database is created with wrong column names
129+
// (name, demand, price) are replaced by (nom, demande, prix)
124130
String createModFilename = new File(getClass().getResource(CREATE_OIL_MOD).getFile()).getAbsolutePath();
125131
String createJdbcConfigurationFile = new File(getClass().getResource(CREATE_CONFIG_RESOURCE).getFile()).getAbsolutePath();
126132
runMod(createModFilename, null, createJdbcConfigurationFile, connectionString);
127133

128-
// now solve the oil model
129-
String modFilename = new File(getClass().getResource(OIL_MOD).getFile()).getAbsolutePath();
130-
String[] datFilenames = {new File(getClass().getResource(OIL_DAT).getFile()).getAbsolutePath()};
131-
String jdbcConfigurationFile = new File(getClass().getResource(CONFIG_RESOURCE).getFile()).getAbsolutePath();
132-
runMod(modFilename, datFilenames, jdbcConfigurationFile, connectionString);
133-
134+
// now solve the oil model => we want an error here
135+
// in that particular test,that should be a runtime exception
136+
try {
137+
String modFilename = new File(getClass().getResource(OIL_MOD).getFile()).getAbsolutePath();
138+
String[] datFilenames = {new File(getClass().getResource(OIL_DAT).getFile()).getAbsolutePath()};
139+
String jdbcConfigurationFile = new File(getClass().getResource(CONFIG_RESOURCE_NAME_MAPPING_WRONG).getFile()).getAbsolutePath();
140+
runMod(modFilename, datFilenames, jdbcConfigurationFile, connectionString);
141+
fail("We should have encountered a SQLException: no such column");
142+
} catch (RuntimeException re) {
143+
Throwable cause = re.getCause();
144+
assertTrue(cause instanceof SQLException);
145+
assertTrue(cause.getMessage().contains("no such column"));
146+
}
147+
148+
// resolve, using the right name mapping
149+
{
150+
String modFilename = new File(getClass().getResource(OIL_MOD).getFile()).getAbsolutePath();
151+
String[] datFilenames = {new File(getClass().getResource(OIL_DAT).getFile()).getAbsolutePath()};
152+
String jdbcConfigurationFile = new File(getClass().getResource(CONFIG_RESOURCE_NAME_MAPPING_OK).getFile()).getAbsolutePath();
153+
runMod(modFilename, datFilenames, jdbcConfigurationFile, connectionString);
154+
}
155+
// resolve, without name mapping
156+
{
157+
String modFilename = new File(getClass().getResource(OIL_MOD).getFile()).getAbsolutePath();
158+
String[] datFilenames = {new File(getClass().getResource(OIL_DAT).getFile()).getAbsolutePath()};
159+
String jdbcConfigurationFile = new File(getClass().getResource(CONFIG_RESOURCE_NO_NAME_MAPPING).getFile()).getAbsolutePath();
160+
runMod(modFilename, datFilenames, jdbcConfigurationFile, connectionString);
161+
}
134162
} catch (IloOplException ex) {
135163
ex.printStackTrace();
136164
fail("### OPL exception: " + ex.getMessage());

src/test/resources/com/ibm/opl/customdatasource/models/oil_create_db.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212

1313
tuple gasType {
14-
string name;
15-
float demand;
16-
float price;
14+
string nom;
15+
float demande;
16+
float prix;
1717
float octane;
1818
float lead;
1919
}

src/test/resources/com/ibm/opl/customdatasource/models/oil_sqlite.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
The name attribute is used to populate the corresponding Data Element.
1414
-->
1515
<read>
16-
<query name="Gasolines">SELECT NAME FROM GASDATA</query>
16+
<query name="Gasolines">SELECT NOM FROM GASDATA</query>
1717
<query name="Oils">SELECT NAME FROM OILDATA</query>
1818
<query name="GasData">SELECT * FROM GASDATA</query>
1919
<query name="OilData">SELECT * FROM OILDATA</query>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<datasource>
2+
<!-- The connection string
3+
The default url connects to mysql on default port, using database
4+
'custom_data_source'
5+
-->
6+
<url>jdbc:sqlite:memory</url>
7+
8+
<!-- Your connection credentials -->
9+
<user></user>
10+
<password></password>
11+
12+
<!-- When name mapping is activated, the mapping from db resultset to tuples is done on the name of the tuple members.
13+
With this file, it should fail (no mapping done on the read section)
14+
-->
15+
<is_mapping_name>true</is_mapping_name>
16+
17+
<!-- The read queries
18+
The name attribute is used to populate the corresponding Data Element.
19+
-->
20+
<read>
21+
<query name="Gasolines">SELECT NOM FROM GASDATA</query>
22+
<query name="Oils">SELECT NAME FROM OILDATA</query>
23+
<query name="GasData">SELECT nom AS NAME, DEMANDE as DEMAND, PRIX as PRICE, OCTANE, LEAD FROM GASDATA</query>
24+
<query name="OilData">SELECT * FROM OILDATA</query>
25+
</read>
26+
27+
<!-- The output table mapping.
28+
This mapping define how output data sets are exported to the database.
29+
-->
30+
<write>
31+
<!-- This maps the output dataset "Result" to the "result" table -->
32+
<table name="Result" target="result"/>
33+
</write>
34+
</datasource>
35+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<datasource>
2+
<!-- The connection string
3+
The default url connects to mysql on default port, using database
4+
'custom_data_source'
5+
-->
6+
<url>jdbc:sqlite:memory</url>
7+
8+
<!-- Your connection credentials -->
9+
<user></user>
10+
<password></password>
11+
12+
<!-- When name mapping is activated, the mapping from db resultset to tuples is done on the name of the tuple members.
13+
With this file, it should work (see field mapping for the GASDATA tupleset)
14+
-->
15+
<is_mapping_name>true</is_mapping_name>
16+
17+
<!-- The read queries
18+
The name attribute is used to populate the corresponding Data Element.
19+
-->
20+
<read>
21+
<query name="Gasolines">SELECT NOM FROM GASDATA</query>
22+
<query name="Oils">SELECT NAME FROM OILDATA</query>
23+
<query name="GasData">SELECT * FROM GASDATA</query>
24+
<query name="OilData">SELECT * FROM OILDATA</query>
25+
</read>
26+
27+
<!-- The output table mapping.
28+
This mapping define how output data sets are exported to the database.
29+
-->
30+
<write>
31+
<!-- This maps the output dataset "Result" to the "result" table -->
32+
<table name="Result" target="result"/>
33+
</write>
34+
</datasource>
35+

0 commit comments

Comments
 (0)