mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-11-22 07:30:24 +00:00
Refactor, small fixes
This commit is contained in:
parent
696568d5bc
commit
b8164339d0
23 changed files with 95 additions and 164 deletions
|
@ -3,7 +3,7 @@ package io.xpipe.beacon.exchange.cli;
|
|||
import io.xpipe.beacon.exchange.MessageExchange;
|
||||
import io.xpipe.beacon.message.RequestMessage;
|
||||
import io.xpipe.beacon.message.ResponseMessage;
|
||||
import io.xpipe.core.config.DialogElement;
|
||||
import io.xpipe.core.dialog.DialogElement;
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
import lombok.extern.jackson.Jacksonized;
|
||||
|
|
|
@ -3,7 +3,7 @@ package io.xpipe.beacon.exchange.cli;
|
|||
import io.xpipe.beacon.exchange.MessageExchange;
|
||||
import io.xpipe.beacon.message.RequestMessage;
|
||||
import io.xpipe.beacon.message.ResponseMessage;
|
||||
import io.xpipe.core.config.DialogElement;
|
||||
import io.xpipe.core.dialog.DialogElement;
|
||||
import lombok.Builder;
|
||||
import lombok.NonNull;
|
||||
import lombok.Value;
|
||||
|
|
|
@ -26,6 +26,10 @@ public class SimpleImmutableValueNode extends ImmutableValueNode {
|
|||
|
||||
@Override
|
||||
public final String asString() {
|
||||
if (getRawData() == null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
return new String(getRawData());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,10 @@ public abstract class ValueNode extends DataStructureNode {
|
|||
}
|
||||
|
||||
public static ValueNode immutable(Object o, boolean textual) {
|
||||
if (o == null) {
|
||||
return immutableNull();
|
||||
}
|
||||
|
||||
return immutable(o.toString().getBytes(StandardCharsets.UTF_8), textual);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import lombok.AllArgsConstructor;
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
@ -68,6 +68,29 @@ public abstract class Dialog {
|
|||
};
|
||||
}
|
||||
|
||||
public static Dialog repeatIf(Dialog d, Supplier<Boolean> shouldRepeat) {
|
||||
return new Dialog() {
|
||||
|
||||
|
||||
@Override
|
||||
public DialogElement start() {
|
||||
return d.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DialogElement receive(String answer) {
|
||||
var next = d.receive(answer);
|
||||
if (next == null) {
|
||||
if (shouldRepeat.get()) {
|
||||
return d.start();
|
||||
}
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
}.evaluateTo(d.onCompletion);
|
||||
}
|
||||
|
||||
public static Dialog of(DialogElement e) {
|
||||
return new Dialog() {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import lombok.EqualsAndHashCode;
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
|
@ -1,5 +1,7 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public abstract class QueryConverter<T> {
|
||||
|
@ -28,6 +30,22 @@ public abstract class QueryConverter<T> {
|
|||
}
|
||||
};
|
||||
|
||||
public static final QueryConverter<URL> URL = new QueryConverter<URL>() {
|
||||
@Override
|
||||
protected URL fromString(String s) {
|
||||
try {
|
||||
return new URL(s);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String toString(URL value) {
|
||||
return value.toString();
|
||||
}
|
||||
};
|
||||
|
||||
public static final QueryConverter<Integer> INTEGER = new QueryConverter<Integer>() {
|
||||
@Override
|
||||
protected Integer fromString(String s) {
|
|
@ -1,4 +1,4 @@
|
|||
package io.xpipe.core.config;
|
||||
package io.xpipe.core.dialog;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
|
@ -1,8 +1,8 @@
|
|||
package io.xpipe.core.source;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import io.xpipe.core.config.ConfigParameter;
|
||||
import io.xpipe.core.config.ConfigParameterSetInstance;
|
||||
import io.xpipe.core.dialog.ConfigParameter;
|
||||
import io.xpipe.core.dialog.ConfigParameterSetInstance;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Value;
|
||||
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
package io.xpipe.core.source;
|
||||
|
||||
import io.xpipe.core.data.node.*;
|
||||
import io.xpipe.core.data.type.TupleType;
|
||||
import io.xpipe.core.data.type.ValueType;
|
||||
import io.xpipe.core.store.JdbcStore;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class JdbcQuerySource extends TableDataSource<JdbcStore> {
|
||||
|
||||
JdbcStore store;
|
||||
|
||||
protected abstract String createQuery();
|
||||
|
||||
public Connection createConnection() throws SQLException {
|
||||
return store.createConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean supportsRead() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TableReadConnection newReadConnection() {
|
||||
return new TableReadConnection() {
|
||||
|
||||
private Connection connection;
|
||||
private Statement statement;
|
||||
private TupleType dataType;
|
||||
private ResultSet resultSet;
|
||||
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
connection = createConnection();
|
||||
statement = connection.createStatement();
|
||||
|
||||
resultSet = statement.executeQuery(createQuery());
|
||||
var meta = resultSet.getMetaData();
|
||||
var names = new ArrayList<String>();
|
||||
for (int i = 0; i < meta.getColumnCount(); i++) {
|
||||
names.add(meta.getColumnName(i + 1));
|
||||
}
|
||||
dataType = TupleType.of(names, Collections.nCopies(names.size(), ValueType.of()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
statement.close();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TupleType getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowCount() throws Exception {
|
||||
return resultSet.getFetchSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withRows(DataStructureNodeAcceptor<TupleNode> lineAcceptor) throws Exception {
|
||||
while (resultSet.next()) {
|
||||
var vals = new ArrayList<DataStructureNode>();
|
||||
for (int i = 0; i < dataType.getSize(); i++) {
|
||||
vals.add(ValueNode.of(resultSet.getString(i)));
|
||||
}
|
||||
|
||||
var node = TupleNode.of(dataType.getNames(), vals);
|
||||
if (!lineAcceptor.accept(node)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayNode readRows(int maxLines) throws Exception {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -41,9 +41,15 @@ public abstract class TableDataSource<DS extends DataStore> extends DataSource<D
|
|||
return con;
|
||||
}
|
||||
|
||||
protected abstract TableWriteConnection newWriteConnection();
|
||||
protected TableWriteConnection newWriteConnection() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
protected abstract TableWriteConnection newAppendingWriteConnection();
|
||||
protected TableWriteConnection newAppendingWriteConnection() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
protected abstract TableReadConnection newReadConnection();
|
||||
protected TableReadConnection newReadConnection() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
package io.xpipe.core.store;
|
||||
|
||||
import lombok.*;
|
||||
import lombok.experimental.FieldDefaults;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
public abstract class JdbcStore implements DataStore {
|
||||
|
||||
String hostname;
|
||||
int port;
|
||||
|
||||
public void checkConnect() throws Exception {
|
||||
try (Connection con = createConnection()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public Connection createConnection() throws SQLException {
|
||||
return DriverManager.getConnection(toUrl(), toProperties());
|
||||
}
|
||||
|
||||
public abstract String toUrl();
|
||||
|
||||
public abstract Properties toProperties();
|
||||
}
|
|
@ -11,9 +11,9 @@ import com.fasterxml.jackson.databind.SerializerProvider;
|
|||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.jsontype.NamedType;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import io.xpipe.core.config.BaseQueryElement;
|
||||
import io.xpipe.core.config.ChoiceElement;
|
||||
import io.xpipe.core.config.HeaderElement;
|
||||
import io.xpipe.core.dialog.BaseQueryElement;
|
||||
import io.xpipe.core.dialog.ChoiceElement;
|
||||
import io.xpipe.core.dialog.HeaderElement;
|
||||
import io.xpipe.core.data.type.ArrayType;
|
||||
import io.xpipe.core.data.type.TupleType;
|
||||
import io.xpipe.core.data.type.ValueType;
|
||||
|
|
|
@ -8,6 +8,8 @@ module io.xpipe.core {
|
|||
exports io.xpipe.core.util;
|
||||
exports io.xpipe.core.data.node;
|
||||
exports io.xpipe.core.data.typed;
|
||||
exports io.xpipe.core.dialog;
|
||||
exports io.xpipe.core.connection;
|
||||
|
||||
opens io.xpipe.core.store;
|
||||
opens io.xpipe.core.source;
|
||||
|
@ -16,15 +18,12 @@ module io.xpipe.core {
|
|||
opens io.xpipe.core.util;
|
||||
opens io.xpipe.core.data.node;
|
||||
opens io.xpipe.core.data.typed;
|
||||
exports io.xpipe.core.config;
|
||||
opens io.xpipe.core.config;
|
||||
exports io.xpipe.core.connection;
|
||||
opens io.xpipe.core.dialog;
|
||||
|
||||
requires com.fasterxml.jackson.core;
|
||||
requires com.fasterxml.jackson.databind;
|
||||
requires java.net.http;
|
||||
requires static lombok;
|
||||
requires java.sql;
|
||||
|
||||
uses com.fasterxml.jackson.databind.Module;
|
||||
provides com.fasterxml.jackson.databind.Module with CoreJacksonModule;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package io.xpipe.extension;
|
||||
|
||||
import io.xpipe.charsetter.NewLine;
|
||||
import io.xpipe.core.config.QueryConverter;
|
||||
import io.xpipe.core.config.ConfigParameter;
|
||||
import io.xpipe.core.dialog.QueryConverter;
|
||||
import io.xpipe.core.dialog.ConfigParameter;
|
||||
import io.xpipe.core.source.DataSource;
|
||||
import io.xpipe.core.source.DataSourceType;
|
||||
import io.xpipe.core.store.DataStore;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.xpipe.extension;
|
||||
|
||||
import io.xpipe.core.config.Dialog;
|
||||
import io.xpipe.core.dialog.Dialog;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.xpipe.extension;
|
||||
|
||||
import io.xpipe.core.config.Dialog;
|
||||
import io.xpipe.core.dialog.Dialog;
|
||||
import io.xpipe.extension.event.ErrorEvent;
|
||||
|
||||
import java.net.URL;
|
||||
|
@ -17,7 +17,7 @@ public class DataStoreProviders {
|
|||
public static void init(ModuleLayer layer) {
|
||||
if (ALL == null) {
|
||||
ALL = ServiceLoader.load(layer, DataStoreProvider.class).stream()
|
||||
.map(p -> (DataStoreProvider) p.get()).collect(Collectors.toSet());
|
||||
.map(ServiceLoader.Provider::get).collect(Collectors.toSet());
|
||||
ALL.forEach(p -> {
|
||||
try {
|
||||
p.init();
|
||||
|
|
|
@ -25,14 +25,6 @@ public class DynamicOptionsBuilder<T extends DataSource<?>> {
|
|||
private final List<DynamicOptionsComp.Entry> entries = new ArrayList<>();
|
||||
private final List<Property<?>> props = new ArrayList<>();
|
||||
|
||||
public DynamicOptionsBuilder<T> addText(ObservableValue<String> name, Property<String> prop) {
|
||||
var comp = new TextField();
|
||||
comp.textProperty().bindBidirectional(prop);
|
||||
entries.add(new DynamicOptionsComp.Entry(name, Comp.of(() -> comp)));
|
||||
props.add(prop);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DynamicOptionsBuilder<T> addNewLine(Property<NewLine> prop) {
|
||||
var map = new LinkedHashMap<NewLine, ObservableValue<String>>();
|
||||
for (var e : NewLine.values()) {
|
||||
|
@ -72,6 +64,19 @@ public class DynamicOptionsBuilder<T extends DataSource<?>> {
|
|||
return this;
|
||||
}
|
||||
|
||||
public DynamicOptionsBuilder<T> addString(ObservableValue<String> name, Property<String> prop) {
|
||||
var comp = Comp.of(() -> {
|
||||
var tf = new TextField(prop.getValue());
|
||||
tf.textProperty().addListener((c, o, n) -> {
|
||||
prop.setValue(n.length() > 0 ? n : null);
|
||||
});
|
||||
return tf;
|
||||
});
|
||||
entries.add(new DynamicOptionsComp.Entry(name, comp));
|
||||
props.add(prop);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Region build(Function<T, T> creator, Property<T> toBind) {
|
||||
var bind = Bindings.createObjectBinding(() -> creator.apply(toBind.getValue()), props.toArray(Observable[]::new));
|
||||
bind.addListener((c,o, n) -> {
|
||||
|
|
Loading…
Reference in a new issue