This commit is contained in:
jackyq2015 2018-11-25 16:25:51 -05:00
parent b9871dd670
commit 7db3af74f8
8 changed files with 39 additions and 265 deletions

View File

@ -228,20 +228,8 @@ Refine.DatabaseSourceUI.prototype.validateQuery = function(query) {
}else if(allCapsQuery.indexOf('UPDATE') > -1){ }else if(allCapsQuery.indexOf('UPDATE') > -1){
window.alert($.i18n._('database-source/alert-invalid-query-keyword') + " UPDATE"); window.alert($.i18n._('database-source/alert-invalid-query-keyword') + " UPDATE");
return false; 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')) { if(!allCapsQuery.startsWith('SELECT')) {
window.alert($.i18n._('database-source/alert-invalid-query-select')); window.alert($.i18n._('database-source/alert-invalid-query-select'));
return false; return false;

View File

@ -116,7 +116,36 @@ public abstract class DatabaseService {
public abstract DatabaseInfo testQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException; 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( "<<<Final input query::{} >>>" , parsedQueryOut );
}
return parsedQueryOut;
}
public abstract List<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException; public abstract List<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException;

View File

@ -36,7 +36,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.mariadb.jdbc.MariaDbResultSetMetaData; import org.mariadb.jdbc.MariaDbResultSetMetaData;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.DatabaseColumn;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.extension.database.model.DatabaseRow; import com.google.refine.extension.database.model.DatabaseRow;
import com.google.refine.extension.database.mysql.MySQLConnectionManager;
public class MariaDBDatabaseService extends DatabaseService { public class MariaDBDatabaseService extends DatabaseService {
private static final Logger logger = LoggerFactory.getLogger("MariaDBDatabaseService"); private static final Logger logger = LoggerFactory.getLogger("MariaDBDatabaseService");
public static final String DB_NAME = "mariadb"; public static final String DB_NAME = "mariadb";
public static final String DB_DRIVER = "org.mariadb.jdbc.Driver"; public static final String DB_DRIVER = "org.mariadb.jdbc.Driver";
private static MariaDBDatabaseService instance; private static MariaDBDatabaseService instance;
private MariaDBDatabaseService() { private MariaDBDatabaseService() {
@ -78,7 +71,6 @@ public class MariaDBDatabaseService extends DatabaseService {
@Override @Override
public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException{
return MariaDBConnectionManager.getInstance().testConnection(dbConfig); return MariaDBConnectionManager.getInstance().testConnection(dbConfig);
} }
@Override @Override
@ -86,11 +78,8 @@ public class MariaDBDatabaseService extends DatabaseService {
return getMetadata(dbConfig); return getMetadata(dbConfig);
} }
@Override @Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{
try { try {
Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false); Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
@ -98,7 +87,6 @@ public class MariaDBDatabaseService extends DatabaseService {
MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData)queryResult.getMetaData(); MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData)queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount); ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
DatabaseColumn dc = new DatabaseColumn( DatabaseColumn dc = new DatabaseColumn(
metadata.getColumnName(i), metadata.getColumnName(i),
@ -109,27 +97,21 @@ public class MariaDBDatabaseService extends DatabaseService {
} }
int index = 0; int index = 0;
List<DatabaseRow> rows = new ArrayList<DatabaseRow>(); List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
while (queryResult.next()) { while (queryResult.next()) {
DatabaseRow row = new DatabaseRow(); DatabaseRow row = new DatabaseRow();
row.setIndex(index); row.setIndex(index);
List<String> values = new ArrayList<String>(columnCount); List<String> values = new ArrayList<String>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
values.add(queryResult.getString(i)); values.add(queryResult.getString(i));
} }
row.setValues(values); row.setValues(values);
rows.add(row); rows.add(row);
index++; index++;
} }
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
dbInfo.setColumns(columns); dbInfo.setColumns(columns);
dbInfo.setRows(rows); dbInfo.setRows(rows);
return dbInfo; return dbInfo;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -139,132 +121,78 @@ public class MariaDBDatabaseService extends DatabaseService {
} }
/** /**
*
* @param connectionInfo * @param connectionInfo
* @return * @return
* @throws DatabaseServiceException * @throws DatabaseServiceException
*/ */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException { private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
try { try {
Connection connection = MariaDBConnectionManager.getInstance().getConnection(connectionInfo, true); Connection connection = MariaDBConnectionManager.getInstance().getConnection(connectionInfo, true);
if(connection != null) { if(connection != null) {
java.sql.DatabaseMetaData metadata; java.sql.DatabaseMetaData metadata = connection.getMetaData();
metadata = connection.getMetaData();
int dbMajorVersion = metadata.getDatabaseMajorVersion(); int dbMajorVersion = metadata.getDatabaseMajorVersion();
int dbMinorVersion = metadata.getDatabaseMinorVersion(); int dbMinorVersion = metadata.getDatabaseMinorVersion();
String dbProductVersion = metadata.getDatabaseProductVersion(); String dbProductVersion = metadata.getDatabaseProductVersion();
String dbProductName = metadata.getDatabaseProductName(); String dbProductName = metadata.getDatabaseProductName();
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
dbInfo.setDatabaseMajorVersion(dbMajorVersion); dbInfo.setDatabaseMajorVersion(dbMajorVersion);
dbInfo.setDatabaseMinorVersion(dbMinorVersion); dbInfo.setDatabaseMinorVersion(dbMinorVersion);
dbInfo.setDatabaseProductVersion(dbProductVersion); dbInfo.setDatabaseProductVersion(dbProductVersion);
dbInfo.setDatabaseProductName(dbProductName); dbInfo.setDatabaseProductName(dbProductName);
return dbInfo; return dbInfo;
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
} }
return null; 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( "<<<Final input query::{} >>>" , parsedQueryOut );
// }
return parsedQueryOut;
} }
@Override @Override
public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{
try { try {
Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, true); Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, true);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData) queryResult.getMetaData(); MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData) queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount); ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i),
DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i)); DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i));
columns.add(dc); columns.add(dc);
} }
return columns; return columns;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
} }
} }
@Override @Override
public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query) public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query)
throws DatabaseServiceException { throws DatabaseServiceException {
try { try {
Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false); Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, false);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData)queryResult.getMetaData(); MariaDbResultSetMetaData metadata = (MariaDbResultSetMetaData)queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
int index = 0; int index = 0;
List<DatabaseRow> rows = new ArrayList<DatabaseRow>(); List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
while (queryResult.next()) { while (queryResult.next()) {
DatabaseRow row = new DatabaseRow(); DatabaseRow row = new DatabaseRow();
row.setIndex(index); row.setIndex(index);
List<String> values = new ArrayList<String>(columnCount); List<String> values = new ArrayList<String>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
values.add(queryResult.getString(i)); values.add(queryResult.getString(i));
} }
row.setValues(values); row.setValues(values);
rows.add(row); rows.add(row);
index++; index++;
} }
return rows; return rows;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -273,11 +201,9 @@ public class MariaDBDatabaseService extends DatabaseService {
@Override @Override
protected String getDatabaseUrl(DatabaseConfiguration dbConfig) { protected String getDatabaseUrl(DatabaseConfiguration dbConfig) {
int port = dbConfig.getDatabasePort(); int port = dbConfig.getDatabasePort();
return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost() return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost()
+ ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL(); + ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL();
} }
@Override @Override
@ -293,14 +219,11 @@ public class MariaDBDatabaseService extends DatabaseService {
Statement statement = null; Statement statement = null;
ResultSet queryResult = null; ResultSet queryResult = null;
try { try {
Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); Connection connection = MariaDBConnectionManager.getInstance().getConnection(dbConfig, true);
statement = connection.createStatement(); statement = connection.createStatement();
queryResult = statement.executeQuery(query); queryResult = statement.executeQuery(query);
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
return dbInfo; return dbInfo;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -308,18 +231,15 @@ public class MariaDBDatabaseService extends DatabaseService {
try { try {
if (queryResult != null) { if (queryResult != null) {
queryResult.close(); queryResult.close();
} }
if (statement != null) { if (statement != null) {
statement.close(); statement.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
MySQLConnectionManager.getInstance().shutdown(); MariaDBConnectionManager.getInstance().shutdown();
} }
} }
} }

View File

@ -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.DatabaseColumn;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.extension.database.model.DatabaseRow; import com.google.refine.extension.database.model.DatabaseRow;
//import com.mysql.jdbc.ResultSetMetaData;
public class MySQLDatabaseService extends DatabaseService { public class MySQLDatabaseService extends DatabaseService {
private static final Logger logger = LoggerFactory.getLogger("MySQLDatabaseService"); private static final Logger logger = LoggerFactory.getLogger("MySQLDatabaseService");
public static final String DB_NAME = "mysql"; public static final String DB_NAME = "mysql";
public static final String DB_DRIVER = "com.mysql.jdbc.Driver"; public static final String DB_DRIVER = "com.mysql.jdbc.Driver";
@ -83,7 +79,6 @@ public class MySQLDatabaseService extends DatabaseService {
return getMetadata(dbConfig); return getMetadata(dbConfig);
} }
@Override @Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{
try { try {
@ -91,15 +86,11 @@ public class MySQLDatabaseService extends DatabaseService {
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
java.sql.ResultSetMetaData metadata = queryResult.getMetaData(); java.sql.ResultSetMetaData metadata = queryResult.getMetaData();
if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) { if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) {
metadata = (com.mysql.jdbc.ResultSetMetaData)metadata; metadata = (com.mysql.jdbc.ResultSetMetaData)metadata;
} }
//ResultSetMetaData metadata = (ResultSetMetaData)queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount); ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
DatabaseColumn dc = new DatabaseColumn( DatabaseColumn dc = new DatabaseColumn(
metadata.getColumnName(i), metadata.getColumnName(i),
@ -110,7 +101,6 @@ public class MySQLDatabaseService extends DatabaseService {
} }
int index = 0; int index = 0;
List<DatabaseRow> rows = new ArrayList<DatabaseRow>(); List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
while (queryResult.next()) { while (queryResult.next()) {
DatabaseRow row = new DatabaseRow(); DatabaseRow row = new DatabaseRow();
row.setIndex(index); row.setIndex(index);
@ -125,12 +115,10 @@ public class MySQLDatabaseService extends DatabaseService {
index++; index++;
} }
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
dbInfo.setColumns(columns); dbInfo.setColumns(columns);
dbInfo.setRows(rows); dbInfo.setRows(rows);
return dbInfo; return dbInfo;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -140,145 +128,85 @@ public class MySQLDatabaseService extends DatabaseService {
} }
/** /**
*
* @param connectionInfo * @param connectionInfo
* @return * @return
* @throws DatabaseServiceException * @throws DatabaseServiceException
*/ */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException { private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
try { try {
Connection connection = MySQLConnectionManager.getInstance().getConnection(connectionInfo, true); Connection connection = MySQLConnectionManager.getInstance().getConnection(connectionInfo, true);
if(connection != null) { if(connection != null) {
java.sql.DatabaseMetaData metadata; java.sql.DatabaseMetaData metadata;
metadata = connection.getMetaData(); metadata = connection.getMetaData();
int dbMajorVersion = metadata.getDatabaseMajorVersion(); int dbMajorVersion = metadata.getDatabaseMajorVersion();
int dbMinorVersion = metadata.getDatabaseMinorVersion(); int dbMinorVersion = metadata.getDatabaseMinorVersion();
String dbProductVersion = metadata.getDatabaseProductVersion(); String dbProductVersion = metadata.getDatabaseProductVersion();
String dbProductName = metadata.getDatabaseProductName(); String dbProductName = metadata.getDatabaseProductName();
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
dbInfo.setDatabaseMajorVersion(dbMajorVersion); dbInfo.setDatabaseMajorVersion(dbMajorVersion);
dbInfo.setDatabaseMinorVersion(dbMinorVersion); dbInfo.setDatabaseMinorVersion(dbMinorVersion);
dbInfo.setDatabaseProductVersion(dbProductVersion); dbInfo.setDatabaseProductVersion(dbProductVersion);
dbInfo.setDatabaseProductName(dbProductName); dbInfo.setDatabaseProductName(dbProductName);
return dbInfo; return dbInfo;
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
} }
return null; 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( "<<<Final input query::{} >>>" , parsedQueryOut );
}
return parsedQueryOut;
} }
@Override @Override
public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{
try { try {
Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
java.sql.ResultSetMetaData metadata = queryResult.getMetaData(); java.sql.ResultSetMetaData metadata = queryResult.getMetaData();
if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) { if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) {
metadata = (com.mysql.jdbc.ResultSetMetaData)metadata; metadata = (com.mysql.jdbc.ResultSetMetaData)metadata;
} }
//ResultSetMetaData metadata = (ResultSetMetaData) queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount); ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i),
DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i)); DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i));
columns.add(dc); columns.add(dc);
} }
return columns; return columns;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
} }
} }
@Override @Override
public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query) public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query)
throws DatabaseServiceException { throws DatabaseServiceException {
try { try {
Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, false); Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, false);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
statement.setFetchSize(10); statement.setFetchSize(10);
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
java.sql.ResultSetMetaData metadata = queryResult.getMetaData(); java.sql.ResultSetMetaData metadata = queryResult.getMetaData();
if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) { if(metadata instanceof com.mysql.jdbc.ResultSetMetaData) {
metadata = (com.mysql.jdbc.ResultSetMetaData)metadata; metadata = (com.mysql.jdbc.ResultSetMetaData)metadata;
} }
//logger.info("metadata class::" + metadata.getClass());
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
int index = 0; int index = 0;
List<DatabaseRow> rows = new ArrayList<DatabaseRow>(); List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
while (queryResult.next()) { while (queryResult.next()) {
DatabaseRow row = new DatabaseRow(); DatabaseRow row = new DatabaseRow();
row.setIndex(index); row.setIndex(index);
List<String> values = new ArrayList<String>(columnCount); List<String> values = new ArrayList<String>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
values.add(queryResult.getString(i)); values.add(queryResult.getString(i));
} }
row.setValues(values); row.setValues(values);
rows.add(row); rows.add(row);
index++; index++;
} }
return rows; return rows;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -287,14 +215,11 @@ public class MySQLDatabaseService extends DatabaseService {
@Override @Override
protected String getDatabaseUrl(DatabaseConfiguration dbConfig) { protected String getDatabaseUrl(DatabaseConfiguration dbConfig) {
int port = dbConfig.getDatabasePort(); int port = dbConfig.getDatabasePort();
return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost() return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost()
+ ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL(); + ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL();
} }
@Override @Override
public Connection getConnection(DatabaseConfiguration dbConfig) public Connection getConnection(DatabaseConfiguration dbConfig)
throws DatabaseServiceException { throws DatabaseServiceException {
@ -311,11 +236,8 @@ public class MySQLDatabaseService extends DatabaseService {
Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true);
statement = connection.createStatement(); statement = connection.createStatement();
queryResult = statement.executeQuery(query); queryResult = statement.executeQuery(query);
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
return dbInfo; return dbInfo;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -323,11 +245,9 @@ public class MySQLDatabaseService extends DatabaseService {
try { try {
if (queryResult != null) { if (queryResult != null) {
queryResult.close(); queryResult.close();
} }
if (statement != null) { if (statement != null) {
statement.close(); statement.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -336,6 +256,4 @@ public class MySQLDatabaseService extends DatabaseService {
MySQLConnectionManager.getInstance().shutdown(); MySQLConnectionManager.getInstance().shutdown();
} }
} }
} }

View File

@ -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.DatabaseColumn;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.extension.database.model.DatabaseRow; import com.google.refine.extension.database.model.DatabaseRow;
import com.google.refine.extension.database.mysql.MySQLConnectionManager;
public class PgSQLDatabaseService extends DatabaseService { public class PgSQLDatabaseService extends DatabaseService {
private static final Logger logger = LoggerFactory.getLogger("PgSQLDatabaseService"); private static final Logger logger = LoggerFactory.getLogger("PgSQLDatabaseService");
public static final String DB_NAME = "postgresql"; public static final String DB_NAME = "postgresql";
public static final String DB_DRIVER = "org.postgresql.Driver"; public static final String DB_DRIVER = "org.postgresql.Driver";
private static PgSQLDatabaseService instance; private static PgSQLDatabaseService instance;
private PgSQLDatabaseService() { private PgSQLDatabaseService() {
@ -72,7 +68,6 @@ public class PgSQLDatabaseService extends DatabaseService {
return instance; return instance;
} }
@Override @Override
public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException{ public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException{
return PgSQLConnectionManager.getInstance().testConnection(dbConfig); return PgSQLConnectionManager.getInstance().testConnection(dbConfig);
@ -84,11 +79,8 @@ public class PgSQLDatabaseService extends DatabaseService {
return getMetadata(dbConfig); return getMetadata(dbConfig);
} }
@Override @Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{
try { try {
Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false); Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
@ -96,7 +88,6 @@ public class PgSQLDatabaseService extends DatabaseService {
PgResultSetMetaData metadata = (PgResultSetMetaData)queryResult.getMetaData(); PgResultSetMetaData metadata = (PgResultSetMetaData)queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount); ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
DatabaseColumn dc = new DatabaseColumn( DatabaseColumn dc = new DatabaseColumn(
metadata.getColumnName(i), metadata.getColumnName(i),
@ -107,27 +98,21 @@ public class PgSQLDatabaseService extends DatabaseService {
} }
int index = 0; int index = 0;
List<DatabaseRow> rows = new ArrayList<DatabaseRow>(); List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
while (queryResult.next()) { while (queryResult.next()) {
DatabaseRow row = new DatabaseRow(); DatabaseRow row = new DatabaseRow();
row.setIndex(index); row.setIndex(index);
List<String> values = new ArrayList<String>(columnCount); List<String> values = new ArrayList<String>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
values.add(queryResult.getString(i)); values.add(queryResult.getString(i));
} }
row.setValues(values); row.setValues(values);
rows.add(row); rows.add(row);
index++; index++;
} }
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
dbInfo.setColumns(columns); dbInfo.setColumns(columns);
dbInfo.setRows(rows); dbInfo.setRows(rows);
return dbInfo; return dbInfo;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -137,105 +122,57 @@ public class PgSQLDatabaseService extends DatabaseService {
} }
/** /**
*
* @param connectionInfo * @param connectionInfo
* @return * @return
* @throws DatabaseServiceException * @throws DatabaseServiceException
*/ */
private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException { private DatabaseInfo getMetadata(DatabaseConfiguration connectionInfo) throws DatabaseServiceException {
try { try {
Connection connection = PgSQLConnectionManager.getInstance().getConnection(connectionInfo, true); Connection connection = PgSQLConnectionManager.getInstance().getConnection(connectionInfo, true);
if(connection != null) { if(connection != null) {
java.sql.DatabaseMetaData metadata; java.sql.DatabaseMetaData metadata = connection.getMetaData();
metadata = connection.getMetaData();
int dbMajorVersion = metadata.getDatabaseMajorVersion(); int dbMajorVersion = metadata.getDatabaseMajorVersion();
int dbMinorVersion = metadata.getDatabaseMinorVersion(); int dbMinorVersion = metadata.getDatabaseMinorVersion();
String dbProductVersion = metadata.getDatabaseProductVersion(); String dbProductVersion = metadata.getDatabaseProductVersion();
String dbProductName = metadata.getDatabaseProductName(); String dbProductName = metadata.getDatabaseProductName();
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
dbInfo.setDatabaseMajorVersion(dbMajorVersion); dbInfo.setDatabaseMajorVersion(dbMajorVersion);
dbInfo.setDatabaseMinorVersion(dbMinorVersion); dbInfo.setDatabaseMinorVersion(dbMinorVersion);
dbInfo.setDatabaseProductVersion(dbProductVersion); dbInfo.setDatabaseProductVersion(dbProductVersion);
dbInfo.setDatabaseProductName(dbProductName); dbInfo.setDatabaseProductName(dbProductName);
return dbInfo; return dbInfo;
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
} }
return null; 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( "<<<Final input query::{} >>>" , parsedQueryOut );
}
return parsedQueryOut;
}
@Override @Override
public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{ public ArrayList<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException{
try { try {
Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true); Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
PgResultSetMetaData metadata = (PgResultSetMetaData) queryResult.getMetaData(); PgResultSetMetaData metadata = (PgResultSetMetaData) queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount); ArrayList<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(columnCount);
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i), DatabaseColumn dc = new DatabaseColumn(metadata.getColumnName(i), metadata.getColumnLabel(i),
DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i)); DatabaseUtils.getDbColumnType(metadata.getColumnType(i)), metadata.getColumnDisplaySize(i));
columns.add(dc); columns.add(dc);
} }
return columns; return columns;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
} }
} }
@Override @Override
public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query) public List<DatabaseRow> getRows(DatabaseConfiguration dbConfig, String query)
throws DatabaseServiceException { throws DatabaseServiceException {
try { try {
Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false); Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
@ -243,10 +180,8 @@ public class PgSQLDatabaseService extends DatabaseService {
ResultSet queryResult = statement.executeQuery(query); ResultSet queryResult = statement.executeQuery(query);
PgResultSetMetaData metadata = (PgResultSetMetaData)queryResult.getMetaData(); PgResultSetMetaData metadata = (PgResultSetMetaData)queryResult.getMetaData();
int columnCount = metadata.getColumnCount(); int columnCount = metadata.getColumnCount();
int index = 0; int index = 0;
List<DatabaseRow> rows = new ArrayList<DatabaseRow>(); List<DatabaseRow> rows = new ArrayList<DatabaseRow>();
while (queryResult.next()) { while (queryResult.next()) {
DatabaseRow row = new DatabaseRow(); DatabaseRow row = new DatabaseRow();
row.setIndex(index); row.setIndex(index);
@ -257,11 +192,8 @@ public class PgSQLDatabaseService extends DatabaseService {
row.setValues(values); row.setValues(values);
rows.add(row); rows.add(row);
index++; index++;
} }
return rows; return rows;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::{}::{}", e); logger.error("SQLException::{}::{}", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -270,11 +202,9 @@ public class PgSQLDatabaseService extends DatabaseService {
@Override @Override
protected String getDatabaseUrl(DatabaseConfiguration dbConfig) { protected String getDatabaseUrl(DatabaseConfiguration dbConfig) {
int port = dbConfig.getDatabasePort(); int port = dbConfig.getDatabasePort();
return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost() return "jdbc:" + dbConfig.getDatabaseType() + "://" + dbConfig.getDatabaseHost()
+ ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL(); + ((port == 0) ? "" : (":" + port)) + "/" + dbConfig.getDatabaseName() + "?useSSL=" + dbConfig.isUseSSL();
} }
@Override @Override
@ -289,14 +219,11 @@ public class PgSQLDatabaseService extends DatabaseService {
Statement statement = null; Statement statement = null;
ResultSet queryResult = null; ResultSet queryResult = null;
try { try {
Connection connection = MySQLConnectionManager.getInstance().getConnection(dbConfig, true); Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, true);
statement = connection.createStatement(); statement = connection.createStatement();
queryResult = statement.executeQuery(query); queryResult = statement.executeQuery(query);
DatabaseInfo dbInfo = new DatabaseInfo(); DatabaseInfo dbInfo = new DatabaseInfo();
return dbInfo; return dbInfo;
} catch (SQLException e) { } catch (SQLException e) {
logger.error("SQLException::", e); logger.error("SQLException::", e);
throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage()); throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
@ -304,17 +231,15 @@ public class PgSQLDatabaseService extends DatabaseService {
try { try {
if (queryResult != null) { if (queryResult != null) {
queryResult.close(); queryResult.close();
} }
if (statement != null) { if (statement != null) {
statement.close(); statement.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
//MySQLConnectionManager.getInstance().shutdown(); PgSQLConnectionManager.getInstance().shutdown();
} }
} }

View File

@ -99,9 +99,7 @@ public class MariaDBDatabaseServiceTest extends DBExtensionTests{
MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService.get(MariaDBDatabaseService.DB_NAME); MariaDBDatabaseService pgSqlService = (MariaDBDatabaseService) DatabaseService.get(MariaDBDatabaseService.DB_NAME);
String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable);
Assert.assertNotNull(limitQuery); Assert.assertNotNull(limitQuery);
Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";");
Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";");
} }
@Test @Test

View File

@ -99,9 +99,7 @@ public class MySQLDatabaseServiceTest extends DBExtensionTests{
MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService.get(MySQLDatabaseService.DB_NAME); MySQLDatabaseService pgSqlService = (MySQLDatabaseService) DatabaseService.get(MySQLDatabaseService.DB_NAME);
String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable);
Assert.assertNotNull(limitQuery); Assert.assertNotNull(limitQuery);
Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";");
Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";");
} }
@Test @Test

View File

@ -100,9 +100,7 @@ public class PgSQLDatabaseServiceTest extends DBExtensionTests{
PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService.get(PgSQLDatabaseService.DB_NAME); PgSQLDatabaseService pgSqlService = (PgSQLDatabaseService) DatabaseService.get(PgSQLDatabaseService.DB_NAME);
String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable); String limitQuery = pgSqlService.buildLimitQuery(100, 0, "SELECT * FROM " + testTable);
Assert.assertNotNull(limitQuery); Assert.assertNotNull(limitQuery);
Assert.assertEquals(limitQuery, "SELECT * FROM (SELECT * FROM " + testTable + ") data LIMIT " + 100 + " OFFSET " + 0 + ";");
Assert.assertEquals(limitQuery, "SELECT * FROM " + testTable + " LIMIT " + 100 + " OFFSET " + 0 + ";");
} }
@Test @Test