Skip to content

Commit e4bbeae

Browse files
committed
Query node api improvements, rollback and savepoint option in Transaction
1 parent a8c4eec commit e4bbeae

7 files changed

Lines changed: 38 additions & 10 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.zort.sqllib.internal.query;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Top-level query node that is used to build a query.
8+
*
9+
* @author ZorTik
10+
*/
11+
public abstract class AncestorQueryNode extends QueryNode<QueryNode<?>> {
12+
public AncestorQueryNode() {
13+
this(new ArrayList<>());
14+
}
15+
public AncestorQueryNode(List<QueryNode<?>> initial) {
16+
super(null, initial, QueryPriority.GENERAL);
17+
}
18+
}

asql-core/src/main/java/me/zort/sqllib/internal/query/DeleteQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.ArrayList;
1111
import java.util.Objects;
1212

13-
public class DeleteQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<DeleteQuery>, Limitable<DeleteQuery> {
13+
public class DeleteQuery extends AncestorQueryNode implements Executive, Conditional<DeleteQuery>, Limitable<DeleteQuery> {
1414

1515
private String table;
1616

@@ -26,7 +26,7 @@ public DeleteQuery(@Nullable SQLDatabaseConnection connection) {
2626
}
2727

2828
public DeleteQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table) {
29-
super(null, new ArrayList<>(), QueryPriority.GENERAL);
29+
super(new ArrayList<>());
3030
this.table = table;
3131
this.connection = connection;
3232
}

asql-core/src/main/java/me/zort/sqllib/internal/query/InsertQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.ArrayList;
1212
import java.util.Objects;
1313

14-
public class InsertQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<InsertQuery> {
14+
public class InsertQuery extends AncestorQueryNode implements Executive, Conditional<InsertQuery> {
1515

1616
@Getter
1717
private String table;
@@ -31,7 +31,7 @@ public InsertQuery(@Nullable SQLDatabaseConnection connection) {
3131
}
3232

3333
public InsertQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table) {
34-
super(null, new ArrayList<>(), QueryPriority.GENERAL);
34+
super(new ArrayList<>());
3535
this.table = table;
3636
this.connection = connection;
3737
this.defs = new String[0];

asql-core/src/main/java/me/zort/sqllib/internal/query/SelectQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.List;
1313
import java.util.Objects;
1414

15-
public class SelectQuery extends QueryNode<QueryNode<?>> implements
15+
public class SelectQuery extends AncestorQueryNode implements
1616
Executive, Conditional<SelectQuery>, Limitable<SelectQuery>, ResultSetAware {
1717

1818
private final List<String> cols;
@@ -30,7 +30,7 @@ public SelectQuery(@Nullable SQLDatabaseConnection connection, String... cols) {
3030
}
3131

3232
public SelectQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table, List<String> cols) {
33-
super(null, new ArrayList<>(), QueryPriority.GENERAL);
33+
super(new ArrayList<>());
3434
this.table = table;
3535
this.cols = cols;
3636
this.connection = connection;

asql-core/src/main/java/me/zort/sqllib/internal/query/UpdateQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.ArrayList;
1111
import java.util.Objects;
1212

13-
public class UpdateQuery extends QueryNode<QueryNode<?>> implements Executive, Conditional<UpdateQuery> {
13+
public class UpdateQuery extends AncestorQueryNode implements Executive, Conditional<UpdateQuery> {
1414

1515
private String table;
1616

@@ -26,7 +26,7 @@ public UpdateQuery(@Nullable SQLDatabaseConnection connection) {
2626
}
2727

2828
public UpdateQuery(@Nullable SQLDatabaseConnection connection, @Nullable String table) {
29-
super(null, new ArrayList<>(), QueryPriority.GENERAL.getPrior());
29+
super(new ArrayList<>());
3030
this.table = table;
3131
this.connection = connection;
3232
}

asql-core/src/main/java/me/zort/sqllib/transaction/Transaction.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.zort.sqllib.transaction;
22

33
import me.zort.sqllib.SQLDatabaseConnection;
4+
import me.zort.sqllib.api.data.QueryResult;
45

56
import java.sql.Connection;
67
import java.sql.SQLException;
@@ -51,6 +52,16 @@ public void rollback() throws SQLException {
5152
connection.rollback();
5253
}
5354

55+
public QueryResult rollback(String savepoint) {
56+
verify();
57+
return databaseConnection.exec(() -> "ROLLBACK TO " + savepoint + ";");
58+
}
59+
60+
public QueryResult savepoint(String savepoint) {
61+
verify();
62+
return databaseConnection.exec(() -> "SAVEPOINT " + savepoint + ";");
63+
}
64+
5465
public boolean isActive() {
5566
return !committed;
5667
}

asql-debezium/src/main/java/me/zort/sqllib/debezium/ASQLDebeziumWatcher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.IOException;
1515
import java.lang.reflect.AnnotatedElement;
1616
import java.net.URI;
17-
import java.net.URISyntaxException;
1817
import java.sql.DatabaseMetaData;
1918
import java.util.HashMap;
2019
import java.util.List;
@@ -66,7 +65,7 @@ public final class ASQLDebeziumWatcher
6665
int port,
6766
@NotNull String username,
6867
@NotNull String password
69-
) throws URISyntaxException {
68+
) {
7069
Configuration.Builder configBuilder = Configuration.create()
7170
.with("database.hostname", hostname)
7271
.with("database.port", String.valueOf(port))

0 commit comments

Comments
 (0)