From 7db3af74f894e3a344926e9382a88b64d684e1ab Mon Sep 17 00:00:00 2001 From: jackyq2015 Date: Sun, 25 Nov 2018 16:25:51 -0500 Subject: [PATCH 1/3] fix issue #1819 --- .../scripts/index/database-source-ui.js | 12 --- .../extension/database/DatabaseService.java | 31 ++++++- .../mariadb/MariaDBDatabaseService.java | 86 +------------------ .../database/mysql/MySQLDatabaseService.java | 82 ------------------ .../database/pgsql/PgSQLDatabaseService.java | 81 +---------------- .../mariadb/MariaDBDatabaseServiceTest.java | 4 +- .../mysql/MySQLDatabaseServiceTest.java | 4 +- .../pgsql/PgSQLDatabaseServiceTest.java | 4 +- 8 files changed, 39 insertions(+), 265 deletions(-) diff --git a/extensions/database/module/scripts/index/database-source-ui.js b/extensions/database/module/scripts/index/database-source-ui.js index d7b892f4d..5b0d92bbb 100644 --- a/extensions/database/module/scripts/index/database-source-ui.js +++ b/extensions/database/module/scripts/index/database-source-ui.js @@ -228,20 +228,8 @@ Refine.DatabaseSourceUI.prototype.validateQuery = function(query) { }else if(allCapsQuery.indexOf('UPDATE') > -1){ window.alert($.i18n._('database-source/alert-invalid-query-keyword') + " UPDATE"); return false; - }else if(allCapsQuery.indexOf('LIMIT') > -1){ - window.alert($.i18n._('database-source/alert-invalid-query-keyword') + " LIMIT"); - return false; } -// if ((allCapsQuery.indexOf('DROP') > -1) || (allCapsQuery.indexOf('TRUNCATE') > -1) || -// (allCapsQuery.indexOf('DELETE') > -1) || (allCapsQuery.indexOf('ROLLBACK') > -1) -// || (allCapsQuery.indexOf('SHUTDOWN') > -1) || (allCapsQuery.indexOf('INSERT') > -1) -// || (allCapsQuery.indexOf('ALTER') > -1) || (allCapsQuery.indexOf('UPDATE') > -1)) -// { -// window.alert($.i18n._('database-source/alert-invalid-query-keyword')); -// return false; -// } - if(!allCapsQuery.startsWith('SELECT')) { window.alert($.i18n._('database-source/alert-invalid-query-select')); return false; diff --git a/extensions/database/src/com/google/refine/extension/database/DatabaseService.java b/extensions/database/src/com/google/refine/extension/database/DatabaseService.java index 13668a98f..c68785d46 100644 --- a/extensions/database/src/com/google/refine/extension/database/DatabaseService.java +++ b/extensions/database/src/com/google/refine/extension/database/DatabaseService.java @@ -116,7 +116,36 @@ public abstract class DatabaseService { public abstract DatabaseInfo testQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException; - public abstract String buildLimitQuery(Integer limit, Integer offset, String query); + public String buildLimitQuery(Integer limit, Integer offset, String query) { + if(logger.isDebugEnabled()) { + logger.info( "<<< original input query::{} >>>" , query ); + } + + final int len = query.length(); + String parsedQuery = len > 0 && query.endsWith(";") ? query.substring(0, len - 1) : query; + + + StringBuilder sb = new StringBuilder(); + sb.append("SELECT * FROM ("); + sb.append(parsedQuery); + sb.append(") data"); + + if(limit != null) { + sb.append(" LIMIT" + " " + limit); + } + + if(offset != null) { + sb.append(" OFFSET" + " " + offset); + } + sb.append(";"); + String parsedQueryOut = sb.toString(); + + if(logger.isDebugEnabled()) { + logger.info( "<<>>" , parsedQueryOut ); + } + + return parsedQueryOut; + } public abstract List getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException; diff --git a/extensions/database/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseService.java b/extensions/database/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseService.java index f5760a6b2..af8b4a948 100644 --- a/extensions/database/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseService.java +++ b/extensions/database/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseService.java @@ -36,7 +36,6 @@ import java.util.ArrayList; import java.util.List; import org.mariadb.jdbc.MariaDbResultSetMetaData; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,17 +47,11 @@ import com.google.refine.extension.database.SQLType; import com.google.refine.extension.database.model.DatabaseColumn; import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseRow; -import com.google.refine.extension.database.mysql.MySQLConnectionManager; - - public class MariaDBDatabaseService extends DatabaseService { - private static final Logger logger = LoggerFactory.getLogger("MariaDBDatabaseService"); - public static final String DB_NAME = "mariadb"; public static final String DB_DRIVER = "org.mariadb.jdbc.Driver"; - private static MariaDBDatabaseService instance; private MariaDBDatabaseService() { @@ -78,19 +71,15 @@ public class MariaDBDatabaseService extends DatabaseService { @Override public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ return MariaDBConnectionManager.getInstance().testConnection(dbConfig); - } @Override public DatabaseInfo connect(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ return getMetadata(dbConfig); } - @Override public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ - - try { Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false); Statement statement = connection.createStatement(); @@ -98,7 +87,6 @@ public class MariaDBDatabaseService extends DatabaseService { MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData)queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList columns = new ArrayList(columnCount); - for (int i = 1; i <= columnCount; i++) { DatabaseColumn dc = new DatabaseColumn( metadata.getColumnName(i), @@ -109,27 +97,21 @@ public class MariaDBDatabaseService extends DatabaseService { } int index = 0; List rows = new ArrayList(); - while (queryResult.next()) { DatabaseRow row = new DatabaseRow(); row.setIndex(index); List values = new ArrayList(columnCount); for (int i = 1; i <= columnCount; i++) { - values.add(queryResult.getString(i)); - } row.setValues(values); rows.add(row); index++; - } - DatabaseInfo dbInfo = new DatabaseInfo(); dbInfo.setColumns(columns); dbInfo.setRows(rows); return dbInfo; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -139,132 +121,78 @@ public class MariaDBDatabaseService extends DatabaseService { } /** - * * @param connectionInfo * @return * @throws DatabaseServiceException */ private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException { - try { Connection connection = MariaDBConnectionManager.getInstance().getConnection(connectionInfo, true); if(connection != null) { - java.sql.DatabaseMetaData metadata; - - metadata = connection.getMetaData(); - + java.sql.DatabaseMetaData metadata = connection.getMetaData(); int dbMajorVersion = metadata.getDatabaseMajorVersion(); int dbMinorVersion = metadata.getDatabaseMinorVersion(); String dbProductVersion = metadata.getDatabaseProductVersion(); String dbProductName = metadata.getDatabaseProductName(); DatabaseInfo dbInfo = new DatabaseInfo(); - dbInfo.setDatabaseMajorVersion(dbMajorVersion); dbInfo.setDatabaseMinorVersion(dbMinorVersion); dbInfo.setDatabaseProductVersion(dbProductVersion); dbInfo.setDatabaseProductName(dbProductName); return dbInfo; } - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); } return null; - } - @Override - public String buildLimitQuery(Integer limit, Integer offset, String query) { -// if(logger.isDebugEnabled()) { -// logger.info( "<<< original input query::{} >>>" , query ); -// } -// - final int len = query.length(); - String parsedQuery = len > 0 && query.endsWith(";") ? query.substring(0, len - 1) : query; - - - StringBuilder sb = new StringBuilder(); - sb.append(parsedQuery); - - if(limit != null) { - sb.append(" LIMIT" + " " + limit); - } - - if(offset != null) { - sb.append(" OFFSET" + " " + offset); - } - sb.append(";"); - String parsedQueryOut = sb.toString(); - -// if(logger.isDebugEnabled()) { -// logger.info( "<<>>" , parsedQueryOut ); -// } - - return parsedQueryOut; - } - @Override public ArrayList getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ - try { - Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, true); Statement statement = connection.createStatement(); - ResultSet queryResult = statement.executeQuery(query); MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData) queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList columns = new ArrayList(columnCount); - for (int i = 1; i <= columnCount; i++) { DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i)); columns.add(dc); } - return columns; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); } - } @Override public List getRows(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException { - - try { Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false); Statement statement = connection.createStatement(); ResultSet queryResult = statement.executeQuery(query); MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData)queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); - int index = 0; List rows = new ArrayList(); - while (queryResult.next()) { DatabaseRow row = new DatabaseRow(); row.setIndex(index); List values = new ArrayList(columnCount); for (int i = 1; i <= columnCount; i++) { - values.add(queryResult.getString(i)); - } row.setValues(values); rows.add(row); index++; - } - return rows; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -273,11 +201,9 @@ public class MariaDBDatabaseService extends DatabaseService { @Override protected String getDatabaseUrl(DatabaseConfiguration dbConfig) { - int port = dbConfig.getDatabasePort(); return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost() + ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL(); - } @Override @@ -293,14 +219,11 @@ public class MariaDBDatabaseService extends DatabaseService { Statement statement = null; ResultSet queryResult = null; try { - Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); + Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, true); statement = connection.createStatement(); queryResult = statement.executeQuery(query); - DatabaseInfo dbInfo = new DatabaseInfo(); - return dbInfo; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -308,18 +231,15 @@ public class MariaDBDatabaseService extends DatabaseService { try { if (queryResult != null) { queryResult.close(); - } if (statement != null) { statement.close(); - } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } - MySQLConnectionManager.getInstance().shutdown(); + MariaDBConnectionManager.getInstance().shutdown(); } } - } diff --git a/extensions/database/src/com/google/refine/extension/database/mysql/MySQLDatabaseService.java b/extensions/database/src/com/google/refine/extension/database/mysql/MySQLDatabaseService.java index 503936e16..6a3fee162 100644 --- a/extensions/database/src/com/google/refine/extension/database/mysql/MySQLDatabaseService.java +++ b/extensions/database/src/com/google/refine/extension/database/mysql/MySQLDatabaseService.java @@ -46,13 +46,9 @@ import com.google.refine.extension.database.SQLType; import com.google.refine.extension.database.model.DatabaseColumn; import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseRow; -//import com.mysql.jdbc.ResultSetMetaData; public class MySQLDatabaseService extends DatabaseService { - - private static final Logger logger = LoggerFactory.getLogger("MySQLDatabaseService"); - public static final String DB_NAME = "mysql"; public static final String DB_DRIVER = "com.mysql.jdbc.Driver"; @@ -82,7 +78,6 @@ public class MySQLDatabaseService extends DatabaseService { public DatabaseInfo connect(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ return getMetadata(dbConfig); } - @Override public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ @@ -91,15 +86,11 @@ public class MySQLDatabaseService extends DatabaseService { Statement statement = connection.createStatement(); ResultSet queryResult = statement.executeQuery(query); java.sql.ResultSetMetaData metadata = queryResult.getMetaData(); - if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) { metadata = (com.mysql.jdbc.ResultSetMetaData)metadata; } - //ResultSetMetaData metadata = (ResultSetMetaData)queryResult.getMetaData(); - int columnCount = metadata.getColumnCount(); ArrayList columns = new ArrayList(columnCount); - for (int i = 1; i <= columnCount; i++) { DatabaseColumn dc = new DatabaseColumn( metadata.getColumnName(i), @@ -110,7 +101,6 @@ public class MySQLDatabaseService extends DatabaseService { } int index = 0; List rows = new ArrayList(); - while (queryResult.next()) { DatabaseRow row = new DatabaseRow(); row.setIndex(index); @@ -125,12 +115,10 @@ public class MySQLDatabaseService extends DatabaseService { index++; } - DatabaseInfo dbInfo = new DatabaseInfo(); dbInfo.setColumns(columns); dbInfo.setRows(rows); return dbInfo; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -140,145 +128,85 @@ public class MySQLDatabaseService extends DatabaseService { } /** - * * @param connectionInfo * @return * @throws DatabaseServiceException */ private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException { - try { Connection connection = MySQLConnectionManager.getInstance().getConnection(connectionInfo, true); if(connection != null) { java.sql.DatabaseMetaData metadata; - metadata = connection.getMetaData(); - int dbMajorVersion = metadata.getDatabaseMajorVersion(); int dbMinorVersion = metadata.getDatabaseMinorVersion(); String dbProductVersion = metadata.getDatabaseProductVersion(); String dbProductName = metadata.getDatabaseProductName(); DatabaseInfo dbInfo = new DatabaseInfo(); - dbInfo.setDatabaseMajorVersion(dbMajorVersion); dbInfo.setDatabaseMinorVersion(dbMinorVersion); dbInfo.setDatabaseProductVersion(dbProductVersion); dbInfo.setDatabaseProductName(dbProductName); return dbInfo; } - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); } - - return null; - - } - - @Override - public String buildLimitQuery(Integer limit, Integer offset, String query) { - if(logger.isDebugEnabled()) { - logger.info( "<<< original input query::{} >>>" , query ); - } - - final int len = query.length(); - String parsedQuery = len > 0 && query.endsWith(";") ? query.substring(0, len - 1) : query; - - - StringBuilder sb = new StringBuilder(); - sb.append(parsedQuery); - - if(limit != null) { - sb.append(" LIMIT" + " " + limit); - } - - if(offset != null) { - sb.append(" OFFSET" + " " + offset); - } - sb.append(";"); - String parsedQueryOut = sb.toString(); - - if(logger.isDebugEnabled()) { - logger.info( "<<>>" , parsedQueryOut ); - } - - return parsedQueryOut; } @Override public ArrayList getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ - try { Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); Statement statement = connection.createStatement(); - ResultSet queryResult = statement.executeQuery(query); java.sql.ResultSetMetaData metadata = queryResult.getMetaData(); if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) { metadata = (com.mysql.jdbc.ResultSetMetaData)metadata; } - - //ResultSetMetaData metadata = (ResultSetMetaData) queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList columns = new ArrayList(columnCount); - for (int i = 1; i <= columnCount; i++) { DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i)); columns.add(dc); } - return columns; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); } - - } @Override public List getRows(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException { - try { Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, false); - Statement statement = connection.createStatement(); statement.setFetchSize(10); ResultSet queryResult = statement.executeQuery(query); - java.sql.ResultSetMetaData metadata = queryResult.getMetaData(); if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) { metadata = (com.mysql.jdbc.ResultSetMetaData)metadata; } - //logger.info("metadata class::" + metadata.getClass()); - int columnCount = metadata.getColumnCount(); - int index = 0; List rows = new ArrayList(); - while (queryResult.next()) { DatabaseRow row = new DatabaseRow(); row.setIndex(index); List values = new ArrayList(columnCount); for (int i = 1; i <= columnCount; i++) { - values.add(queryResult.getString(i)); - } row.setValues(values); rows.add(row); index++; - } - return rows; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -287,14 +215,11 @@ public class MySQLDatabaseService extends DatabaseService { @Override protected String getDatabaseUrl(DatabaseConfiguration dbConfig) { - int port = dbConfig.getDatabasePort(); return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost() + ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL(); - } - @Override public Connection getConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException { @@ -311,11 +236,8 @@ public class MySQLDatabaseService extends DatabaseService { Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); statement = connection.createStatement(); queryResult = statement.executeQuery(query); - DatabaseInfo dbInfo = new DatabaseInfo(); - return dbInfo; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -323,11 +245,9 @@ public class MySQLDatabaseService extends DatabaseService { try { if (queryResult != null) { queryResult.close(); - } if (statement != null) { statement.close(); - } } catch (SQLException e) { // TODO Auto-generated catch block @@ -336,6 +256,4 @@ public class MySQLDatabaseService extends DatabaseService { MySQLConnectionManager.getInstance().shutdown(); } } - - } diff --git a/extensions/database/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseService.java b/extensions/database/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseService.java index 9a6308373..10051ca16 100644 --- a/extensions/database/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseService.java +++ b/extensions/database/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseService.java @@ -47,15 +47,11 @@ import com.google.refine.extension.database.SQLType; import com.google.refine.extension.database.model.DatabaseColumn; import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseRow; -import com.google.refine.extension.database.mysql.MySQLConnectionManager; public class PgSQLDatabaseService extends DatabaseService { - private static final Logger logger = LoggerFactory.getLogger("PgSQLDatabaseService"); - public static final String DB_NAME = "postgresql"; public static final String DB_DRIVER = "org.postgresql.Driver"; - private static PgSQLDatabaseService instance; private PgSQLDatabaseService() { @@ -72,7 +68,6 @@ public class PgSQLDatabaseService extends DatabaseService { return instance; } - @Override public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ return PgSQLConnectionManager.getInstance().testConnection(dbConfig); @@ -83,12 +78,9 @@ public class PgSQLDatabaseService extends DatabaseService { public DatabaseInfo connect(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ return getMetadata(dbConfig); } - @Override public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ - - try { Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false); Statement statement = connection.createStatement(); @@ -96,7 +88,6 @@ public class PgSQLDatabaseService extends DatabaseService { PgResultSetMetaData metadata = (PgResultSetMetaData)queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList columns = new ArrayList(columnCount); - for (int i = 1; i <= columnCount; i++) { DatabaseColumn dc = new DatabaseColumn( metadata.getColumnName(i), @@ -107,27 +98,21 @@ public class PgSQLDatabaseService extends DatabaseService { } int index = 0; List rows = new ArrayList(); - while (queryResult.next()) { DatabaseRow row = new DatabaseRow(); row.setIndex(index); List values = new ArrayList(columnCount); for (int i = 1; i <= columnCount; i++) { - values.add(queryResult.getString(i)); - } row.setValues(values); rows.add(row); index++; - } - DatabaseInfo dbInfo = new DatabaseInfo(); dbInfo.setColumns(columns); dbInfo.setRows(rows); return dbInfo; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -137,105 +122,57 @@ public class PgSQLDatabaseService extends DatabaseService { } /** - * * @param connectionInfo * @return * @throws DatabaseServiceException */ private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException { - - - try { Connection connection = PgSQLConnectionManager.getInstance().getConnection(connectionInfo, true); if(connection != null) { - java.sql.DatabaseMetaData metadata; - - metadata = connection.getMetaData(); - + java.sql.DatabaseMetaData metadata = connection.getMetaData(); int dbMajorVersion = metadata.getDatabaseMajorVersion(); int dbMinorVersion = metadata.getDatabaseMinorVersion(); String dbProductVersion = metadata.getDatabaseProductVersion(); String dbProductName = metadata.getDatabaseProductName(); DatabaseInfo dbInfo = new DatabaseInfo(); - dbInfo.setDatabaseMajorVersion(dbMajorVersion); dbInfo.setDatabaseMinorVersion(dbMinorVersion); dbInfo.setDatabaseProductVersion(dbProductVersion); dbInfo.setDatabaseProductName(dbProductName); return dbInfo; } - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); } - return null; } - @Override - public String buildLimitQuery(Integer limit, Integer offset, String query) { - if(logger.isDebugEnabled()) { - logger.debug( "<<< original input query::{} >>>" , query ); - } - - final int len = query.length(); - String parsedQuery = len > 0 && query.endsWith(";") ? query.substring(0, len - 1) : query; - - - StringBuilder sb = new StringBuilder(); - sb.append(parsedQuery); - - if(limit != null) { - sb.append(" LIMIT" + " " + limit); - } - - if(offset != null) { - sb.append(" OFFSET" + " " + offset); - } - sb.append(";"); - String parsedQueryOut = sb.toString(); - - if(logger.isDebugEnabled()) { - logger.debug( "<<>>" , parsedQueryOut ); - } - - return parsedQueryOut; - } - @Override public ArrayList getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ - try { Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true); Statement statement = connection.createStatement(); - ResultSet queryResult = statement.executeQuery(query); PgResultSetMetaData metadata = (PgResultSetMetaData) queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList columns = new ArrayList(columnCount); - for (int i = 1; i <= columnCount; i++) { DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i)); columns.add(dc); } - return columns; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); } - - } @Override public List getRows(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException { - try { Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false); Statement statement = connection.createStatement(); @@ -243,10 +180,8 @@ public class PgSQLDatabaseService extends DatabaseService { ResultSet queryResult = statement.executeQuery(query); PgResultSetMetaData metadata = (PgResultSetMetaData)queryResult.getMetaData(); int columnCount = metadata.getColumnCount(); - int index = 0; List rows = new ArrayList(); - while (queryResult.next()) { DatabaseRow row = new DatabaseRow(); row.setIndex(index); @@ -257,11 +192,8 @@ public class PgSQLDatabaseService extends DatabaseService { row.setValues(values); rows.add(row); index++; - } - return rows; - } catch (SQLException e) { logger.error("SQLException::{}::{}", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -270,11 +202,9 @@ public class PgSQLDatabaseService extends DatabaseService { @Override protected String getDatabaseUrl(DatabaseConfiguration dbConfig) { - int port = dbConfig.getDatabasePort(); return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost() + ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL(); - } @Override @@ -289,14 +219,11 @@ public class PgSQLDatabaseService extends DatabaseService { Statement statement = null; ResultSet queryResult = null; try { - Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); + Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true); statement = connection.createStatement(); queryResult = statement.executeQuery(query); - DatabaseInfo dbInfo = new DatabaseInfo(); - return dbInfo; - } catch (SQLException e) { logger.error("SQLException::", e); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); @@ -304,17 +231,15 @@ public class PgSQLDatabaseService extends DatabaseService { try { if (queryResult != null) { queryResult.close(); - } if (statement != null) { statement.close(); - } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } - //MySQLConnectionManager.getInstance().shutdown(); + PgSQLConnectionManager.getInstance().shutdown(); } } diff --git a/extensions/database/tests/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseServiceTest.java b/extensions/database/tests/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseServiceTest.java index 927eb05d8..dc1373401 100644 --- a/extensions/database/tests/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseServiceTest.java +++ b/extensions/database/tests/src/com/google/refine/extension/database/mariadb/MariaDBDatabaseServiceTest.java @@ -99,9 +99,7 @@ public class MariaDBDatabaseServiceTest extends DBExtensionTests{ MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService.get(MariaDBDatabaseService.DB_NAME); String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); Assert.assertNotNull(limitQuery); - - Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";"); - + Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";"); } @Test diff --git a/extensions/database/tests/src/com/google/refine/extension/database/mysql/MySQLDatabaseServiceTest.java b/extensions/database/tests/src/com/google/refine/extension/database/mysql/MySQLDatabaseServiceTest.java index 653d468df..0c58301e8 100644 --- a/extensions/database/tests/src/com/google/refine/extension/database/mysql/MySQLDatabaseServiceTest.java +++ b/extensions/database/tests/src/com/google/refine/extension/database/mysql/MySQLDatabaseServiceTest.java @@ -99,9 +99,7 @@ public class MySQLDatabaseServiceTest extends DBExtensionTests{ MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService.get(MySQLDatabaseService.DB_NAME); String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); Assert.assertNotNull(limitQuery); - - Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";"); - + Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";"); } @Test diff --git a/extensions/database/tests/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseServiceTest.java b/extensions/database/tests/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseServiceTest.java index 161d669b7..664ba2900 100644 --- a/extensions/database/tests/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseServiceTest.java +++ b/extensions/database/tests/src/com/google/refine/extension/database/pgsql/PgSQLDatabaseServiceTest.java @@ -100,9 +100,7 @@ public class PgSQLDatabaseServiceTest extends DBExtensionTests{ PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService.get(PgSQLDatabaseService.DB_NAME); String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); Assert.assertNotNull(limitQuery); - - Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";"); - + Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";"); } @Test From d9df1b63d9fea8b1aa24ae265329ddea52825041 Mon Sep 17 00:00:00 2001 From: jackyq2015 Date: Sun, 25 Nov 2018 16:40:41 -0500 Subject: [PATCH 2/3] fix UT --- .../google/refine/extension/database/DatabaseServiceTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/extensions/database/tests/src/com/google/refine/extension/database/DatabaseServiceTest.java b/extensions/database/tests/src/com/google/refine/extension/database/DatabaseServiceTest.java index 2999d36c5..10dabca46 100644 --- a/extensions/database/tests/src/com/google/refine/extension/database/DatabaseServiceTest.java +++ b/extensions/database/tests/src/com/google/refine/extension/database/DatabaseServiceTest.java @@ -119,10 +119,8 @@ public class DatabaseServiceTest extends DBExtensionTests{ public void testBuildLimitQuery() { DatabaseService dbService = DatabaseService.get(testDbConfig.getDatabaseType()); String limitQuery = dbService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); - Assert.assertNotNull(limitQuery); - - Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";"); + Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";"); } @Test(groups = {"requiresMySQL"}) From 83019e74d9a75ad6f763a5a6740a0d680f133ea0 Mon Sep 17 00:00:00 2001 From: Jacky Date: Sun, 25 Nov 2018 23:24:41 -0500 Subject: [PATCH 3/3] fix typo --- extensions/database/module/scripts/index/database-source-ui.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/database/module/scripts/index/database-source-ui.js b/extensions/database/module/scripts/index/database-source-ui.js index fdfc35f8b..7b5932c2e 100644 --- a/extensions/database/module/scripts/index/database-source-ui.js +++ b/extensions/database/module/scripts/index/database-source-ui.js @@ -4,7 +4,7 @@ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * - Redistrihttps://github.com/OpenRefine/OpenRefine/pull/1862/conflict?name=extensions%252Fdatabase%252Fmodule%252Fscripts%252Findex%252Fdatabase-source-ui.js&ancestor_oid=d7b892f4d20ae2529e9bcc269502869eef9f1a31&base_oid=5b0d92bbb98c3603d39148470d319f1153e417ef&head_oid=997b6757adc87d5b9d4a9ef385eb6ec63e383739butions of source code must retain the above copyright notice, this + * - Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation