Skip to content

Commit 6d5398b

Browse files
authored
Merge pull request #12 from djunglas/master
Support username/password and slightly improve write performance.
2 parents 39343e9 + c50b9aa commit 6d5398b

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

lib/jdbc-custom-data-source.jar

2.54 KB
Binary file not shown.

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ public class JdbcConfiguration {
3030
private String _url = null;
3131
private String _user = null;
3232
private String _password = null;
33+
34+
/** Resolve <code>s</code> using environment variables.
35+
* If <code>s</code> starts with "$" and is the name of an existing
36+
* environment variable then return the value of that variable, otherwise
37+
* return <code>s</code>.
38+
* @param s The string to resolve.
39+
* @return The value of environment variable <code>s</code> if such a
40+
* variable exists, <code>s</code> otherwise.
41+
*/
42+
private static String resolveString(String s) {
43+
if ( s == null || s.length() < 2 || s.charAt(0) != '$' )
44+
return s;
45+
final String value = System.getenv(s.substring(1));
46+
if ( value != null )
47+
return value;
48+
return s;
49+
}
3350

3451
/**
3552
* Creates a new JDBC configuration.
@@ -46,11 +63,19 @@ public void setUrl(String url) {
4663
}
4764

4865
public String getUser() {
49-
return _user;
66+
return resolveString(_user);
67+
}
68+
69+
public void setUser(String user) {
70+
_user = user;
5071
}
5172

5273
public String getPassword() {
53-
return _password;
74+
return resolveString(_password);
75+
}
76+
77+
public void setPassword(String password) {
78+
_password = password;
5479
}
5580

5681
public Properties getReadQueries() {

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,14 @@ String getInsertQuery(IloTupleSchema schema, String table) {
102102
return query;
103103
}
104104

105-
void updateValues(IloTuple tuple, IloTupleSchema schema,
106-
IloOplTupleSchemaDefinition tupleSchemaDef, PreparedStatement stmt) throws SQLException {
107-
for (int i = 0; i < schema.getSize(); i++) {
105+
void updateValues(IloTuple tuple, PreparedStatement stmt, Type[] columnType) throws SQLException {
106+
for (int i = 0; i < columnType.length; i++) {
108107
int index = i+1; // index in PreparedStatement
109-
Type columnType = tupleSchemaDef.getComponent(i).getElementDefinitionType();
110-
if (columnType == Type.INTEGER)
108+
if (columnType[i] == Type.INTEGER)
111109
stmt.setInt(index, tuple.getIntValue(i));
112-
else if (columnType == Type.FLOAT)
110+
else if (columnType[i] == Type.FLOAT)
113111
stmt.setDouble(index, tuple.getNumValue(i));
114-
else if (columnType == Type.STRING)
112+
else if (columnType[i] == Type.STRING)
115113
stmt.setString(index, tuple.getStringValue(i));
116114
}
117115
}
@@ -149,14 +147,17 @@ void customWrite(String name, String table) {
149147
try {
150148
IloOplElementDefinition tupleDef = _def.getElementDefinition(schema.getName());
151149
IloOplTupleSchemaDefinition tupleSchemaDef = tupleDef.asTupleSchema();
150+
final Type[] columnType = new Type[schema.getSize()];
151+
for (int i = 0; i < columnType.length; ++i)
152+
columnType[i] = tupleSchemaDef.getComponent(i).getElementDefinitionType();
152153

153154
conn.setAutoCommit(false); // begin transaction
154155
String psql = getInsertQuery(schema, table);
155156
PreparedStatement pstmt = conn.prepareStatement(psql);
156157
// iterate the set and create the final insert statement
157158
for (java.util.Iterator it1 = tupleSet.iterator(); it1.hasNext();) {
158159
IloTuple tuple = (IloTuple) it1.next();
159-
updateValues(tuple, schema, tupleSchemaDef, pstmt);
160+
updateValues(tuple, pstmt, columnType);
160161
pstmt.executeUpdate();
161162
}
162163
conn.commit();
@@ -175,4 +176,4 @@ void customWrite(String name, String table) {
175176
}
176177
}
177178
}
178-
}
179+
}

0 commit comments

Comments
 (0)