fix issue #1819
This commit is contained in:
parent
b9871dd670
commit
7db3af74f8
@ -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;
|
||||
|
@ -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( "<<<Final input query::{} >>>" , parsedQueryOut );
|
||||
}
|
||||
|
||||
return parsedQueryOut;
|
||||
}
|
||||
|
||||
public abstract List<DatabaseColumn> getColumns(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException;
|
||||
|
||||
|
@ -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<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(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<DatabaseRow> rows = new ArrayList<DatabaseRow>();
|
||||
|
||||
while (queryResult.next()) {
|
||||
DatabaseRow row = new DatabaseRow();
|
||||
row.setIndex(index);
|
||||
List<String> values = new ArrayList<String>(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( "<<<Final input query::{} >>>" , parsedQueryOut );
|
||||
// }
|
||||
|
||||
return parsedQueryOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<DatabaseColumn> 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<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(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<DatabaseRow> 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<DatabaseRow> rows = new ArrayList<DatabaseRow>();
|
||||
|
||||
while (queryResult.next()) {
|
||||
DatabaseRow row = new DatabaseRow();
|
||||
row.setIndex(index);
|
||||
List<String> values = new ArrayList<String>(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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(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<DatabaseRow> rows = new ArrayList<DatabaseRow>();
|
||||
|
||||
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( "<<<Final input query::{} >>>" , parsedQueryOut );
|
||||
}
|
||||
|
||||
return parsedQueryOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<DatabaseColumn> 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<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(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<DatabaseRow> 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<DatabaseRow> rows = new ArrayList<DatabaseRow>();
|
||||
|
||||
while (queryResult.next()) {
|
||||
DatabaseRow row = new DatabaseRow();
|
||||
row.setIndex(index);
|
||||
List<String> values = new ArrayList<String>(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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(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<DatabaseRow> rows = new ArrayList<DatabaseRow>();
|
||||
|
||||
while (queryResult.next()) {
|
||||
DatabaseRow row = new DatabaseRow();
|
||||
row.setIndex(index);
|
||||
List<String> values = new ArrayList<String>(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( "<<<Final input query::{} >>>" , parsedQueryOut );
|
||||
}
|
||||
|
||||
return parsedQueryOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<DatabaseColumn> 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<DatabaseColumn> columns = new ArrayList<DatabaseColumn>(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<DatabaseRow> 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<DatabaseRow> rows = new ArrayList<DatabaseRow>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user