Skip to content

Commit d6bd942

Browse files
committed
Added NamedParametersPreparedStatement
1 parent c0710c8 commit d6bd942

File tree

9 files changed

+473
-53
lines changed

9 files changed

+473
-53
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ This example will work with any 12.x OPL version, even if it is configured to ru
1616
## Table of Contents
1717
- [Prerequisites](#prerequisites)
1818
- [Build and run the sample from java](#build-and-run-the-sample-from-java)
19-
- [Build the sample](#build-the-sample)
20-
- [Run the sample from OPL](#run-the-sample-from-opl)
19+
- [Run the sample from OPL](#run-the-sample-from-opl)
20+
- [Reusing the sample with other databases](#reusing-the-sample-with-other-databases)
2121
- [Export plain dat files](#export-plain-dat-files)
22-
- [Run with a previous OPL version](#run-with-a-previous-opl-version)
22+
- [Run with another OPL version](#run-with-another-opl-version)
2323
- [License](#license)
2424

2525
### Prerequisites
@@ -59,6 +59,7 @@ using a jdbc-custom-data-source from `oplrun` or OPL Studio.
5959

6060

6161
### Reusing the sample with other databases
62+
6263
As the sample is build on JDBC, it's possible to reuse <code>JdbcCustomDataSource</code> with minimal changes:
6364

6465
* Add your JDBC driver in your classpath

examples/embedded_in_dat/oil.dat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ prepare {
1616
// Now create JdbcConfiguration
1717
var config = IloOplCallJava("com.ibm.opl.customdatasource.JdbcConfiguration", "<init>", "");
1818
config.setUrl(connUrl);
19+
config.setUser(userName);
20+
config.setPassword(password);
1921

2022
config.addReadQuery("Gasolines", "SELECT NAME FROM GASDATA");
2123
config.addReadQuery("Oils", "SELECT NAME FROM OILDATA");

lib/jdbc-custom-data-source.jar

3.13 KB
Binary file not shown.

pom.xml

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<project xmlns="http://maven.apache.org/POM/4.0.0"
32
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
43
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
@@ -64,53 +63,4 @@
6463
</plugin>
6564
</plugins>
6665
</build>
67-
68-
<profiles>
69-
<profile>
70-
<id>binary.windows</id>
71-
<activation>
72-
<os>
73-
<family>windows</family>
74-
</os>
75-
</activation>
76-
<properties>
77-
<binary.group>x64_windows</binary.group>
78-
</properties>
79-
</profile>
80-
<profile>
81-
<id>binary.linux</id>
82-
<activation>
83-
<os>
84-
<name>Linux</name>
85-
<arch>amd64</arch>
86-
</os>
87-
</activation>
88-
<properties>
89-
<binary.group>x86-64_linux</binary.group>
90-
</properties>
91-
</profile>
92-
<profile>
93-
<id>binary.plinux</id>
94-
<activation>
95-
<os>
96-
<name>Linux</name>
97-
<arch>ppc64le</arch>
98-
</os>
99-
</activation>
100-
<properties>
101-
<binary.group>ppc64le_linux</binary.group>
102-
</properties>
103-
</profile>
104-
<profile>
105-
<id>binary.osx</id>
106-
<activation>
107-
<os>
108-
<name>Mac</name>
109-
</os>
110-
</activation>
111-
<properties>
112-
<binary.group>x86-64_osx</binary.group>
113-
</properties>
114-
</profile>
115-
</profiles>
11666
</project>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ public void setUrl(String url) {
4848
public String getUser() {
4949
return _user;
5050
}
51+
52+
public void setUser(String user) {
53+
_user = user;
54+
}
5155

5256
public String getPassword() {
5357
return _password;
5458
}
59+
60+
public void setPassword(String password) {
61+
_password = password;
62+
}
5563

5664
public Properties getReadQueries() {
5765
return _readProperties;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.ibm.opl.customdatasource.sql;
2+
3+
/**
4+
* Iterates over a string with the ability to look ahead one char
5+
* @author kong
6+
*
7+
*/
8+
public class LookAheadStringIterator {
9+
String _s;
10+
int _len;
11+
int _index;
12+
13+
/**
14+
* Construcs a new LookAheadStringIterator.
15+
* @param s The string to iterate over.
16+
*/
17+
public LookAheadStringIterator(String s) {
18+
_s = s;
19+
_index = 0;
20+
if (s != null)
21+
_len = s.length();
22+
else
23+
_len = 0;
24+
}
25+
26+
/**
27+
* @return The current char in the iterator.
28+
*/
29+
int currentChar() {
30+
if (_index < _len)
31+
return _s.charAt(_index);
32+
else
33+
return -1;
34+
}
35+
36+
/**
37+
*
38+
* @return The next char in the iterator.
39+
*/
40+
int nextChar() {
41+
if ((_index+1) < _len)
42+
return _s.charAt(_index+1);
43+
else
44+
return -1;
45+
}
46+
47+
/**
48+
* Advance to the next char.
49+
*/
50+
void next() {
51+
if (_index < _len)
52+
_index++;
53+
}
54+
55+
/**
56+
* @return true if there are more chars available on this iterator.
57+
*/
58+
boolean available() {
59+
return _index < _len;
60+
}
61+
62+
/**
63+
* Extracts the next java identifier available from the string. The identifier starts at the current char.
64+
*
65+
* After the identifier is extracted, nextChar() returns the first char after the identifier.
66+
* @return The next java identifier available.
67+
*/
68+
public String extractIdentifierName() {
69+
int i = _index;
70+
while (i < _len && Character.isJavaIdentifierPart(_s.charAt(i))) {
71+
i++;
72+
}
73+
String name = _s.substring(_index, i);
74+
_index = i-1;
75+
return name;
76+
}
77+
78+
/**
79+
* Swallows all chars until endChar appears in the stream
80+
* @param buffer The StringBuffer to append swallowed chars
81+
* @param endChar
82+
*/
83+
void swallow(StringBuffer buffer, char endChar) {
84+
while (available()) {
85+
int c = currentChar();
86+
buffer.append((char)c); // append the char in every case
87+
if ((char)c != endChar) {
88+
// nothing to do, char already swallowed
89+
} else if ((char)nextChar() == c) {
90+
// case where ' or " is escaped (so '' or "")
91+
next();
92+
buffer.append((char)c);
93+
} else {
94+
break;
95+
}
96+
next();
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)