Skip to content

Commit 897191a

Browse files
committed
Merge allmost done
1 parent c255de9 commit 897191a

32 files changed

+3186
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ This is an open source component for working with object-relational database man
44
### Purpose
55
With this component you will have following triggers:
66

7-
``SELECT`` - this trigger will execute an [SQL](https://en.wikipedia.org/wiki/SQL "SQL") queryOld that returns multiple results, it has limitations on the queryOld and suited only for SELECT type of queries. The trigger will remember last execution timestamp and let you build queries on it.
7+
``SELECT`` - this trigger will execute an [SQL](https://en.wikipedia.org/wiki/SQL "SQL") query that returns multiple results, it has limitations on the query and suited only for SELECT type of queries. The trigger will remember last execution timestamp and let you build queries on it.
88

9-
``GET ROWS POLLING`` - this trigger will execute select queryOld from specified table with simple criteria of selected datetime or timestamp table. The trigger will remember last execution timestamp and let you build queries on it.
9+
``GET ROWS POLLING`` - this trigger will execute select query from specified table with simple criteria of selected datetime or timestamp table. The trigger will remember last execution timestamp and let you build queries on it.
1010

1111
Following actions are inside:
1212

13-
``SELECT`` - this action will execute an [SQL](https://en.wikipedia.org/wiki/SQL "SQL") queryOld that returns multiple results, it has limitations on the queryOld and suited only for SELECT type of queries.
13+
``SELECT`` - this action will execute an [SQL](https://en.wikipedia.org/wiki/SQL "SQL") query that returns multiple results, it has limitations on the query and suited only for SELECT type of queries.
1414

15-
``LOOKUP BY PRIMARY KEY`` - this action will execute select queryOld from specified table, as criteria can be used only [PRIMARY KEY](https://en.wikipedia.org/wiki/Primary_key "PRIMARY KEY"). The action returns only one result (a primary key is unique).
15+
``LOOKUP BY PRIMARY KEY`` - this action will execute select query from specified table, as criteria can be used only [PRIMARY KEY](https://en.wikipedia.org/wiki/Primary_key "PRIMARY KEY"). The action returns only one result (a primary key is unique).
1616

17-
``DELETE BY PRIMARY KEY`` - this action will execute delete queryOld from specified table, as criteria can be used only [PRIMARY KEY](https://en.wikipedia.org/wiki/Primary_key "PRIMARY KEY"). The action returns an integer value that indicates the number of rows affected, the returned value can be 0 or 1 (a primary key is unique).
17+
``DELETE BY PRIMARY KEY`` - this action will execute delete query from specified table, as criteria can be used only [PRIMARY KEY](https://en.wikipedia.org/wiki/Primary_key "PRIMARY KEY"). The action returns an integer value that indicates the number of rows affected, the returned value can be 0 or 1 (a primary key is unique).
1818
### How works
1919

2020
### Requirements
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package io.elastic.jdbc;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import io.elastic.api.DynamicMetadataProvider;
6+
import io.elastic.api.SelectModelProvider;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import java.sql.*;
11+
import java.util.Map;
12+
13+
@Deprecated
14+
public class ColumnNamesProviderOld implements DynamicMetadataProvider, SelectModelProvider {
15+
private static final Logger logger = LoggerFactory.getLogger(ColumnNamesProviderOld.class);
16+
17+
public javax.json.JsonObject getSelectModel(javax.json.JsonObject configuration) {
18+
JsonObject result = new JsonObject();
19+
JsonObject properties = SailorVersionsAdapter.javaxToGson(getColumns(configuration));
20+
for (Map.Entry<String, JsonElement> entry : properties.entrySet()) {
21+
JsonObject field = entry.getValue().getAsJsonObject();
22+
result.addProperty(entry.getKey(), field.get("title").getAsString());
23+
}
24+
return SailorVersionsAdapter.gsonToJavax(result);
25+
}
26+
27+
/**
28+
* Returns Columns list as metadata
29+
*
30+
* @param configuration
31+
* @return
32+
*/
33+
34+
public javax.json.JsonObject getMetaModel(javax.json.JsonObject configuration) {
35+
JsonObject result = new JsonObject();
36+
JsonObject inMetadata = new JsonObject();
37+
JsonObject properties = SailorVersionsAdapter.javaxToGson(getColumns(configuration));
38+
inMetadata.addProperty("type", "object");
39+
inMetadata.add("properties", properties);
40+
result.add("out", inMetadata);
41+
result.add("in", inMetadata);
42+
return SailorVersionsAdapter.gsonToJavax(result);
43+
}
44+
45+
public javax.json.JsonObject getColumns(javax.json.JsonObject configuration) {
46+
if (SailorVersionsAdapter.javaxToGson(configuration).get("tableName") == null ||
47+
SailorVersionsAdapter.javaxToGson(configuration).get("tableName").getAsString().isEmpty()) {
48+
throw new RuntimeException("Table name is required");
49+
}
50+
String tableName = SailorVersionsAdapter.javaxToGson(configuration).get("tableName").getAsString();
51+
JsonObject properties = new JsonObject();
52+
Connection connection = null;
53+
ResultSet rs = null;
54+
String schemaName = null;
55+
Boolean isEmpty = true;
56+
try {
57+
connection = UtilsOld.getConnection(SailorVersionsAdapter.javaxToGson(configuration));
58+
DatabaseMetaData dbMetaData = connection.getMetaData();
59+
if (tableName.contains(".")) {
60+
schemaName = tableName.split("\\.")[0];
61+
tableName = tableName.split("\\.")[1];
62+
}
63+
rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
64+
while (rs.next()) {
65+
JsonObject field = new JsonObject();
66+
String name = rs.getString("COLUMN_NAME");
67+
Boolean isRequired = rs.getInt("NULLABLE") == 0 && !rs.getString("IS_AUTOINCREMENT").equals("YES");
68+
field.addProperty("required", isRequired);
69+
field.addProperty("title", name);
70+
field.addProperty("type", convertType(rs.getInt("DATA_TYPE")));
71+
properties.add(name, field);
72+
isEmpty = false;
73+
}
74+
if (isEmpty) {
75+
properties.addProperty("", "no columns");
76+
}
77+
} catch (SQLException e) {
78+
throw new RuntimeException(e);
79+
} finally {
80+
if (rs != null) {
81+
try {
82+
rs.close();
83+
} catch (SQLException e) {
84+
logger.error("Failed to close result set", e.toString());
85+
}
86+
}
87+
if (connection != null) {
88+
try {
89+
connection.close();
90+
} catch (SQLException e) {
91+
logger.error("Failed to close connection", e.toString());
92+
}
93+
}
94+
}
95+
return SailorVersionsAdapter.gsonToJavax(properties);
96+
}
97+
98+
/**
99+
* Converts JDBC column type name to js type according to http://db.apache.org/ojb/docu/guides/jdbc-types.html
100+
*
101+
* @param sqlType JDBC column type
102+
* @return
103+
* @url http://db.apache.org/ojb/docu/guides/jdbc-types.html
104+
*/
105+
private String convertType(Integer sqlType) {
106+
if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL || sqlType == Types.TINYINT
107+
|| sqlType == Types.SMALLINT || sqlType == Types.INTEGER || sqlType == Types.BIGINT
108+
|| sqlType == Types.REAL || sqlType == Types.FLOAT || sqlType == Types.DOUBLE) {
109+
return "number";
110+
}
111+
if (sqlType == Types.BIT || sqlType == Types.BOOLEAN) {
112+
return "boolean";
113+
}
114+
return "string";
115+
}
116+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.elastic.jdbc;
2+
3+
@Deprecated
4+
public enum EnginesOld {
5+
MYSQL("com.mysql.jdbc.Driver", 3306) {
6+
@Override
7+
protected String getSubprotocol(String host, Integer port, String db) {
8+
return "mysql";
9+
}
10+
},
11+
12+
HSQLDB("org.hsqldb.jdbcDriver", null) {
13+
@Override
14+
protected String getSubprotocol(String host, Integer port, String db) {
15+
return "hsqldb";
16+
}
17+
18+
@Override
19+
protected String getSubname(String host, Integer port, String db) {
20+
return db;
21+
}
22+
},
23+
24+
POSTGRESQL("org.postgresql.Driver", 5432) {
25+
@Override
26+
protected String getSubprotocol(String host, Integer port, String db) {
27+
return "postgresql";
28+
}
29+
},
30+
31+
ORACLE("oracle.jdbc.driver.OracleDriver", 1521) {
32+
@Override
33+
protected String getSubprotocol(String host, Integer port, String db) {
34+
return "oracle:thin";
35+
}
36+
37+
@Override
38+
protected String getSubname(String host, Integer port, String db) {
39+
return String.format("@%s:%s:%s", host, port, db);
40+
}
41+
},
42+
43+
MSSQL("com.microsoft.sqlserver.jdbc.SQLServerDriver", 1433) {
44+
@Override
45+
protected String getSubprotocol(String host, Integer port, String db) {
46+
return "sqlserver";
47+
}
48+
49+
@Override
50+
protected String getSubname(String host, Integer port, String db) {
51+
return String.format("//%s:%s;;databaseName=%s", host, port, db);
52+
}
53+
};
54+
55+
private final String driverClassName;
56+
private Integer defaultPort;
57+
58+
EnginesOld(final String driverClassName, final Integer defaultPort) {
59+
this.driverClassName = driverClassName;
60+
this.defaultPort = defaultPort;
61+
}
62+
63+
protected abstract String getSubprotocol(String host, Integer port, String db);
64+
65+
protected String getSubname(String host, Integer port, String db) {
66+
return String.format("//%s:%s/%s", host, port, db);
67+
}
68+
69+
public String getConnectionString(String host, Integer port, String db) {
70+
return String.format("jdbc:%s:%s",
71+
getSubprotocol(host, port, db),
72+
getSubname(host, port, db));
73+
}
74+
75+
public void loadDriverClass() {
76+
try {
77+
Class.forName(driverClassName);
78+
} catch (ClassNotFoundException e) {
79+
throw new RuntimeException(e);
80+
}
81+
}
82+
83+
public Integer defaultPort() {
84+
return defaultPort;
85+
}
86+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.elastic.jdbc.QueryBuilders;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
@Deprecated
9+
public class MSSQLOld extends QueryOld {
10+
public ResultSet execute(Connection connection) throws SQLException {
11+
validateQuery();
12+
String sql = "WITH Results_CTE AS" +
13+
"(" +
14+
" SELECT" +
15+
" *," +
16+
" ROW_NUMBER() OVER (ORDER BY " + orderField + ") AS RowNum" +
17+
" FROM " + tableName +
18+
" )" +
19+
" SELECT *" +
20+
" FROM Results_CTE" +
21+
" WHERE RowNum > ?" +
22+
" AND RowNum < ?";
23+
PreparedStatement stmt = connection.prepareStatement(sql);
24+
stmt.setInt(1, skipNumber);
25+
stmt.setInt(2, countNumber + skipNumber);
26+
return stmt.executeQuery();
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.elastic.jdbc.QueryBuilders;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
@Deprecated
9+
public class MySQLOld extends QueryOld {
10+
public ResultSet execute(Connection connection) throws SQLException {
11+
validateQuery();
12+
13+
StringBuilder sql = new StringBuilder("SELECT * FROM ");
14+
sql.append(tableName);
15+
if (orderField != null) {
16+
sql.append(" ORDER BY ").append(orderField);
17+
}
18+
sql.append(" ASC LIMIT ? OFFSET ?");
19+
20+
PreparedStatement stmt = connection.prepareStatement(sql.toString());
21+
stmt.setInt(1, countNumber);
22+
stmt.setInt(2, skipNumber);
23+
return stmt.executeQuery();
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.elastic.jdbc.QueryBuilders;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
@Deprecated
9+
public class OracleOld extends QueryOld {
10+
public ResultSet execute(Connection connection) throws SQLException {
11+
validateQuery();
12+
String sql = "SELECT * FROM " +
13+
"(SELECT b.*, rank() over (order by " + orderField + ") as rnk FROM " +
14+
tableName + " b) WHERE rnk BETWEEN ? AND ? " +
15+
"ORDER BY " + orderField;
16+
PreparedStatement stmt = connection.prepareStatement(sql);
17+
stmt.setInt(1, skipNumber);
18+
stmt.setInt(2, countNumber);
19+
return stmt.executeQuery();
20+
}
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.elastic.jdbc.QueryBuilders;
2+
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
7+
@Deprecated
8+
public abstract class QueryOld {
9+
protected Integer skipNumber = 0;
10+
protected Integer countNumber = 5000;
11+
protected String tableName = null;
12+
protected String orderField = null;
13+
14+
public QueryOld skip(Integer skip) {
15+
this.skipNumber = skip;
16+
return this;
17+
}
18+
19+
public QueryOld count(Integer count) {
20+
this.countNumber = count;
21+
return this;
22+
}
23+
24+
public QueryOld from(String tableName) {
25+
this.tableName = tableName;
26+
return this;
27+
}
28+
29+
public QueryOld orderBy(String fieldName) {
30+
this.orderField = fieldName;
31+
return this;
32+
}
33+
34+
abstract public ResultSet execute(Connection connection) throws SQLException;
35+
36+
public void validateQuery() {
37+
if (tableName == null) {
38+
throw new RuntimeException("Table name is required field");
39+
}
40+
if (orderField == null) {
41+
throw new RuntimeException("Order field is required field");
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)