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

Commit

Permalink
Merge pull request #9 from zazi/sprint-42/dd-965
Browse files Browse the repository at this point in the history
[DD-965] enhanced sqlmap function
  • Loading branch information
fniederl committed Jul 7, 2015
2 parents 7c7fe70 + 0507b4c commit 74e6305
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.culturegraph</groupId>
<artifactId>metafacture-core</artifactId>
<version>3.1.3-dswarm-SNAPSHOT</version>
<version>3.1.4-dswarm-SNAPSHOT</version>
<name>metafacture.core</name>
<description>Core package of the metafacture tool suite</description>
<licenses>
Expand Down
55 changes: 43 additions & 12 deletions src/main/java/org/culturegraph/mf/morph/maps/SqlMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.sql.SQLException;

import org.culturegraph.mf.exceptions.MorphException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

private static final Logger LOG = LoggerFactory.getLogger(SqlMap.class);

public static final String JDBC_PREFIX_IDENTIFIER = "jdbc";
public static final String COLON = ":";
public static final String SLASH = "/";

private boolean isUninitialized = true;

private Connection conn;
private String host;
private String login;
private String password;
private String database;
private String query;
private String driver;
private String host;
private String port;
private String login;
private String password;
private String database;
private String query;
private String driver;
private String databaseType;

private PreparedStatement preparedStatement;

public void init() {

try {

LOG.debug("generate a prepared statement with the following query string '{}'", query);

preparedStatement = getMySqlConnection().prepareStatement(query);
} catch (final SQLException e) {
throw new MorphException(e);
Expand All @@ -72,15 +85,26 @@ public void close() throws IOException {
private Connection getMySqlConnection() {

try {
Class.forName(driver);
Class.forName(driver).newInstance();

final StringBuilder urlSB = new StringBuilder();
urlSB.append(JDBC_PREFIX_IDENTIFIER).append(COLON)
.append(databaseType).append(COLON).append(SLASH).append(SLASH)
.append(host).append(COLON).append(port).append(SLASH).append(database);

final String url = urlSB.toString();

conn = DriverManager.getConnection("jdbc:mysql://" + host + "/"
+ database + "?" + "user=" + login + "&" + "password="
+ password);
LOG.debug("try to connection to database with connection string '{}'", url);

conn = DriverManager.getConnection(url, login, password);
} catch (final ClassNotFoundException e) {
throw new MorphException(e);
} catch (final SQLException e) {
throw new MorphException(e);
} catch (final InstantiationException e) {
throw new MorphException(e);
} catch (final IllegalAccessException e) {
throw new MorphException(e);
}
return conn;
}
Expand All @@ -93,10 +117,10 @@ public String get(final Object key) {
String resultString = null;
final ResultSet resultSet;
try {
preparedStatement.setString(1, key.toString());
preparedStatement.setObject(1, key.toString());
resultSet = preparedStatement.executeQuery();
if (resultSet.first()) {
resultString = resultSet.getString(1);
resultString = resultSet.getObject(1).toString();
}
resultSet.close();
} catch (final SQLException e) {
Expand Down Expand Up @@ -129,4 +153,11 @@ public void setQuery(final String query) {
this.query = query;
}

public void setPort(final String port) {
this.port = port;
}

public void setDatabaseType(final String databaseType) {
this.databaseType = databaseType;
}
}
6 changes: 6 additions & 0 deletions src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@
</element>

<element name="sqlmap">
<annotation>
<documentation>
<![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>
</annotation>
<complexType>
<attribute name="name" type="string" use="required"/>
<attribute name="host" type="string" use="optional"
Expand All @@ -624,6 +628,8 @@
<attribute name="query" type="string" use="required"/>
<attribute name="driver" type="string" use="optional"
default="com.mysql.jdbc.Driver"/>
<attribute name="databaseType" type="string" use="optional" default="mysql"/>
<attribute name="port" type="string" use="optional" default="3306"/>
<attribute ref="xml:base"/>
</complexType>
</element>
Expand Down

0 comments on commit 74e6305

Please sign in to comment.