Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit 74e6305

Browse files
committed
Merge pull request #9 from zazi/sprint-42/dd-965
[DD-965] enhanced sqlmap function
2 parents 7c7fe70 + 0507b4c commit 74e6305

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.culturegraph</groupId>
55
<artifactId>metafacture-core</artifactId>
6-
<version>3.1.3-dswarm-SNAPSHOT</version>
6+
<version>3.1.4-dswarm-SNAPSHOT</version>
77
<name>metafacture.core</name>
88
<description>Core package of the metafacture tool suite</description>
99
<licenses>

src/main/java/org/culturegraph/mf/morph/maps/SqlMap.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.sql.SQLException;
2525

2626
import org.culturegraph.mf.exceptions.MorphException;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2729

2830
/**
2931
* A map implementation that queries an sql database.
@@ -35,21 +37,32 @@
3537
public final class SqlMap extends AbstractReadOnlyMap<String, String> implements
3638
Closeable {
3739

40+
private static final Logger LOG = LoggerFactory.getLogger(SqlMap.class);
41+
42+
public static final String JDBC_PREFIX_IDENTIFIER = "jdbc";
43+
public static final String COLON = ":";
44+
public static final String SLASH = "/";
45+
3846
private boolean isUninitialized = true;
3947

4048
private Connection conn;
41-
private String host;
42-
private String login;
43-
private String password;
44-
private String database;
45-
private String query;
46-
private String driver;
49+
private String host;
50+
private String port;
51+
private String login;
52+
private String password;
53+
private String database;
54+
private String query;
55+
private String driver;
56+
private String databaseType;
4757

4858
private PreparedStatement preparedStatement;
4959

5060
public void init() {
5161

5262
try {
63+
64+
LOG.debug("generate a prepared statement with the following query string '{}'", query);
65+
5366
preparedStatement = getMySqlConnection().prepareStatement(query);
5467
} catch (final SQLException e) {
5568
throw new MorphException(e);
@@ -72,15 +85,26 @@ public void close() throws IOException {
7285
private Connection getMySqlConnection() {
7386

7487
try {
75-
Class.forName(driver);
88+
Class.forName(driver).newInstance();
89+
90+
final StringBuilder urlSB = new StringBuilder();
91+
urlSB.append(JDBC_PREFIX_IDENTIFIER).append(COLON)
92+
.append(databaseType).append(COLON).append(SLASH).append(SLASH)
93+
.append(host).append(COLON).append(port).append(SLASH).append(database);
94+
95+
final String url = urlSB.toString();
7696

77-
conn = DriverManager.getConnection("jdbc:mysql://" + host + "/"
78-
+ database + "?" + "user=" + login + "&" + "password="
79-
+ password);
97+
LOG.debug("try to connection to database with connection string '{}'", url);
98+
99+
conn = DriverManager.getConnection(url, login, password);
80100
} catch (final ClassNotFoundException e) {
81101
throw new MorphException(e);
82102
} catch (final SQLException e) {
83103
throw new MorphException(e);
104+
} catch (final InstantiationException e) {
105+
throw new MorphException(e);
106+
} catch (final IllegalAccessException e) {
107+
throw new MorphException(e);
84108
}
85109
return conn;
86110
}
@@ -93,10 +117,10 @@ public String get(final Object key) {
93117
String resultString = null;
94118
final ResultSet resultSet;
95119
try {
96-
preparedStatement.setString(1, key.toString());
120+
preparedStatement.setObject(1, key.toString());
97121
resultSet = preparedStatement.executeQuery();
98122
if (resultSet.first()) {
99-
resultString = resultSet.getString(1);
123+
resultString = resultSet.getObject(1).toString();
100124
}
101125
resultSet.close();
102126
} catch (final SQLException e) {
@@ -129,4 +153,11 @@ public void setQuery(final String query) {
129153
this.query = query;
130154
}
131155

156+
public void setPort(final String port) {
157+
this.port = port;
158+
}
159+
160+
public void setDatabaseType(final String databaseType) {
161+
this.databaseType = databaseType;
162+
}
132163
}

src/main/resources/schemata/metamorph.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@
614614
</element>
615615

616616
<element name="sqlmap">
617+
<annotation>
618+
<documentation>
619+
<![CDATA[Does a lookup in a SQL table via a prepared SQL statement, i.e., the statement needs to include some variable parts. Furthermore, the connection string to the SQL database can be defined via the following attributes: "databaseType" = the type of the database, e.g., 'mysql'; "host" = the host of the database, e.g., 'localhost' (default = "localhost"); "port" = the port of the database, e.g., "3306" (default = "3306"); "database" = the name of the database; "login" = the username that should be utilised to connect to the database; "password" = the password that should be utilised to connect to the database; "query" = the prepared SQL statement that should be utilised to retrieve the value for a given key, e.g. "SELECT value FROM mytable WHERE key = ?"; "driver" = the JDBC driver that should be utilised to connect to the database (default = "com.mysql.jdbc.Driver"; note: the database driver needs to be part of the classpath of the execution environment).]]></documentation>
620+
</annotation>
617621
<complexType>
618622
<attribute name="name" type="string" use="required"/>
619623
<attribute name="host" type="string" use="optional"
@@ -624,6 +628,8 @@
624628
<attribute name="query" type="string" use="required"/>
625629
<attribute name="driver" type="string" use="optional"
626630
default="com.mysql.jdbc.Driver"/>
631+
<attribute name="databaseType" type="string" use="optional" default="mysql"/>
632+
<attribute name="port" type="string" use="optional" default="3306"/>
627633
<attribute ref="xml:base"/>
628634
</complexType>
629635
</element>

0 commit comments

Comments
 (0)