use of com.google.refine.extension.database.model.DatabaseInfo in project OpenRefine by OpenRefine.
the class PgSQLDatabaseService method getMetadata.
/**
* @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 = 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;
}
use of com.google.refine.extension.database.model.DatabaseInfo in project OpenRefine by OpenRefine.
the class PgSQLDatabaseService method testQuery.
@Override
public DatabaseInfo testQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
Statement statement = null;
ResultSet queryResult = null;
try {
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());
} finally {
try {
if (queryResult != null) {
queryResult.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PgSQLConnectionManager.getInstance().shutdown();
}
}
use of com.google.refine.extension.database.model.DatabaseInfo in project OpenRefine by OpenRefine.
the class PgSQLDatabaseService method executeQuery.
@Override
public DatabaseInfo executeQuery(DatabaseConfiguration dbConfig, String query) throws DatabaseServiceException {
try {
Connection connection = PgSQLConnectionManager.getInstance().getConnection(dbConfig, false);
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);
}
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());
} finally {
PgSQLConnectionManager.getInstance().shutdown();
}
}
use of com.google.refine.extension.database.model.DatabaseInfo in project OpenRefine by OpenRefine.
the class ConnectCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
DatabaseConfiguration databaseConfiguration = getJdbcConfiguration(request);
if (logger.isDebugEnabled()) {
logger.debug("ConnectCommand::Post::{}", databaseConfiguration);
}
// ProjectManager.singleton.setBusy(true);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
ObjectMapper mapperObj = new ObjectMapper();
try {
DatabaseInfo databaseInfo = DatabaseService.get(databaseConfiguration.getDatabaseType()).connect(databaseConfiguration);
String databaseInfoString = mapperObj.writeValueAsString(databaseInfo);
response.setStatus(HttpStatus.SC_OK);
writer.writeStartObject();
writer.writeStringField("code", "ok");
writer.writeStringField("databaseInfo", databaseInfoString);
writer.writeEndObject();
} catch (DatabaseServiceException e) {
logger.error("ConnectCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED, response, e);
} catch (Exception e) {
logger.error("ConnectCommand::Post::Exception::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED, response, e);
} finally {
writer.flush();
writer.close();
w.close();
}
} catch (Exception e) {
logger.error("ConnectCommand::Post::Exception::{}", e);
throw new ServletException(e);
}
// finally {
// // ProjectManager.singleton.setBusy(false);
// }
}
use of com.google.refine.extension.database.model.DatabaseInfo in project OpenRefine by OpenRefine.
the class ExecuteQueryCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
DatabaseConfiguration databaseConfiguration = getJdbcConfiguration(request);
String query = request.getParameter("queryString");
if (logger.isDebugEnabled()) {
logger.debug("QueryCommand::Post::DatabaseConfiguration::{}::Query::{} ", databaseConfiguration, query);
}
// ProjectManager.singleton.setBusy(true);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
try {
DatabaseInfo databaseInfo = DatabaseService.get(databaseConfiguration.getDatabaseType()).executeQuery(databaseConfiguration, query);
ObjectMapper mapperObj = new ObjectMapper();
response.setStatus(HttpStatus.SC_OK);
String jsonStr = mapperObj.writeValueAsString(databaseInfo);
if (logger.isDebugEnabled()) {
logger.debug("QueryCommand::Post::Result::{} ", jsonStr);
}
writer.writeStartObject();
writer.writeStringField("code", "ok");
writer.writeStringField("QueryResult", jsonStr);
writer.writeEndObject();
} catch (DatabaseServiceException e) {
logger.error("QueryCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_BAD_REQUEST, response, e);
} catch (Exception e) {
logger.error("QueryCommand::Post::Exception::{}", e);
sendError(HttpStatus.SC_BAD_REQUEST, response, e);
} finally {
writer.flush();
writer.close();
w.close();
}
} catch (Exception e) {
logger.error("QueryCommand::Post::Exception::{}", e);
throw new ServletException(e);
}
// finally {
// // ProjectManager.singleton.setBusy(false);
// }
}
Aggregations