Skip to content

Commit f543725

Browse files
authored
Merge pull request #29 from mendix/callable-statements
This PR adds support for calling statements with more complex input and output structures than the prepared statements already available in the module. Because of this extra complexity, it also adds entities to the module's domain model to allow the user to define a more complex call/return process.
2 parents 2a6329d + 426861b commit f543725

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3854
-1240
lines changed

DatabaseConnector.mpr

180 KB
Binary file not shown.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// This file was generated by Mendix Studio Pro.
2+
//
3+
// WARNING: Only the following code will be retained when actions are regenerated:
4+
// - the import list
5+
// - the code between BEGIN USER CODE and END USER CODE
6+
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7+
// Other code you write will be lost the next time you deploy the project.
8+
// Special characters, e.g., é, ö, à, etc. are supported in comments.
9+
10+
package databaseconnector.actions;
11+
12+
import com.mendix.core.Core;
13+
import com.mendix.logging.ILogNode;
14+
import com.mendix.systemwideinterfaces.core.IContext;
15+
import com.mendix.webui.CustomJavaAction;
16+
import databaseconnector.impl.JdbcConnector;
17+
import com.mendix.systemwideinterfaces.core.IMendixObject;
18+
19+
/**
20+
* For a more detailed documentation, please visit the website at
21+
* https://docs.mendix.com/appstore/connectors/database-connector
22+
*
23+
* This Java action provides a consistent environment for Mendix projects to
24+
* call an arbitrary statement on external relational databases.
25+
*
26+
* This action requires an instance of the Statement NPE (Non-Persistable Entity)
27+
* with associated NPEs of its Parameters. Input parameters need to be filled in
28+
* before calling this action and output parameters will be filled in afterwards.
29+
*
30+
* The JDBC drivers for the databases you want to connect to must be placed
31+
* inside the userlib directory of your project.
32+
*
33+
* Note: Proper security is required when manually composing the statement text to
34+
* avoid SQL injection.
35+
*
36+
* @param jdbcUrl A database JDBC URL address that points to your database.
37+
*
38+
* @param userName The user name for logging into the database.
39+
*
40+
* @param password The password for logging into the database.
41+
*
42+
* @param statement An instance of the Statement NPE containing both the content of the
43+
* statement to be called as well as all of its parameters.
44+
*/
45+
public class ExecuteCallableStatement extends CustomJavaAction<java.lang.Void>
46+
{
47+
private java.lang.String jdbcUrl;
48+
private java.lang.String userName;
49+
private java.lang.String password;
50+
private IMendixObject __statement;
51+
private databaseconnector.proxies.Statement statement;
52+
53+
public ExecuteCallableStatement(IContext context, java.lang.String jdbcUrl, java.lang.String userName, java.lang.String password, IMendixObject statement)
54+
{
55+
super(context);
56+
this.jdbcUrl = jdbcUrl;
57+
this.userName = userName;
58+
this.password = password;
59+
this.__statement = statement;
60+
}
61+
62+
@java.lang.Override
63+
public java.lang.Void executeAction() throws Exception
64+
{
65+
this.statement = __statement == null ? null : databaseconnector.proxies.Statement.initialize(getContext(), __statement);
66+
67+
// BEGIN USER CODE
68+
if (this.statement == null) {
69+
throw new IllegalArgumentException("Execute callable statement was called with an empty value.");
70+
}
71+
connector.executeCallableStatement(this.jdbcUrl, this.userName, this.password, this.statement);
72+
return null;
73+
// END USER CODE
74+
}
75+
76+
/**
77+
* Returns a string representation of this action
78+
*/
79+
@java.lang.Override
80+
public java.lang.String toString()
81+
{
82+
return "ExecuteCallableStatement";
83+
}
84+
85+
// BEGIN EXTRA CODE
86+
private final ILogNode logNode = Core.getLogger(this.getClass().getName());
87+
88+
private final JdbcConnector connector = new JdbcConnector(logNode);
89+
// END EXTRA CODE
90+
}

