public RemoteTable getTable() { List<SchemaField> columns = (this.columns == null ? ExternalFileCustomDataSource.fillColumnIfNotSet(typeOptions, format, url, indexUrl) : this.columns); return new RemoteTable(url, indexUrl, typeOptions, columns, compressionType, format); } }
private boolean inSameDatabase(CustomDataSource current, CustomDataSource dataSource) { if (current.schemaName.equals(dataSource)) { return true; } return current.type.equals(dataSource.type) && current.options.getHost().equals(dataSource.options.getHost()) && Objects.equals(current.options.getUsername(), dataSource.options.getUsername()) && Objects.equals(current.options.getPassword(), dataSource.options.getPassword()) && Objects.equals(current.options.getPort(), dataSource.options.getPort()) && Objects.equals(current.options.getEnableSSL(), dataSource.options.getEnableSSL()); }
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); }
@Override public Connection openConnection(JDBCSchemaConfig config) throws SQLException { Properties info = new Properties(); String userName = config.getUsername(); String password = config.getPassword(); Optional.ofNullable(userName).map(value -> info.put("user", value)); Optional.ofNullable(password).map(value -> info.put("password", value)); return new com.microsoft.sqlserver.jdbc.SQLServerDriver() .connect(format("jdbc:sqlserver://%s:%s;databaseName=%s", config.getHost(), Optional.ofNullable(config.getPort()).orElse(1433), config.getDatabase()), info); } }, (jdbcSchemaConfig, table) -> ofNullable(jdbcSchemaConfig.getSchema())
public SuccessMessage testDatabase(String project, String type, JDBCSchemaConfig options) { SupportedCustomDatabase optionalFunction = SupportedCustomDatabase.getAdapter(type); Optional<String> test = optionalFunction.getDataSource().test(options); if (test.isPresent()) { throw new RakamException(test.get(), BAD_REQUEST); } return SuccessMessage.success(); }
@ApiOperation(value = "List data-sources", authorizations = @Authorization(value = "master_key")) @Path("/list") @GET public CustomDataSourceService.CustomDataSourceList listDatabases(@Named("project") RequestContext context) { CustomDataSourceService.CustomDataSourceList customDataSourceList = service.listDatabases(context.project); for (CustomDataSource customDataSource : customDataSourceList.customDataSources) { customDataSource.options.setPassword(null); } return customDataSourceList; }
@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 CustomDataSourceList listDatabases(String project) { try (Handle handle = dbi.open()) { List<CustomDataSource> customDataSources = handle.createQuery("SELECT schema_name, type, options FROM custom_data_source WHERE project = :project") .bind("project", project) .map((index, r, ctx) -> { JDBCSchemaConfig read = JsonHelper.read(r.getString(3), JDBCSchemaConfig.class); return new CustomDataSource(r.getString(2), r.getString(1), read); }).list(); HashMap<String, RemoteTable> files = new HashMap<>(); handle.createQuery("SELECT table_name, options FROM custom_file_source WHERE project = :project") .bind("project", project) .map((index, r, ctx) -> { files.put(r.getString(1), JsonHelper.read(r.getString(2), RemoteTable.class)); return null; }).list(); return new CustomDataSourceList(customDataSources, files); } }
public SuccessMessage testFile(String project, DiscoverableRemoteTable hook) { ExternalFileCustomDataSource source = new ExternalFileCustomDataSource(); Optional<String> test = source.test(hook.getTable()); if (test.isPresent()) { throw new RakamException(test.get(), BAD_REQUEST); } return SuccessMessage.success(); }
@Inject public ActiveModuleListBuilder(UserPluginConfig userPluginConfig, CustomDataSourceConfig customDataSourceConfig, TaskConfig taskConfig, Optional<UserMailboxStorage> mailboxStorage, RealTimeConfig realtimeConfig, EventStreamConfig eventStreamConfig, EventExplorerConfig eventExplorerConfig, UserPluginConfig userStorage, ProjectConfig projectConfig) { this.userPluginConfig = userPluginConfig; this.realtimeConfig = realtimeConfig; this.eventStreamConfig = eventStreamConfig; this.eventExplorerConfig = eventExplorerConfig; this.userStorage = userStorage; this.projectConfig = projectConfig; this.customDataSourceEnabled = customDataSourceConfig.getEnabled(); this.userStorageMailbox = mailboxStorage.isPresent(); this.taskConfig = taskConfig; }
@Override public Optional<String> test(JDBCSchemaConfig factory) { Connection connect = null; try { connect = openConnection(factory); String schemaPattern = connect.getSchema() == null ? "public" : factory.getSchema(); ResultSet schemas = connect.getMetaData().getSchemas(null, schemaPattern); return schemas.next() ? Optional.empty() : Optional.of(format("Schema '%s' does not exist", schemaPattern)); } catch (SQLException e) { return Optional.of(e.getMessage()); } finally { if (connect != null) { try { connect.close(); } catch (SQLException e) { throw Throwables.propagate(e); } } } }
@ApiOperation(value = "Get file", authorizations = @Authorization(value = "master_key")) @Path("/list/file") @JsonRequest public Map<String, RemoteTable> getFiles(@Named("project") RequestContext context) { return service.getFiles(context.project); }
@ApiOperation(value = "Get data-source", authorizations = @Authorization(value = "master_key")) @Path("/get/file") @JsonRequest public RemoteTable getFile(@Named("project") RequestContext context, String tableName) { return service.getFile(context.project, tableName); }
@ApiOperation(value = "Schema of data-sources", authorizations = @Authorization(value = "read_key")) @Path("/schema/tables") @GET public CompletableFuture<Map<String, List<String>>> schemaDatabases(@Named("project") RequestContext context) { return service.schemaDatabases(context.project); }
@ApiOperation(value = "Add data-source", authorizations = @Authorization(value = "master_key")) @Path("/add/database") @JsonRequest public SuccessMessage addDatabase(@Named("project") RequestContext context, @BodyParam CustomDataSource hook) { return service.addDatabase(context.project, hook); }
@ApiOperation(value = "Test database", authorizations = @Authorization(value = "master_key")) @Path("/test/file") @JsonRequest public SuccessMessage testFile(@Named("project") RequestContext context, @BodyParam CustomDataSourceService.DiscoverableRemoteTable hook) { return service.testFile(context.project, hook); } }
@ApiOperation(value = "Test database", authorizations = @Authorization(value = "master_key")) @Path("/test/database") @JsonRequest public SuccessMessage testDatabase(@Named("project") RequestContext context, @ApiParam("type") String type, @ApiParam(value = "options") JDBCSchemaConfig options) { return service.testDatabase(context.project, type, options); }
@ApiOperation(value = "Add file data-source", authorizations = @Authorization(value = "master_key")) @Path("/add/file") @JsonRequest public SuccessMessage addFile(@Named("project") RequestContext context, @ApiParam("tableName") String tableName, @ApiParam("options") CustomDataSourceService.DiscoverableRemoteTable hook) { return service.addFile(context.project, tableName, hook); }
@Override public Connection openConnection(JDBCSchemaConfig factory) throws SQLException { Properties info = new Properties(); Optional.ofNullable(factory.getUsername()).map(value -> info.put("user", value)); Optional.ofNullable(factory.getPassword()).map(value -> info.put("password", value)); info.setProperty("socketTimeout", "120000"); info.setProperty("connectTimeout", "120000"); return new com.mysql.jdbc.Driver().connect(format("jdbc:mysql://%s:%s/%s", factory.getHost(), Optional.ofNullable(factory.getPort()).orElse(3306), factory.getDatabase()), info); } }, (jdbcSchemaConfig, table) -> checkCollection(jdbcSchemaConfig.getDatabase(), '`') + '.' + checkCollection(table, '`')),