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()); }
JDBCSchemaConfig source = new JDBCSchemaConfig() .setDatabase(uri.getPath().substring(1).split("\\?", 2)[0]) .setHost(uri.getHost()) .setUsername(userJdbcConfig.getUsername()) .setPassword(userJdbcConfig.getPassword()) .setSchema("users");
@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; }
@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); } } } }
ResultSet dbColumns = conn.getMetaData().getTables(null, customDataSource.options.getSchema(), null, null);
@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 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 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, '`')),
@Override public Connection openConnection(JDBCSchemaConfig factory) throws SQLException { Properties properties = new Properties(); Optional.ofNullable(factory.getPassword()) .ifPresent(pass -> properties.setProperty("password", pass)); Optional.ofNullable(factory.getUsername()) .ifPresent(user -> properties.setProperty("user", user)); properties.setProperty("loginTimeout", "120"); properties.setProperty("socketTimeout", "120"); properties.setProperty("connectTimeout", "120"); if (factory.getEnableSSL() != null) { properties.setProperty("ssl", factory.getEnableSSL().toString()); } return new org.postgresql.Driver().connect( format("jdbc:postgresql://%s:%s/%s", factory.getHost(), Optional.ofNullable(factory.getPort()).orElse(5432), factory.getDatabase()), properties); } }, (jdbcSchemaConfig, table) -> ofNullable(jdbcSchemaConfig.getSchema())