@ApiOperation(value = "Get data-source", authorizations = @Authorization(value = "master_key")) @Path("/get/database") @JsonRequest public CustomDataSource getDatabase(@Named("project") RequestContext context, String schema) { return service.getDatabase(context.project, schema); }
public CompletableFuture<List<SchemaField>> schemaTable(String project, String schema, String table) { CustomDataSource customDataSource = getDatabase(project, schema); List<SchemaField> builder = new ArrayList<>(); return CompletableFuture.supplyAsync(() -> { SupportedCustomDatabase source = SupportedCustomDatabase.getAdapter(customDataSource.type); try (Connection conn = source.getDataSource().openConnection(customDataSource.options)) { ResultSet dbColumns = conn.getMetaData().getColumns(null, customDataSource.options.getSchema(), table, null); while (dbColumns.next()) { String columnName = dbColumns.getString("COLUMN_NAME"); FieldType fieldType; try { fieldType = fromSql(dbColumns.getInt("DATA_TYPE"), dbColumns.getString("TYPE_NAME")); } catch (UnsupportedOperationException e) { continue; } builder.add(new SchemaField(columnName, fieldType)); } return builder; } catch (SQLException e) { throw Throwables.propagate(e); } }, executor); }
CustomDataSource dataSource; try { dataSource = customDataSource.getDatabase(project, prefix); } catch (RakamException e) { if (e.getStatusCode() == NOT_FOUND) {
CustomDataSource dataSource = customDataSource.getDatabase(project, prefix);
public SuccessMessage addDatabase(String project, CustomDataSource hook) { try (Handle handle = dbi.open()) { try { handle.createStatement("INSERT INTO custom_data_source (project, schema_name, type, options) " + "VALUES (:project, :schema_name, :type, :options)") .bind("project", project) .bind("schema_name", hook.schemaName) .bind("type", hook.type) .bind("options", JsonHelper.encode(hook.options)) .execute(); return SuccessMessage.success(); } catch (Exception e) { try { getDatabase(project, hook.schemaName); throw new AlreadyExistsException("Custom database", BAD_REQUEST); } catch (RakamException e1) { if (e1.getStatusCode() != NOT_FOUND) { throw e1; } else { throw e; } } } } }