use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.
the class DBCPConnectionPoolService method generatePreviewQueryForDatasource.
/**
* Generates a preview query on a table that is appropriate for the type of specified data source.
*
* @param datasource the JDBC datasource
* @param schema the schema determining the SQL dialect, or null/empty for the default dialect
* @param tableName the name of the table
* @param limit the result size
* @return the query string
* @throws IllegalArgumentException if the datasource is invalid
*/
@Nonnull
public String generatePreviewQueryForDatasource(@Nonnull final JdbcDatasource datasource, @Nonnull final String schema, @Nonnull final String tableName, final int limit) {
final Optional<ControllerServiceDTO> controllerService = Optional.ofNullable(datasource.getControllerServiceId()).map(id -> getControllerService(id, null));
if (controllerService.isPresent()) {
final DatabaseType dbType = DatabaseType.fromJdbcConnectionString(datasource.getDatabaseConnectionUrl());
final ExecuteQueryControllerServiceRequest serviceProperties = new ExecuteQueryControllerServiceRequestBuilder(controllerService.get()).previewQuery(dbType, schema, tableName, limit).build();
return serviceProperties.getQuery();
} else {
log.error("Cannot execute query for datasource: {}", datasource);
throw new IllegalArgumentException("Missing controller service for datasource: " + datasource);
}
}
use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.
the class DBCPConnectionPoolService method executePreviewQueryForControllerService.
/**
* Executes preview query on a table, in the context of the specified controller service, based
* on the supplied schema (if provided) and limit.
*
* @param serviceId a NiFi controller service id
* @param serviceName a NiFi controller service name
* @param tableName the name of the table
* @param schema the schema determining the SQL dialect, or null/empty for the default dialect
* @param limit the result size
* @return the query results
* @throws DataAccessException if the query cannot be executed
* @throws IllegalArgumentException if the controller service cannot be found, the schema/tableName is empty, or the limit is less than zero
*/
public QueryResult executePreviewQueryForControllerService(final String serviceId, final String serviceName, @Nonnull final String schema, @Nonnull final String tableName, final int limit) {
Validate.notEmpty(schema, "No schema provided");
Validate.notEmpty(tableName, "No table name provided");
Validate.isTrue(limit >= 0, "The query result size must be greater than or equal to 0");
final ControllerServiceDTO controllerService = getControllerService(serviceId, serviceName);
if (controllerService != null) {
final ExecuteQueryControllerServiceRequest serviceProperties = new ExecuteQueryControllerServiceRequestBuilder(controllerService).build();
final PoolingDataSourceService.DataSourceProperties dataSourceProperties = getDataSourceProperties(serviceProperties);
final DatabaseType dbType = DatabaseType.fromJdbcConnectionString(dataSourceProperties.getUrl());
final ExecuteQueryControllerServiceRequest previewRequest = new ExecuteQueryControllerServiceRequestBuilder(controllerService).using(serviceProperties).previewQuery(dbType, schema, tableName, limit).build();
return executeQueryForControllerService(dataSourceProperties, previewRequest);
} else {
log.error("Cannot execute query for controller service. Unable to obtain controller service: {}, {}", serviceId, serviceName);
throw new IllegalArgumentException("Not a valid controller service: " + serviceId + ", " + serviceName);
}
}
use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.
the class DBCPConnectionPoolService method parseValidationQueryFromConnectionString.
/**
* get the validation query from the db name that is parsed from the
*/
private String parseValidationQueryFromConnectionString(String connectionString) {
String validationQuery = null;
try {
if (StringUtils.isNotBlank(connectionString)) {
DatabaseType databaseType = DatabaseType.fromJdbcConnectionString(connectionString);
validationQuery = databaseType.getValidationQuery();
}
} catch (IllegalArgumentException e) {
// if we cant find it in the map its ok.
}
return validationQuery;
}
use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.
the class DataSourceSupplier method get.
@Override
public DataSource get() {
if (cache == null) {
// Get validation query
final DatabaseType databaseType = DatabaseType.fromJdbcConnectionString(config.getDatabaseConnectionUrl());
final String validationQuery = databaseType.getValidationQuery();
// Build data source
final PoolingDataSourceService.DataSourceProperties dataSourceProperties = new PoolingDataSourceService.DataSourceProperties(config.getDatabaseUser(), config.getPassword(), config.getDatabaseConnectionUrl(), config.getDatabaseDriverClassName(), (validationQuery != null), validationQuery);
dataSourceProperties.setDriverLocation(config.getDatabaseDriverLocation());
cache = PoolingDataSourceService.getDataSource(dataSourceProperties);
}
return cache;
}
use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.
the class DatasourceController method testConnection.
@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Tests datasource connection")
@ApiResponses({ @ApiResponse(code = 200, message = "Connection was tested, test failure will appear in message, successful connections return no message", response = RestResponseStatus.class), @ApiResponse(code = 400, message = "UserDatasource is not a JdbcDatasource", response = RestResponseStatus.class) })
public Response testConnection(@Nonnull final UserDatasource datasource) {
return metadata.commit(() -> {
accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_DATASOURCES);
if (datasource instanceof JdbcDatasource) {
JdbcDatasource jdbcDatasource = (JdbcDatasource) datasource;
DatabaseType databaseType = DatabaseType.fromJdbcConnectionString(jdbcDatasource.getDatabaseConnectionUrl());
String query = databaseType.getValidationQuery();
try {
dbcpConnectionPoolTableInfo.testConnectionForDatasource(jdbcDatasource, query);
return Response.ok().build();
} catch (CannotGetJdbcConnectionException e) {
Map<String, String> message = new HashMap<>(1);
message.put("message", e.getRootCause().toString());
return Response.ok(message).build();
}
} else {
return Response.status(Response.Status.BAD_REQUEST).build();
}
});
}
Aggregations