javasource/databaseconnector/actions/ExecuteParameterizedQuery.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
package databaseconnector.actions;
1111

1212
import java.util.List;
13-
import java.util.stream.Collectors;
14-
import java.util.stream.Stream;
1513
import com.mendix.core.Core;
1614
import com.mendix.logging.ILogNode;
1715
import com.mendix.systemwideinterfaces.core.IContext;
@@ -21,39 +19,34 @@
2119
import databaseconnector.impl.JdbcConnector;
2220

2321
/**
24-
* <p>
25-
* This Java action provides a consistent environment for Mendix projects to perform an arbitrary parameterized SELECT SQL query on relational external databases.
26-
* JDBC (Java Database Connectivity) API, a standard Java API, is used when this Java action attempts
27-
* to connect with a Relational Database for which a JDBC driver exists.
28-
* The JDBC drivers for the databases you want to connect to, must be placed inside the userlib directory of a project.
29-
* </p>
22+
* For a more detailed documentation, please visit the website at
23+
* https://docs.mendix.com/appstore/connectors/database-connector
3024
*
31-
* Do not use this Java action for INSERT, UPDATE, DELETE or DDL queries.
32-
* This action returns a list of Mendix objects based on the JDBC result set.
33-
* The jdbcUrl argument must specify a database URL address that points to your relational database and is dependent
34-
* upon the particular database and JDBC driver. It will always begin with "jdbc:" protocol, but the rest is up to particular vendor.
35-
* For example 'jdbc:mysql://hostname/databaseName' jdbcUrl format can be used for MySQL databases.
36-
* Note: Proper security must be applied as this action can allow SQL Injection in your Mendix application.
25+
* This Java action provides a consistent environment for Mendix projects to
26+
* perform an arbitrary parameterized SELECT SQL query on external relational
27+
* databases.
3728
*
38-
* @see JdbcConnector
39-
* @since Mendix World 2016
40-
* @param <String> jdbcUrl
41-
* A database URL address that points to your database.
29+
* Do not use this Java action for INSERT, UPDATE, DELETE or DDL queries. This
30+
* action returns a list of Mendix objects based on the JDBC result set.
4231
*
43-
* @param <String> userName
44-
* The user name for logging into the database, relative to the jdbcUrl argument.
32+
* The JDBC drivers for the databases you want to connect to must be placed
33+
* inside the userlib directory of your project.
4534
*
46-
* @param <String> password
47-
* The password for logging into the database, relative to the jdbcUrl argument.
35+
* Note: Proper security is required when manually composing the query text to
36+
* avoid SQL injection.
4837
*
49-
* @param <IStringTemplate> sql
50-
* A string template containing the SELECT query to be performed and the query parameters, relative to the database type.
38+
* @param jdbcUrl A database JDBC URL address that points to your database.
5139
*
52-
* @param <String> resultObjectType
53-
* A fully qualified name of the result object type. Concrete <IMetaObject> can be retrieved with a call to `Core.getMetaObject`.
40+
* @param userName The user name for logging into the database.
5441
*
55-
* @return <List<IMendixObject>>
56-
* SELECT Query result as a list of objects.
42+
* @param password The password for logging into the database.
43+
*
44+
* @param sql A string template containing the SELECT query to be performed and
45+
* its query parameters.
46+
*
47+
* @param resultObjectType A fully qualified name for the result object type.
48+
*
49+
* @return Result of the query as a list of mendix objects.
5750
*/
5851
public class ExecuteParameterizedQuery extends CustomJavaAction<java.util.List<IMendixObject>>
5952
{
@@ -78,9 +71,8 @@ public java.util.List<IMendixObject> executeAction() throws Exception
7871
{
7972
// BEGIN USER CODE
8073
IMetaObject metaObject = Core.getMetaObject(this.resultObjectType);
81-
Stream<IMendixObject> resultStream = connector.executeQuery(
82-
this.jdbcUrl, this.userName, this.password, metaObject, this.sql, this.getContext());
83-
List<IMendixObject> resultList = resultStream.collect(Collectors.toList());
74+
List<IMendixObject> resultList = connector.executeQuery(this.jdbcUrl, this.userName, this.password,
75+
metaObject, this.sql, this.getContext());
8476
logNode.trace(String.format("Result list count: %d", resultList.size()));
8577

8678
return resultList;

javasource/databaseconnector/actions/ExecuteParameterizedStatement.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,34 @@
1616
import databaseconnector.impl.JdbcConnector;
1717

1818
/**
19-
* <p>
20-
* This Java action provides a consistent environment for Mendix projects to perform an arbitrary parameterized SQL statement on relational
21-
* external databases.
22-
* JDBC (Java Database Connectivity) API, a standard Java API, is used when this Java action attempts
23-
* to connect with a Relational Database for which a JDBC driver exists.
24-
* The JDBC drivers for the databases you want to connect to, must be placed inside the userlib directory of a project.
25-
* </p>
19+
* For a more detailed documentation, please visit the website at
20+
* https://docs.mendix.com/appstore/connectors/database-connector
2621
*
27-
* Do not use this Java action for SELECT queries.
28-
* This Java action returns number of affected rows.
29-
* The jdbcUrl argument must specify a database URL address that points to your relational database and is dependent
30-
* upon the particular database and JDBC driver. It will always begin with "jdbc:" protocol, but the rest is up to particular vendor.
31-
* For example 'jdbc:mysql://hostname/databaseName' jdbcUrl format can be used for MySQL databases.
32-
* Note: Proper security must be applied as this action can allow SQL Injection in your Mendix application.
22+
* This Java action provides a consistent environment for Mendix projects to
23+
* perform arbitrary parameterized SQL statements on external relational
24+
* databases. The statement text may contain placeholders to be filled in with
25+
* values directly from Mendix.
3326
*
34-
* @see JdbcConnector
35-
* @since Mendix World 2016
36-
* @param <String> jdbcUrl
37-
* A database URL address that points to your database.
27+
* Do not use this Java action for SELECT queries. This action returns the
28+
* number of affected rows.
3829
*
39-
* @param <String> userName
40-
* The user name for logging into the database, relative to the jdbcUrl argument.
30+
* The JDBC drivers for the databases you want to connect to must be placed
31+
* inside the userlib directory of your project.
4132
*
42-
* @param <String> password
43-
* The password for logging into the database, relative to the jdbcUrl argument.
33+
* Note: While the text parameters are properly escaped, proper security is still
34+
* required when manually composing the parameterized template text to avoid
35+
* SQL injection.
4436
*
45-
* @param <IStringTemplate> sql
46-
* A string template containing the SQL statement to be performed and the statement parameters, relative to the database type.
37+
* @param jdbcUrl A database JDBC URL address that points to your database.
4738
*
48-
* @return <Integer/Long>
49-
* Number of affected rows.
39+
* @param userName The user name for logging into the database.
40+
*
41+
* @param password The password for logging into the database.
42+
*
43+
* @param sql A string template containing the SQL statement to be performed and the
44+
* statement parameters.
45+
*
46+
* @return Number of affected rows.
5047
*/
5148
public class ExecuteParameterizedStatement extends CustomJavaAction<java.lang.Long>
5249
{
@@ -84,6 +81,6 @@ public java.lang.String toString()
8481
// BEGIN EXTRA CODE
8582
private final ILogNode logNode = Core.getLogger(this.getClass().getName());
8683

87-
private final JdbcConnector connector = new JdbcConnector(logNode);
84+
private final JdbcConnector connector = new JdbcConnector(logNode);
8885
// END EXTRA CODE
8986
}

javasource/databaseconnector/actions/ExecuteQuery.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
package databaseconnector.actions;
1111

1212
import java.util.List;
13-
import java.util.stream.Collectors;
14-
import java.util.stream.Stream;
1513
import com.mendix.core.Core;
1614
import com.mendix.logging.ILogNode;
1715
import com.mendix.systemwideinterfaces.core.IContext;
@@ -21,39 +19,33 @@
2119
import databaseconnector.impl.JdbcConnector;
2220

2321
/**
24-
* <p>
25-
* This Java action provides a consistent environment for Mendix projects to perform an arbitrary SELECT SQL query on relational external databases.
26-
* JDBC (Java Database Connectivity) API, a standard Java API, is used when this Java action attempts
27-
* to connect with a Relational Database for which a JDBC driver exists.
28-
* The JDBC drivers for the databases you want to connect to, must be placed inside the userlib directory of a project.
29-
* </p>
22+
* For a more detailed documentation, please visit the website at
23+
* https://docs.mendix.com/appstore/connectors/database-connector
3024
*
31-
* Do not use this Java action for INSERT, UPDATE, DELETE or DDL queries.
32-
* This action returns a list of Mendix objects based on the JDBC result set.
33-
* The jdbcUrl argument must specify a database URL address that points to your relational database and is dependent
34-
* upon the particular database and JDBC driver. It will always begin with "jdbc:" protocol, but the rest is up to particular vendor.
35-
* For example 'jdbc:mysql://hostname/databaseName' jdbcUrl format can be used for MySQL databases.
36-
* Note: Proper security must be applied as this action can allow SQL Injection in your Mendix application.
25+
* This Java action provides a consistent environment for Mendix projects to
26+
* perform an arbitrary SELECT SQL query on external relational databases.
3727
*
38-
* @see JdbcConnector
39-
* @since Mendix World 2016
40-
* @param <String> jdbcUrl
41-
* A database URL address that points to your database.
28+
* Do not use this Java action for INSERT, UPDATE, DELETE or DDL queries. This
29+
* action returns a list of Mendix objects based on the JDBC result set.
4230
*
43-
* @param <String> userName
44-
* The user name for logging into the database, relative to the jdbcUrl argument.
31+
* The JDBC drivers for the databases you want to connect to must be placed
32+
* inside the userlib directory of your project.
4533
*
46-
* @param <String> password
47-
* The password for logging into the database, relative to the jdbcUrl argument.
34+
* Note: Proper security is required when manually composing the query text to
35+
* avoid SQL injection.
4836
*
49-
* @param <String> sql
50-
* The SELECT query to be performed, relative to the database type.
37+
* @param jdbcUrl A database JDBC URL address that points to your database.
5138
*
52-
* @param <IMendixObject> resultObject
53-
* An instance of the resulting object. This instance is used only for defining the type of object to be returned.
39+
* @param userName The user name for logging into the database.
5440
*
55-
* @return <List<IMendixObject>>
56-
* SELECT Query result as a list of objects.
41+
* @param password The password for logging into the database.
42+
*
43+
* @param sql The SELECT query to be performed.
44+
*
45+
* @param resultObject An instance of the resulting object. This instance is used
46+
* only for defining the type of object to be returned.
47+
*
48+
* @return Result of the query as a list of mendix objects.
5749
*/
5850
public class ExecuteQuery extends CustomJavaAction<java.util.List<IMendixObject>>
5951
{
@@ -78,9 +70,8 @@ public java.util.List<IMendixObject> executeAction() throws Exception
7870
{
7971
// BEGIN USER CODE
8072
IMetaObject metaObject = resultObject.getMetaObject();
81-
Stream<IMendixObject> resultStream = connector.executeQuery(
82-
this.jdbcUrl, this.userName, this.password, metaObject, this.sql, this.getContext());
83-
List<IMendixObject> resultList = resultStream.collect(Collectors.toList());
73+
List<IMendixObject> resultList = connector.executeQuery(this.jdbcUrl, this.userName, this.password,
74+
metaObject, this.sql, this.getContext());
8475
logNode.trace(String.format("Result list count: %d", resultList.size()));
8576

8677
return resultList;

0 commit comments

Comments
 (0)